3 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 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Provides a base form for action forms.
14 abstract class ActionFormBase extends EntityForm {
17 * The action plugin being configured.
19 * @var \Drupal\Core\Action\ActionInterface
26 * @var \Drupal\Core\Entity\EntityStorageInterface
31 * Constructs a new action form.
33 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
36 public function __construct(EntityStorageInterface $storage) {
37 $this->storage = $storage;
43 public static function create(ContainerInterface $container) {
45 $container->get('entity.manager')->getStorage('action')
52 public function buildForm(array $form, FormStateInterface $form_state) {
53 $this->plugin = $this->entity->getPlugin();
54 return parent::buildForm($form, $form_state);
60 public function form(array $form, FormStateInterface $form_state) {
62 '#type' => 'textfield',
63 '#title' => $this->t('Label'),
64 '#default_value' => $this->entity->label(),
65 '#maxlength' => '255',
66 '#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
70 '#type' => 'machine_name',
71 '#default_value' => $this->entity->id(),
72 '#disabled' => !$this->entity->isNew(),
74 '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
76 'exists' => [$this, 'exists'],
81 '#value' => $this->entity->get('plugin'),
85 '#value' => $this->entity->getType(),
88 if ($this->plugin instanceof PluginFormInterface) {
89 $form += $this->plugin->buildConfigurationForm($form, $form_state);
92 return parent::form($form, $form_state);
96 * Determines if the action already exists.
102 * TRUE if the action exists, FALSE otherwise.
104 public function exists($id) {
105 $action = $this->storage->load($id);
106 return !empty($action);
112 protected function actions(array $form, FormStateInterface $form_state) {
113 $actions = parent::actions($form, $form_state);
114 unset($actions['delete']);
121 public function validateForm(array &$form, FormStateInterface $form_state) {
122 parent::validateForm($form, $form_state);
124 if ($this->plugin instanceof PluginFormInterface) {
125 $this->plugin->validateConfigurationForm($form, $form_state);
132 public function submitForm(array &$form, FormStateInterface $form_state) {
133 parent::submitForm($form, $form_state);
135 if ($this->plugin instanceof PluginFormInterface) {
136 $this->plugin->submitConfigurationForm($form, $form_state);
143 public function save(array $form, FormStateInterface $form_state) {
144 $this->entity->save();
145 drupal_set_message($this->t('The action has been successfully saved.'));
147 $form_state->setRedirect('entity.action.collection');