ResponsiveImageStyleForm.php 6.99 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains Drupal\responsive_image\ResponsiveImageStyleForm.
6 7
 */

8
namespace Drupal\responsive_image;
9

10
use Drupal\breakpoint\BreakpointManagerInterface;
11
use Drupal\Core\Entity\EntityForm;
12
use Drupal\Core\Form\FormStateInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14 15

/**
16
 * Form controller for the responsive image edit/add forms.
17
 */
18
class ResponsiveImageStyleForm extends EntityForm {
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  /**
   * The breakpoint manager.
   *
   * @var \Drupal\breakpoint\BreakpointManagerInterface
   */
  protected $breakpointManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('breakpoint.manager')
    );
  }

  /**
37
   * Constructs the responsive image style form.
38 39 40 41 42 43 44 45
   *
   * @param \Drupal\breakpoint\BreakpointManagerInterface $breakpoint_manager
   *   The breakpoint manager.
   */
  public function __construct(BreakpointManagerInterface $breakpoint_manager) {
    $this->breakpointManager = $breakpoint_manager;
  }

46
  /**
47
   * Overrides Drupal\Core\Entity\EntityForm::form().
48 49 50
   *
   * @param array $form
   *   A nested array form elements comprising the form.
51 52
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
53 54 55 56
   *
   * @return array
   *   The array containing the complete form.
   */
57
  public function form(array $form, FormStateInterface $form_state) {
58
    if ($this->operation == 'duplicate') {
59
      $form['#title'] = $this->t('<em>Duplicate responsive image style</em> @label', array('@label' => $this->entity->label()));
60 61 62
      $this->entity = $this->entity->createDuplicate();
    }
    if ($this->operation == 'edit') {
63
      $form['#title'] = $this->t('<em>Edit responsive image style</em> @label', array('@label' => $this->entity->label()));
64 65
    }

66 67
    /** @var \Drupal\responsive_image\ResponsiveImageStyleInterface $responsive_image_style */
    $responsive_image_style = $this->entity;
68 69
    $form['label'] = array(
      '#type' => 'textfield',
70
      '#title' => $this->t('Label'),
71
      '#maxlength' => 255,
72
      '#default_value' => $responsive_image_style->label(),
73
      '#description' => $this->t("Example: 'Hero image' or 'Author image'."),
74 75 76 77
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
78
      '#default_value' => $responsive_image_style->id(),
79
      '#machine_name' => array(
80
        'exists' => '\Drupal\responsive_image\Entity\ResponsiveImageStyle::load',
81 82
        'source' => array('label'),
      ),
83
      '#disabled' => (bool) $responsive_image_style->id() && $this->operation != 'duplicate',
84 85
    );

86 87
    if ((bool) $responsive_image_style->id() && $this->operation != 'duplicate') {
      $description = $this->t('Select a breakpoint group from the installed themes.') . ' ' . $this->t("Warning: if you change the breakpoint group you lose all your selected image style mappings.");
88 89
    }
    else {
90
      $description = $this->t('Select a breakpoint group from the installed themes.');
91
    }
92
    $form['breakpoint_group'] = array(
93
      '#type' => 'select',
94
      '#title' => $this->t('Breakpoint group'),
95
      '#default_value' => $responsive_image_style->getBreakpointGroup(),
96
      '#options' => $this->breakpointManager->getGroups(),
97 98 99 100 101
      '#required' => TRUE,
      '#description' => $description,
    );

    $image_styles = image_style_options(TRUE);
102
    $image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -');
103
    $breakpoints = $this->breakpointManager->getBreakpointsByGroup($responsive_image_style->getBreakpointGroup());
104 105 106
    foreach ($breakpoints as $breakpoint_id => $breakpoint) {
      foreach ($breakpoint->getMultipliers() as $multiplier) {
        $label = $multiplier . ' ' . $breakpoint->getLabel() . ' [' . $breakpoint->getMediaQuery() . ']';
107 108 109 110 111 112 113 114 115 116 117 118
        $form['keyed_styles'][$breakpoint_id][$multiplier] = array(
          '#type' => 'container',
        );
        $image_style_mapping = $responsive_image_style->getImageStyleMapping($breakpoint_id, $multiplier);
        // @todo The image_mapping_type is only temporarily hardcoded, until
        // support for the other responsive image mapping type ('sizes') is
        // added in https://www.drupal.org/node/2334387.
        $form['keyed_styles'][$breakpoint_id][$multiplier]['image_mapping_type'] = array(
          '#type' => 'value',
          '#value' => 'image_style',
        );
        $form['keyed_styles'][$breakpoint_id][$multiplier]['image_mapping'] = array(
119
          '#type' => 'select',
120
          '#title' => $label,
121
          '#options' => $image_styles,
122
          '#default_value' => isset($image_style_mapping['image_mapping']) ? $image_style_mapping['image_mapping'] : array(),
123
          '#description' => $this->t('Select an image style for this breakpoint.'),
124 125 126 127 128 129
        );
      }
    }

    $form['#tree'] = TRUE;

130
    return parent::form($form, $form_state, $responsive_image_style);
131 132 133
  }

  /**
134
   * {@inheritdoc}
135
   */
136
  public function validate(array $form, FormStateInterface $form_state) {
137
    // Only validate on edit.
138
    if ($form_state->hasValue('keyed_styles')) {
139
      // Check if another breakpoint group is selected.
140 141 142
      if ($form_state->getValue('breakpoint_group') != $form_state->getCompleteForm()['breakpoint_group']['#default_value']) {
        // Remove the image style mappings since the breakpoint ID has changed.
        $form_state->unsetValue('keyed_styles');
143
      }
144 145 146
      // @todo Filter 'sizes_image_styles' to a normal array in
      // https://www.drupal.org/node/2334387. For an example see
      // \Drupal\Core\Block\BlockBase::validateConfigurationForm().
147 148 149 150
    }
  }

  /**
151
   * {@inheritdoc}
152
   */
153
  public function save(array $form, FormStateInterface $form_state) {
154 155
    /** @var \Drupal\responsive_image\ResponsiveImageStyleInterface $responsive_image_style */
    $responsive_image_style = $this->entity;
156
    // Remove all the existing mappings and replace with submitted values.
157 158 159 160 161
    $responsive_image_style->removeImageStyleMappings();
    if ($form_state->hasValue('keyed_styles')) {
      foreach ($form_state->getValue('keyed_styles') as $breakpoint_id => $multipliers) {
        foreach ($multipliers as $multiplier => $image_style_mapping) {
          $responsive_image_style->addImageStyleMapping($breakpoint_id, $multiplier, $image_style_mapping);
162 163 164
        }
      }
    }
165
    $responsive_image_style->save();
166

167 168
    $this->logger('responsive_image')->notice('Responsive image style @label saved.', array('@label' => $responsive_image_style->label()));
    drupal_set_message($this->t('Responsive image style %label saved.', array('%label' => $responsive_image_style->label())));
169

170 171 172
    // Redirect to edit form after creating a new responsive image style or
    // after selecting another breakpoint group.
    if (!$responsive_image_style->hasImageStyleMappings()) {
173
      $form_state->setRedirect(
174 175
        'entity.responsive_image_style.edit_form',
        array('responsive_image_style' => $responsive_image_style->id())
176
      );
177 178
    }
    else {
179
      $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
180 181 182 183
    }
  }

}