abstract class ActionFormControllerBase

Provides a base form controller for action forms.

Hierarchy

Expanded class hierarchy of ActionFormControllerBase

File

drupal/core/modules/action/lib/Drupal/action/ActionFormControllerBase.php, line 19
Contains Drupal\action\ActionEditFormController.

Namespace

Drupal\action
View source
abstract class ActionFormControllerBase extends EntityFormController implements EntityControllerInterface {

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

  /**
   * The action storage controller.
   *
   * @var \Drupal\Core\Entity\EntityStorageControllerInterface
   */
  protected $storageController;

  /**
   * Constructs a new action form.
   *
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller
   *   The action storage controller.
   */
  public function __construct(EntityStorageControllerInterface $storage_controller) {
    parent::__construct();
    $this->storageController = $storage_controller;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
    return new static($container
      ->get('plugin.manager.entity')
      ->getStorageController($entity_type));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $this->plugin = $this->entity
      ->getPlugin();
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, array &$form_state) {
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#default_value' => $this->entity
        ->label(),
      '#maxlength' => '255',
      '#description' => t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#title' => t('Machine name'),
      '#default_value' => $this->entity
        ->id(),
      '#disabled' => !$this->entity
        ->isNew(),
      '#maxlength' => 64,
      '#description' => t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
      '#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(),
    );
    if ($this->plugin instanceof ConfigurableActionInterface) {
      $form += $this->plugin
        ->form($form, $form_state);
    }
    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) {
    $actions = $this->storageController
      ->load(array(
      $id,
    ));
    return isset($actions[$id]);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, array &$form_state) {
    $actions = parent::actions($form, $form_state);
    unset($actions['delete']);
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function validate(array $form, array &$form_state) {
    parent::validate($form, $form_state);
    if ($this->plugin instanceof ConfigurableActionInterface) {
      $this->plugin
        ->validate($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submit(array $form, array &$form_state) {
    parent::submit($form, $form_state);
    if ($this->plugin instanceof ConfigurableActionInterface) {
      $this->plugin
        ->submit($form, $form_state);
    }
    return $this->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, array &$form_state) {
    $this->entity
      ->save();
    drupal_set_message(t('The action has been successfully saved.'));
    $form_state['redirect'] = 'admin/config/system/actions';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionFormControllerBase::$plugin protected property The action plugin being configured.
ActionFormControllerBase::$storageController protected property The action storage controller.
ActionFormControllerBase::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityFormController::actions
ActionFormControllerBase::buildForm public function Form constructor. Overrides EntityFormController::buildForm 1
ActionFormControllerBase::createInstance public static function Instantiates a new instance of this entity controller. Overrides EntityControllerInterface::createInstance 1
ActionFormControllerBase::exists public function Determines if the action already exists.
ActionFormControllerBase::form public function Returns the actual form array to be built. Overrides EntityFormController::form
ActionFormControllerBase::save public function Form submission handler for the 'save' action. Overrides EntityFormController::save
ActionFormControllerBase::submit public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::submit(). Overrides EntityFormController::submit
ActionFormControllerBase::validate public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::validate(). Overrides EntityFormController::validate
ActionFormControllerBase::__construct public function Constructs a new action form. Overrides EntityFormController::__construct 1
EntityFormController::$entity protected property The entity being used by this form.
EntityFormController::$operation protected property The name of the current operation.
EntityFormController::actionsElement protected function Returns the action form element for the current entity form.
EntityFormController::buildEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::buildEntity(). Overrides EntityFormControllerInterface::buildEntity 2
EntityFormController::delete public function Form submission handler for the 'delete' action. 12
EntityFormController::getBaseFormID public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormID
EntityFormController::getEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getEntity(). Overrides EntityFormControllerInterface::getEntity
EntityFormController::getFormDisplay public function Returns the form display. Overrides EntityFormControllerInterface::getFormDisplay
EntityFormController::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
EntityFormController::getFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getFormLangcode(). Overrides EntityFormControllerInterface::getFormLangcode
EntityFormController::getOperation public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getOperation(). Overrides EntityFormControllerInterface::getOperation
EntityFormController::init protected function Initialize the form state and the entity before the first form build. 1
EntityFormController::isDefaultFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::isDefaultFormLangcode(). Overrides EntityFormControllerInterface::isDefaultFormLangcode
EntityFormController::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityFormController::setEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::setEntity(). Overrides EntityFormControllerInterface::setEntity
EntityFormController::setFormDisplay public function Sets the form display. Overrides EntityFormControllerInterface::setFormDisplay
EntityFormController::setOperation public function Sets the operation for this form.
EntityFormController::submitEntityLanguage protected function Handle possible entity language changes. 1
EntityFormController::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityFormController::updateFormLangcode protected function Updates the form language to reflect any change to the entity language.
EntityFormController::validateForm public function Form validation handler. Overrides FormInterface::validateForm