HeartbeatForm.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Drupal\heartbeat\Form;
  3. use Drupal\Core\Entity\ContentEntityForm;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\heartbeat\Entity;
  6. /**
  7. * Form controller for Heartbeat edit forms.
  8. *
  9. * @ingroup heartbeat
  10. */
  11. class HeartbeatForm extends ContentEntityForm {
  12. //TODO add dependency injection
  13. protected $nodeManager;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function buildForm(array $form, FormStateInterface $form_state) {
  18. $this->nodeManager = \Drupal::service('entity_type.manager')->getStorage('node');
  19. /* @var $entity \Drupal\heartbeat\Entity\Heartbeat */
  20. $form = parent::buildForm($form, $form_state);
  21. $entity = &$this->entity;
  22. if ($entity->isNew()) {
  23. $form['new_revision'] = array(
  24. '#type' => 'checkbox',
  25. '#title' => $this->t('Create new revision'),
  26. '#default_value' => FALSE,
  27. '#weight' => 10,
  28. );
  29. }
  30. $form['uid'] = array(
  31. '#type' => 'entity_autocomplete',
  32. '#target_type' => 'user',
  33. '#default_value' => $entity->getOwner()->isAnonymous() ? NULL : $entity->getOwner(),
  34. // A comment can be made anonymous by leaving this field empty therefore
  35. // there is no need to list them in the autocomplete.
  36. '#selection_settings' => ['include_anonymous' => FALSE],
  37. '#title' => $this->t('Authored by'),
  38. '#description' => $this->t('No cucks.')
  39. );
  40. $form['message'] = array(
  41. '#type' => 'textarea',
  42. '#description' => t('The Heartbeat message'),
  43. '#title' => 'Message',
  44. '#default' => $entity->getMessage()->getValue()[0]['value'],
  45. '#value' => $entity->getMessage()->getValue()[0]['value'],
  46. );
  47. $nodeId = $entity->getNid()->getValue()[0]['target_id'];
  48. $node = $this->nodeManager->load($nodeId);
  49. $form['nid'] = array(
  50. '#type' => 'entity_autocomplete',
  51. '#entity_type' => 'node',
  52. '#target_type' => 'node',
  53. '#selection_handler' => 'default',
  54. '#default_value' => $nodeId > 0 ? null : $this->nodeManager->load($nodeId),
  55. '#title' => 'Node',
  56. '#description' => t('The node referenced by this Heartbeat')
  57. );
  58. $form['status'] = array(
  59. '#type' => 'checkbox',
  60. '#title' => 'Status',
  61. '#description' => t('Published'),
  62. '#default_value' => $entity->isPublished(),
  63. );
  64. $entity = $this->entity;
  65. return $form;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function save(array $form, FormStateInterface $form_state) {
  71. $entity = &$this->entity;
  72. // Save as a new revision if requested to do so.
  73. if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) {
  74. $entity->setNewRevision();
  75. // If a new revision is created, save the current user as revision author.
  76. $entity->setRevisionCreationTime(REQUEST_TIME);
  77. $entity->setRevisionUserId(\Drupal::currentUser()->id());
  78. }
  79. else {
  80. $entity->setNewRevision(FALSE);
  81. }
  82. $status = parent::save($form, $form_state);
  83. switch ($status) {
  84. case SAVED_NEW:
  85. drupal_set_message($this->t('Created the %label Heartbeat.', [
  86. '%label' => $entity->label(),
  87. ]));
  88. break;
  89. default:
  90. drupal_set_message($this->t('Saved the %label Heartbeat.', [
  91. '%label' => $entity->label(),
  92. ]));
  93. }
  94. $form_state->setRedirect('entity.heartbeat.canonical', ['heartbeat' => $entity->id()]);
  95. }
  96. }