public function PictureMappingFormController::form

Overrides Drupal\Core\Entity\EntityFormController::form().

Parameters

array $form: A nested array form elements comprising the form.

array $form_state: An associative array containing the current state of the form.

\Drupal\picture\PictureMappingInterface $picture_mapping: The entity being edited.

Return value

array The array containing the complete form.

Overrides EntityFormController::form

File

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

Class

PictureMappingFormController
Form controller for the picture edit/add forms.

Namespace

Drupal\picture

Code

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);
}