summaryrefslogtreecommitdiffstats
path: root/core/modules/responsive_image/src/ResponsiveImageMappingForm.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/responsive_image/src/ResponsiveImageMappingForm.php')
-rw-r--r--core/modules/responsive_image/src/ResponsiveImageMappingForm.php73
1 files changed, 49 insertions, 24 deletions
diff --git a/core/modules/responsive_image/src/ResponsiveImageMappingForm.php b/core/modules/responsive_image/src/ResponsiveImageMappingForm.php
index d1510dd..b3780f0 100644
--- a/core/modules/responsive_image/src/ResponsiveImageMappingForm.php
+++ b/core/modules/responsive_image/src/ResponsiveImageMappingForm.php
@@ -7,9 +7,10 @@
namespace Drupal\responsive_image;
-use Drupal\Component\Utility\String;
+use Drupal\breakpoint\BreakpointManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for the responsive image edit/add forms.
@@ -17,14 +18,38 @@ use Drupal\Core\Form\FormStateInterface;
class ResponsiveImageMappingForm extends EntityForm {
/**
+ * The breakpoint manager.
+ *
+ * @var \Drupal\breakpoint\BreakpointManagerInterface
+ */
+ protected $breakpointManager;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('breakpoint.manager')
+ );
+ }
+
+ /**
+ * Constructs the responsive image mapping form.
+ *
+ * @param \Drupal\breakpoint\BreakpointManagerInterface $breakpoint_manager
+ * The breakpoint manager.
+ */
+ public function __construct(BreakpointManagerInterface $breakpoint_manager) {
+ $this->breakpointManager = $breakpoint_manager;
+ }
+
+ /**
* Overrides Drupal\Core\Entity\EntityForm::form().
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* 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.
@@ -67,23 +92,23 @@ class ResponsiveImageMappingForm extends EntityForm {
$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(),
+ '#default_value' => $responsive_image_mapping->getBreakpointGroup(),
+ '#options' => $this->breakpointManager->getGroups(),
'#required' => TRUE,
'#description' => $description,
);
$image_styles = image_style_options(TRUE);
$image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -');
- 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(
+ $breakpoints = $this->breakpointManager->getBreakpointsByGroup($responsive_image_mapping->getBreakpointGroup());
+ foreach ($breakpoints as $breakpoint_id => $breakpoint) {
+ foreach ($breakpoint->getMultipliers() as $multiplier) {
+ $label = $multiplier . ' ' . $breakpoint->getLabel() . ' [' . $breakpoint->getMediaQuery() . ']';
+ $form['keyed_mappings'][$breakpoint_id][$multiplier] = array(
'#type' => 'select',
- '#title' => String::checkPlain($label),
+ '#title' => $label,
'#options' => $image_styles,
- '#default_value' => $image_style,
+ '#default_value' => $responsive_image_mapping->getImageStyle($breakpoint_id, $multiplier),
'#description' => $this->t('Select an image style for this breakpoint.'),
);
}
@@ -98,21 +123,12 @@ class ResponsiveImageMappingForm extends EntityForm {
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
- /** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
- $responsive_image_mapping = $this->entity;
-
// Only validate on edit.
- if ($form_state->hasValue('mappings')) {
- $responsive_image_mapping->setMappings($form_state->getValue('mappings'));
-
+ if ($form_state->hasValue('keyed_mappings')) {
// Check if another breakpoint group is selected.
if ($form_state->getValue('breakpointGroup') != $form_state->getCompleteForm()['breakpointGroup']['#default_value']) {
- // Remove the mappings.
- $form_state->unsetValue('mappings');
- }
- // Make sure at least one mapping is defined.
- elseif (!$responsive_image_mapping->isNew() && !$responsive_image_mapping->hasMappings()) {
- $form_state->setErrorByName('mappings', $this->t('Please select at least one mapping.'));
+ // Remove the mappings since the breakpoint ID has changed.
+ $form_state->unsetValue('keyed_mappings');
}
}
}
@@ -123,6 +139,15 @@ class ResponsiveImageMappingForm extends EntityForm {
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
$responsive_image_mapping = $this->entity;
+ // Remove all the existing mappings and replace with submitted values.
+ $responsive_image_mapping->removeMappings();
+ if ($form_state->hasValue('keyed_mappings')) {
+ foreach ($form_state->getValue('keyed_mappings') as $breakpoint_id => $multipliers) {
+ foreach ($multipliers as $multiplier => $image_style) {
+ $responsive_image_mapping->addMapping($breakpoint_id, $multiplier, $image_style);
+ }
+ }
+ }
$responsive_image_mapping->save();
$this->logger('responsive_image')->notice('Responsive image mapping @label saved.', array('@label' => $responsive_image_mapping->label()));