ConfigTestFormController.php

Contains Drupal\config_test\ConfigTestFormController.

Namespace

Drupal\config_test

File

drupal/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestFormController.php
View source
<?php

/**
 * @file
 * Contains Drupal\config_test\ConfigTestFormController.
 */
namespace Drupal\config_test;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormController;

/**
 * Form controller for the test config edit forms.
 */
class ConfigTestFormController extends EntityFormController {

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, array &$form_state, EntityInterface $entity) {
    $form = parent::form($form, $form_state, $entity);
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => 'Label',
      '#default_value' => $entity
        ->label(),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $entity
        ->id(),
      '#required' => TRUE,
      '#machine_name' => array(
        'exists' => 'config_test_load',
      ),
    );
    $form['style'] = array(
      '#type' => 'select',
      '#title' => 'Image style',
      '#options' => array(),
      '#default_value' => $entity
        ->get('style'),
      '#access' => FALSE,
    );
    if (module_exists('image')) {
      $form['style']['#access'] = TRUE;
      $form['style']['#options'] = image_style_options();
    }
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Save',
    );
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => 'Delete',
    );
    return $form;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, array &$form_state) {
    $entity = $this
      ->getEntity($form_state);
    $status = $entity
      ->save();
    if ($status === SAVED_UPDATED) {
      drupal_set_message(format_string('%label configuration has been updated.', array(
        '%label' => $entity
          ->label(),
      )));
    }
    else {
      drupal_set_message(format_string('%label configuration has been created.', array(
        '%label' => $entity
          ->label(),
      )));
    }
    $form_state['redirect'] = 'admin/structure/config_test';
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::delete().
   */
  public function delete(array $form, array &$form_state) {
    $entity = $this
      ->getEntity($form_state);
    $form_state['redirect'] = 'admin/structure/config_test/manage/' . $entity
      ->id() . '/delete';
  }

}

Classes

Namesort descending Description
ConfigTestFormController Form controller for the test config edit forms.