ActionFormBase.php 3.89 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\action;

5
use Drupal\Core\Entity\EntityForm;
6
use Drupal\Core\Entity\EntityStorageInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Plugin\PluginFormInterface;
9 10 11
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
12
 * Provides a base form for action forms.
13
 */
14
abstract class ActionFormBase extends EntityForm {
15 16

  /**
17
   * The action storage.
18
   *
19
   * @var \Drupal\Core\Entity\EntityStorageInterface
20
   */
21
  protected $storage;
22 23

  /**
24
   * The action entity.
25
   *
26
   * @var \Drupal\system\ActionConfigEntityInterface
27
   */
28
  protected $entity;
29 30 31 32

  /**
   * Constructs a new action form.
   *
33 34
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The action storage.
35
   */
36 37
  public function __construct(EntityStorageInterface $storage) {
    $this->storage = $storage;
38 39 40 41 42
  }

  /**
   * {@inheritdoc}
   */
43
  public static function create(ContainerInterface $container) {
44
    return new static(
45
      $container->get('entity.manager')->getStorage('action')
46 47 48 49 50 51
    );
  }

  /**
   * {@inheritdoc}
   */
52
  public function form(array $form, FormStateInterface $form_state) {
53
    $form['label'] = [
54
      '#type' => 'textfield',
55
      '#title' => $this->t('Label'),
56 57
      '#default_value' => $this->entity->label(),
      '#maxlength' => '255',
58
      '#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
59
    ];
60

61
    $form['id'] = [
62 63 64 65
      '#type' => 'machine_name',
      '#default_value' => $this->entity->id(),
      '#disabled' => !$this->entity->isNew(),
      '#maxlength' => 64,
66
      '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
67 68 69 70 71
      '#machine_name' => [
        'exists' => [$this, 'exists'],
      ],
    ];
    $form['plugin'] = [
72 73
      '#type' => 'value',
      '#value' => $this->entity->get('plugin'),
74 75
    ];
    $form['type'] = [
76 77
      '#type' => 'value',
      '#value' => $this->entity->getType(),
78
    ];
79

80 81
    if ($plugin = $this->getPlugin()) {
      $form += $plugin->buildConfigurationForm($form, $form_state);
82 83 84 85 86 87 88 89 90
    }

    return parent::form($form, $form_state);
  }

  /**
   * Determines if the action already exists.
   *
   * @param string $id
91
   *   The action ID.
92 93 94 95 96
   *
   * @return bool
   *   TRUE if the action exists, FALSE otherwise.
   */
  public function exists($id) {
97
    $action = $this->storage->load($id);
98
    return !empty($action);
99 100 101 102 103
  }

  /**
   * {@inheritdoc}
   */
104
  protected function actions(array $form, FormStateInterface $form_state) {
105 106 107 108 109 110 111 112
    $actions = parent::actions($form, $form_state);
    unset($actions['delete']);
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
113 114
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
115 116
    if ($plugin = $this->getPlugin()) {
      $plugin->validateConfigurationForm($form, $form_state);
117 118 119 120 121 122
    }
  }

  /**
   * {@inheritdoc}
   */
123 124
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
125 126
    if ($plugin = $this->getPlugin()) {
      $plugin->submitConfigurationForm($form, $form_state);
127 128 129 130 131 132
    }
  }

  /**
   * {@inheritdoc}
   */
133
  public function save(array $form, FormStateInterface $form_state) {
134
    $this->entity->save();
135
    drupal_set_message($this->t('The action has been successfully saved.'));
136

137
    $form_state->setRedirect('entity.action.collection');
138 139
  }

140 141 142 143 144 145 146 147 148 149 150 151 152
  /**
   * Gets the action plugin while ensuring it implements configuration form.
   *
   * @return \Drupal\Core\Action\ActionInterface|\Drupal\Core\Plugin\PluginFormInterface|null
   *   The action plugin, or NULL if it does not implement configuration forms.
   */
  protected function getPlugin() {
    if ($this->entity->getPlugin() instanceof PluginFormInterface) {
      return $this->entity->getPlugin();
    }
    return NULL;
  }

153
}