class EntityTestFormController

Form controller for the test entity edit forms.

Hierarchy

Expanded class hierarchy of EntityTestFormController

File

drupal/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php, line 15
Definition of Drupal\entity_test\EntityTestFormController.

Namespace

Drupal\entity_test
View source
class EntityTestFormController extends EntityFormControllerNG {

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, array &$form_state) {
    $form = parent::form($form, $form_state);
    $entity = $this->entity;
    $langcode = $this
      ->getFormLangcode($form_state);
    $translation = $entity
      ->getTranslation($langcode);
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $translation->name->value,
      '#size' => 60,
      '#maxlength' => 128,
      '#required' => TRUE,
      '#weight' => -10,
    );
    $form['user_id'] = array(
      '#type' => 'textfield',
      '#title' => 'UID',
      '#default_value' => $translation->user_id->target_id,
      '#size' => 60,
      '#maxlength' => 128,
      '#required' => TRUE,
      '#weight' => -10,
    );
    $form['langcode'] = array(
      '#title' => t('Language'),
      '#type' => 'language_select',
      '#default_value' => $entity
        ->language()->langcode,
      '#languages' => Language::STATE_ALL,
    );

    // @todo: Is there a better way to check if an entity type is revisionable?
    $entity_info = $entity
      ->entityInfo();
    if (!empty($entity_info['entity_keys']['revision']) && !$entity
      ->isNew()) {
      $form['revision'] = array(
        '#type' => 'checkbox',
        '#title' => t('Create new revision'),
        '#default_value' => $entity
          ->isNewRevision(),
      );
    }
    return $form;
  }

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::submit().
   */
  public function submit(array $form, array &$form_state) {

    // Build the entity object from the submitted values.
    $entity = parent::submit($form, $form_state);

    // Save as a new revision if requested to do so.
    if (!empty($form_state['values']['revision'])) {
      $entity
        ->setNewRevision();
    }
    return $entity;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, array &$form_state) {
    $entity = $this->entity;
    $is_new = $entity
      ->isNew();
    $entity
      ->save();
    if ($is_new) {
      $message = t('%entity_type @id has been created.', array(
        '@id' => $entity
          ->id(),
        '%entity_type' => $entity
          ->entityType(),
      ));
    }
    else {
      $message = t('%entity_type @id has been updated.', array(
        '@id' => $entity
          ->id(),
        '%entity_type' => $entity
          ->entityType(),
      ));
    }
    drupal_set_message($message);
    if ($entity
      ->id()) {
      $form_state['redirect'] = $entity
        ->entityType() . '/manage/' . $entity
        ->id() . '/edit';
    }
    else {

      // Error on save.
      drupal_set_message(t('The entity could not be saved.'), 'error');
      $form_state['rebuild'] = TRUE;
    }
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::delete().
   */
  public function delete(array $form, array &$form_state) {
    $entity = $this->entity;
    $entity
      ->delete();
    drupal_set_message(t('%entity_type @id has been deleted.', array(
      '@id' => $entity
        ->id(),
      '%entity_type' => $entity
        ->entityType(),
    )));
    $form_state['redirect'] = '<front>';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityFormController::$entity protected property The entity being used by this form.
EntityFormController::$operation protected property The name of the current operation.
EntityFormController::actions protected function Returns an array of supported actions for the current entity form. 17
EntityFormController::actionsElement protected function Returns the action form element for the current entity form.
EntityFormController::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
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::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
EntityFormController::__construct public function Constructs an EntityFormController object. 5
EntityFormControllerNG::buildEntity public function Overrides EntityFormController::buildEntity(). Overrides EntityFormController::buildEntity 2
EntityFormControllerNG::submitEntityLanguage protected function Overrides EntityFormController::submitEntityLanguage(). Overrides EntityFormController::submitEntityLanguage
EntityFormControllerNG::validate public function Overrides EntityFormController::validate(). Overrides EntityFormController::validate 3
EntityTestFormController::delete public function Overrides Drupal\Core\Entity\EntityFormController::delete(). Overrides EntityFormController::delete
EntityTestFormController::form public function Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityFormControllerNG::form
EntityTestFormController::save public function Overrides Drupal\Core\Entity\EntityFormController::save(). Overrides EntityFormController::save
EntityTestFormController::submit public function Overrides \Drupal\Core\Entity\EntityFormController::submit(). Overrides EntityFormController::submit