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

/**
 * @file
5
 * Contains Drupal\action\ActionEditForm.
6 7 8 9
 */

namespace Drupal\action;

10
use Drupal\Core\Entity\EntityForm;
11
use Drupal\Core\Entity\EntityStorageInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Plugin\PluginFormInterface;
14 15 16
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
17
 * Provides a base form for action forms.
18
 */
19
abstract class ActionFormBase extends EntityForm {
20 21 22 23 24 25 26 27 28

  /**
   * The action plugin being configured.
   *
   * @var \Drupal\Core\Action\ActionInterface
   */
  protected $plugin;

  /**
29
   * The action storage.
30
   *
31
   * @var \Drupal\Core\Entity\EntityStorageInterface
32
   */
33
  protected $storage;
34 35 36 37

  /**
   * Constructs a new action form.
   *
38 39
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The action storage.
40
   */
41 42
  public function __construct(EntityStorageInterface $storage) {
    $this->storage = $storage;
43 44 45 46 47
  }

  /**
   * {@inheritdoc}
   */
48
  public static function create(ContainerInterface $container) {
49
    return new static(
50
      $container->get('entity.manager')->getStorage('action')
51 52 53 54 55 56
    );
  }

  /**
   * {@inheritdoc}
   */
57
  public function buildForm(array $form, FormStateInterface $form_state) {
58 59 60 61 62 63 64
    $this->plugin = $this->entity->getPlugin();
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
65
  public function form(array $form, FormStateInterface $form_state) {
66 67
    $form['label'] = array(
      '#type' => 'textfield',
68
      '#title' => $this->t('Label'),
69 70
      '#default_value' => $this->entity->label(),
      '#maxlength' => '255',
71
      '#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
72 73 74 75 76 77 78
    );

    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $this->entity->id(),
      '#disabled' => !$this->entity->isNew(),
      '#maxlength' => 64,
79
      '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
80 81 82 83 84 85 86 87 88 89 90 91 92
      '#machine_name' => array(
        'exists' => array($this, 'exists'),
      ),
    );
    $form['plugin'] = array(
      '#type' => 'value',
      '#value' => $this->entity->get('plugin'),
    );
    $form['type'] = array(
      '#type' => 'value',
      '#value' => $this->entity->getType(),
    );

93 94
    if ($this->plugin instanceof PluginFormInterface) {
      $form += $this->plugin->buildConfigurationForm($form, $form_state);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    }

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

  /**
   * Determines if the action already exists.
   *
   * @param string $id
   *   The action ID
   *
   * @return bool
   *   TRUE if the action exists, FALSE otherwise.
   */
  public function exists($id) {
110
    $action = $this->storage->load($id);
111
    return !empty($action);
112 113 114 115 116
  }

  /**
   * {@inheritdoc}
   */
117
  protected function actions(array $form, FormStateInterface $form_state) {
118 119 120 121 122 123 124 125
    $actions = parent::actions($form, $form_state);
    unset($actions['delete']);
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
126
  public function validate(array $form, FormStateInterface $form_state) {
127 128
    parent::validate($form, $form_state);

129 130
    if ($this->plugin instanceof PluginFormInterface) {
      $this->plugin->validateConfigurationForm($form, $form_state);
131 132 133 134 135 136
    }
  }

  /**
   * {@inheritdoc}
   */
137 138
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
139

140 141
    if ($this->plugin instanceof PluginFormInterface) {
      $this->plugin->submitConfigurationForm($form, $form_state);
142 143 144 145 146 147
    }
  }

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

152
    $form_state->setRedirect('entity.action.collection');
153 154 155
  }

}