HeartbeatStreamForm.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Drupal\heartbeat8\Form;
  3. use Drupal\Component\Datetime\TimeInterface;
  4. use Drupal\Core\Entity\EntityManager;
  5. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  6. use Drupal\heartbeat8\HeartbeatTypeServices;
  7. use Drupal\heartbeat8\Entity\HeartbeatStream;
  8. use Drupal\heartbeat8\Entity\HeartbeatType;
  9. use Drupal\Core\Database\Database;
  10. use Drupal\Core\Entity\ContentEntityForm;
  11. use Drupal\Core\Form\FormStateInterface;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Form controller for Heartbeat stream edit forms.
  15. *
  16. * @ingroup heartbeat8
  17. */
  18. class HeartbeatStreamForm extends ContentEntityForm {
  19. protected $heartbeatTypeService;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function create(ContainerInterface $container) {
  24. return new static(
  25. $container->get('heartbeat8.heartbeattype'),
  26. $container->get('entity.manager'),
  27. $container->get('entity_type.bundle.info'),
  28. $container->get('datetime.time')
  29. );
  30. }
  31. /**
  32. * PHP 5 allows developers to declare constructor methods for classes.
  33. * Classes which have a constructor method call this method on each newly-created object,
  34. * so it is suitable for any initialization that the object may need before it is used.
  35. *
  36. * Note: Parent constructors are not called implicitly if the child class defines a constructor.
  37. * In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
  38. *
  39. * param [ mixed $args [, $... ]]
  40. * @param TreeBuilder $tree_builder
  41. * @param Renderer $renderer
  42. * @throws \Exception
  43. */
  44. public function __construct(HeartbeatTypeServices $heartbeatTypeService, EntityManager $entityManager) {
  45. parent::__construct($entityManager);
  46. $this->heartbeatTypeService = $heartbeatTypeService;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function buildForm(array $form, FormStateInterface $form_state) {
  52. /* @var $entity \Drupal\heartbeat8\Entity\HeartbeatStream */
  53. $form = parent::buildForm($form, $form_state);
  54. if (!$this->entity->isNew()) {
  55. $form['new_revision'] = array(
  56. '#type' => 'checkbox',
  57. '#title' => $this->t('Create new revision'),
  58. '#default_value' => FALSE,
  59. '#weight' => 10,
  60. );
  61. }
  62. $form['types'] = array(
  63. '#type' => 'checkboxes',
  64. '#options' => $this->heartbeatTypeService->getTypes(),
  65. '#title' => $this->t('Please select all the Heartbeat Types you wish to include in this stream'),
  66. );
  67. return $form;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function save(array $form, FormStateInterface $form_state) {
  73. $entity = &$this->entity;
  74. // Save as a new revision if requested to do so.
  75. if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) {
  76. $entity->setNewRevision();
  77. // If a new revision is created, save the current user as revision author.
  78. $entity->setRevisionCreationTime(REQUEST_TIME);
  79. $entity->setRevisionUserId(\Drupal::currentUser()->id());
  80. }
  81. else {
  82. $entity->setNewRevision(FALSE);
  83. }
  84. if ($entity instanceof HeartbeatStream) {
  85. foreach ($form_state->getValue('types') as $type) {
  86. $entity->get('types')->appendItem($type);
  87. }
  88. $entity->save();
  89. }
  90. $status = parent::save($form, $form_state);
  91. switch ($status) {
  92. case SAVED_NEW:
  93. drupal_set_message($this->t('Created the %label Heartbeat stream.', [
  94. '%label' => $entity->label(),
  95. ]));
  96. break;
  97. default:
  98. drupal_set_message($this->t('Saved the %label Heartbeat stream.', [
  99. '%label' => $entity->label(),
  100. ]));
  101. }
  102. $form_state->setRedirect('entity.heartbeat_stream.canonical', ['heartbeat_stream' => $entity->id()]);
  103. }
  104. }