summaryrefslogtreecommitdiffstats
path: root/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingForm.php
blob: 1b4bb4a89d755788bba58a2e8f333590c4a0309e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

/**
 * @file
 * Contains Drupal\responsive_image\ResponsiveImageForm.
 */

namespace Drupal\responsive_image;

use Drupal\Core\Entity\EntityForm;

/**
 * Form controller for the responsive image edit/add forms.
 */
class ResponsiveImageMappingForm extends EntityForm {

  /**
   * Overrides Drupal\Core\Entity\EntityForm::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\responsive_image\ResponsiveImageMappingInterface $responsive_image_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') {
      $form['#title'] = $this->t('<em>Duplicate responsive image mapping</em> @label', array('@label' => $this->entity->label()));
      $this->entity = $this->entity->createDuplicate();
    }
    if ($this->operation == 'edit') {
      $form['#title'] = $this->t('<em>Edit responsive image mapping</em> @label', array('@label' => $this->entity->label()));
    }

    /** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
    $responsive_image_mapping = $this->entity;
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $responsive_image_mapping->label(),
      '#description' => $this->t("Example: 'Hero image' or 'Author image'."),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $responsive_image_mapping->id(),
      '#machine_name' => array(
        'exists' => 'responsive_image_mapping_load',
        'source' => array('label'),
      ),
      '#disabled' => (bool) $responsive_image_mapping->id() && $this->operation != 'duplicate',
    );

    if ((bool) $responsive_image_mapping->id() && $this->operation != 'duplicate') {
      $description = $this->t('Select a breakpoint group from the enabled themes.') . ' ' . $this->t("Warning: if you change the breakpoint group you lose all your selected mappings.");
    }
    else {
      $description = $this->t('Select a breakpoint group from the enabled themes.');
    }
    $form['breakpointGroup'] = array(
      '#type' => 'select',
      '#title' => $this->t('Breakpoint group'),
      '#default_value' => ($responsive_image_mapping->getBreakpointGroup() != '') ? $responsive_image_mapping->getBreakpointGroup()->id() : '',
      '#options' => breakpoint_group_select_options(),
      '#required' => TRUE,
      '#description' => $description,
    );

    $image_styles = image_style_options(TRUE);
    foreach ($responsive_image_mapping->getMappings() as $breakpoint_id => $mapping) {
      foreach ($mapping as $multiplier => $image_style) {
        $breakpoint = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_id);
        $label = $multiplier . ' ' . $breakpoint->name . ' [' . $breakpoint->mediaQuery . ']';
        $form['mappings'][$breakpoint_id][$multiplier] = array(
          '#type' => 'select',
          '#title' => check_plain($label),
          '#options' => $image_styles,
          '#default_value' => $image_style,
          '#description' => $this->t('Select an image style for this breakpoint.'),
        );
      }
    }

    $form['#tree'] = TRUE;

    return parent::form($form, $form_state, $responsive_image_mapping);
  }

  /**
   * {@inheritdoc}
   */
  public function validate(array $form, array &$form_state) {
    /** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
    $responsive_image_mapping = $this->entity;

    // Only validate on edit.
    if (isset($form_state['values']['mappings'])) {
      $responsive_image_mapping->setMappings($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']);
      }
      // Make sure at least one mapping is defined.
      elseif (!$responsive_image_mapping->isNew() && !$responsive_image_mapping->hasMappings()) {
        $this->setFormError('mappings', $form_state, $this->t('Please select at least one mapping.'));
      }
    }
  }

  /**
   * Overrides Drupal\Core\Entity\EntityForm::save().
   */
  public function save(array $form, array &$form_state) {
    /** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
    $responsive_image_mapping = $this->entity;
    $responsive_image_mapping->save();

    watchdog('responsive_image', 'Responsive image mapping @label saved.', array('@label' => $responsive_image_mapping->label()), WATCHDOG_NOTICE);
    drupal_set_message($this->t('Responsive image mapping %label saved.', array('%label' => $responsive_image_mapping->label())));

    // Redirect to edit form after creating a new mapping or after selecting
    // another breakpoint group.
    if (!$responsive_image_mapping->hasMappings()) {
      $form_state['redirect_route'] = array(
        'route_name' => 'responsive_image.mapping_page_edit',
        'route_parameters' => array(
          'responsive_image_mapping' => $responsive_image_mapping->id(),
        ),
      );
    }
    else {
      $form_state['redirect_route']['route_name'] = 'responsive_image.mapping_page';
    }
  }

}