ActionFormBase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Drupal\action;
  3. use Drupal\Core\Entity\EntityForm;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Plugin\PluginFormInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9. * Provides a base form for action forms.
  10. */
  11. abstract class ActionFormBase extends EntityForm {
  12. /**
  13. * The action storage.
  14. *
  15. * @var \Drupal\Core\Entity\EntityStorageInterface
  16. */
  17. protected $storage;
  18. /**
  19. * The action entity.
  20. *
  21. * @var \Drupal\system\ActionConfigEntityInterface
  22. */
  23. protected $entity;
  24. /**
  25. * Constructs a new action form.
  26. *
  27. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  28. * The action storage.
  29. */
  30. public function __construct(EntityStorageInterface $storage) {
  31. $this->storage = $storage;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function create(ContainerInterface $container) {
  37. return new static(
  38. $container->get('entity.manager')->getStorage('action')
  39. );
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function form(array $form, FormStateInterface $form_state) {
  45. $form['label'] = [
  46. '#type' => 'textfield',
  47. '#title' => $this->t('Label'),
  48. '#default_value' => $this->entity->label(),
  49. '#maxlength' => '255',
  50. '#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
  51. ];
  52. $form['id'] = [
  53. '#type' => 'machine_name',
  54. '#default_value' => $this->entity->id(),
  55. '#disabled' => !$this->entity->isNew(),
  56. '#maxlength' => 64,
  57. '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
  58. '#machine_name' => [
  59. 'exists' => [$this, 'exists'],
  60. ],
  61. ];
  62. $form['plugin'] = [
  63. '#type' => 'value',
  64. '#value' => $this->entity->get('plugin'),
  65. ];
  66. $form['type'] = [
  67. '#type' => 'value',
  68. '#value' => $this->entity->getType(),
  69. ];
  70. if ($plugin = $this->getPlugin()) {
  71. $form += $plugin->buildConfigurationForm($form, $form_state);
  72. }
  73. return parent::form($form, $form_state);
  74. }
  75. /**
  76. * Determines if the action already exists.
  77. *
  78. * @param string $id
  79. * The action ID.
  80. *
  81. * @return bool
  82. * TRUE if the action exists, FALSE otherwise.
  83. */
  84. public function exists($id) {
  85. $action = $this->storage->load($id);
  86. return !empty($action);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. protected function actions(array $form, FormStateInterface $form_state) {
  92. $actions = parent::actions($form, $form_state);
  93. unset($actions['delete']);
  94. return $actions;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function validateForm(array &$form, FormStateInterface $form_state) {
  100. parent::validateForm($form, $form_state);
  101. if ($plugin = $this->getPlugin()) {
  102. $plugin->validateConfigurationForm($form, $form_state);
  103. }
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function submitForm(array &$form, FormStateInterface $form_state) {
  109. parent::submitForm($form, $form_state);
  110. if ($plugin = $this->getPlugin()) {
  111. $plugin->submitConfigurationForm($form, $form_state);
  112. }
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function save(array $form, FormStateInterface $form_state) {
  118. $this->entity->save();
  119. $this->messenger()->addStatus($this->t('The action has been successfully saved.'));
  120. $form_state->setRedirect('entity.action.collection');
  121. }
  122. /**
  123. * Gets the action plugin while ensuring it implements configuration form.
  124. *
  125. * @return \Drupal\Core\Action\ActionInterface|\Drupal\Core\Plugin\PluginFormInterface|null
  126. * The action plugin, or NULL if it does not implement configuration forms.
  127. */
  128. protected function getPlugin() {
  129. if ($this->entity->getPlugin() instanceof PluginFormInterface) {
  130. return $this->entity->getPlugin();
  131. }
  132. return NULL;
  133. }
  134. }