class PictureMappingFormController

Form controller for the picture edit/add forms.

Hierarchy

Expanded class hierarchy of PictureMappingFormController

File

drupal/core/modules/picture/lib/Drupal/picture/PictureMappingFormController.php, line 15
Contains Drupal\picture\PictureFormController.

Namespace

Drupal\picture
View source
class PictureMappingFormController extends EntityFormController {

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   *
   * @param array $form
   *   A nested array form elements comprising the form.
   * @param array $form_state
   *   An associative array containing the current state of the form.
   * @param \Drupal\picture\PictureMappingInterface $picture_mapping
   *   The entity being edited.
   *
   * @return array
   *   The array containing the complete form.
   */
  public function form(array $form, array &$form_state) {
    if ($this->operation == 'duplicate') {
      drupal_set_title(t('<em>Duplicate picture mapping</em> @label', array(
        '@label' => $this->entity
          ->label(),
      )), PASS_THROUGH);
      $this->entity = $this->entity
        ->createDuplicate();
    }
    if ($this->operation == 'edit') {
      drupal_set_title(t('<em>Edit picture mapping</em> @label', array(
        '@label' => $this->entity
          ->label(),
      )), PASS_THROUGH);
    }
    $picture_mapping = $this->entity;
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#maxlength' => 255,
      '#default_value' => $picture_mapping
        ->label(),
      '#description' => t("Example: 'Hero image' or 'Author image'."),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $picture_mapping
        ->id(),
      '#machine_name' => array(
        'exists' => 'picture_mapping_load',
        'source' => array(
          'label',
        ),
      ),
      '#disabled' => (bool) $picture_mapping
        ->id() && $this->operation != 'duplicate',
    );
    if ((bool) $picture_mapping
      ->id() && $this->operation != 'duplicate') {
      $description = t('Select a breakpoint group from the enabled themes.') . ' ' . t("Warning: if you change the breakpoint group you lose all your selected mappings.");
    }
    else {
      $description = t('Select a breakpoint group from the enabled themes.');
    }
    $form['breakpointGroup'] = array(
      '#type' => 'select',
      '#title' => t('Breakpoint group'),
      '#default_value' => !empty($picture_mapping->breakpointGroup) ? $picture_mapping->breakpointGroup
        ->id() : '',
      '#options' => breakpoint_group_select_options(),
      '#required' => TRUE,
      '#description' => $description,
    );
    $image_styles = image_style_options(TRUE);
    foreach ($picture_mapping->mappings as $breakpoint_id => $mapping) {
      foreach ($mapping as $multiplier => $image_style) {
        $label = $multiplier . ' ' . $picture_mapping->breakpointGroup->breakpoints[$breakpoint_id]->name . ' [' . $picture_mapping->breakpointGroup->breakpoints[$breakpoint_id]->mediaQuery . ']';
        $form['mappings'][$breakpoint_id][$multiplier] = array(
          '#type' => 'select',
          '#title' => check_plain($label),
          '#options' => $image_styles,
          '#default_value' => $image_style,
          '#description' => t('Select an image style for this breakpoint.'),
        );
      }
    }
    $form['#tree'] = TRUE;
    return parent::form($form, $form_state, $picture_mapping);
  }

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

    // Only includes a Save action for the entity, no direct Delete button.
    return array(
      'submit' => array(
        '#value' => t('Save'),
        '#validate' => array(
          array(
            $this,
            'validate',
          ),
        ),
        '#submit' => array(
          array(
            $this,
            'submit',
          ),
          array(
            $this,
            'save',
          ),
        ),
      ),
    );
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::validate().
   */
  public function validate(array $form, array &$form_state) {
    $picture_mapping = $this->entity;

    // Only validate on edit.
    if (isset($form_state['values']['mappings'])) {
      $picture_mapping->mappings = $form_state['values']['mappings'];

      // Check if another breakpoint group is selected.
      if ($form_state['values']['breakpointGroup'] != $form_state['complete_form']['breakpointGroup']['#default_value']) {

        // Remove the mappings.
        unset($form_state['values']['mappings']);
      }
      elseif (!$picture_mapping
        ->isNew() && !$picture_mapping
        ->hasMappings()) {
        form_set_error('mappings', t('Please select at least one mapping.'));
      }
    }
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, array &$form_state) {
    $picture_mapping = $this->entity;
    $picture_mapping
      ->save();
    watchdog('picture', 'Picture mapping @label saved.', array(
      '@label' => $picture_mapping
        ->label(),
    ), WATCHDOG_NOTICE);
    drupal_set_message(t('Picture mapping %label saved.', array(
      '%label' => $picture_mapping
        ->label(),
    )));

    // Redirect to edit form after creating a new mapping or after selecting
    // another breakpoint group.
    if (!$picture_mapping
      ->hasMappings()) {
      $uri = $picture_mapping
        ->uri();
      $form_state['redirect'] = $uri['path'] . '/edit';
    }
    else {
      $form_state['redirect'] = 'admin/config/media/picturemapping';
    }
  }

}

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::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::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
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::submit public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::submit(). Overrides EntityFormControllerInterface::submit 13
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
EntityFormController::__construct public function Constructs an EntityFormController object. 5
PictureMappingFormController::actions protected function Overrides Drupal\Core\Entity\EntityFormController::actions(). Overrides EntityFormController::actions
PictureMappingFormController::form public function Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityFormController::form
PictureMappingFormController::save public function Overrides Drupal\Core\Entity\EntityFormController::save(). Overrides EntityFormController::save
PictureMappingFormController::validate public function Overrides Drupal\Core\Entity\EntityFormController::validate(). Overrides EntityFormController::validate