diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php b/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php index 658ad5d8b0c1a85fab701c00b1260f3ca1d6d8d5..b4fb7fc84551be202a59d09579b474230e24c69a 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\responsive_image\ResponsiveImageMapping. + * Contains \Drupal\responsive_image\Entity\ResponsiveImageMapping. */ namespace Drupal\responsive_image\Entity; @@ -59,14 +59,14 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage * * @var array */ - public $mappings = array(); + protected $mappings = array(); /** * The responsive image breakpoint group. * - * @var BreakpointGroup + * @var Drupal\breakpoint\Entity\BreakpointGroup */ - public $breakpointGroup = ''; + protected $breakpointGroup = ''; /** * Overrides Drupal\config\ConfigEntityBase::__construct(). @@ -97,8 +97,9 @@ public function calculateDependencies() { */ public function save() { // Only save the keys, but return the full objects. - if (isset($this->breakpointGroup) && is_object($this->breakpointGroup)) { - $this->breakpointGroup = $this->breakpointGroup->id(); + $breakpoint_group = $this->getBreakpointGroup(); + if ($breakpoint_group && is_object($breakpoint_group)) { + $this->setBreakpointGroup($breakpoint_group->id()); } // Split the breakpoint ids into their different parts, as dots as @@ -122,7 +123,7 @@ public function createDuplicate() { return entity_create('responsive_image_mapping', array( 'id' => '', 'label' => t('Clone of !label', array('!label' => check_plain($this->label()))), - 'mappings' => $this->mappings, + 'mappings' => $this->getMappings(), )); } @@ -130,9 +131,9 @@ public function createDuplicate() { * Loads the breakpoint group. */ protected function loadBreakpointGroup() { - if ($this->breakpointGroup) { - $breakpoint_group = entity_load('breakpoint_group', $this->breakpointGroup); - $this->breakpointGroup = $breakpoint_group; + if ($this->getBreakpointGroup()) { + $breakpoint_group = entity_load('breakpoint_group', $this->getBreakpointGroup()); + $this->setBreakpointGroup($breakpoint_group); } } @@ -140,33 +141,34 @@ protected function loadBreakpointGroup() { * Loads all mappings and removes non-existing ones. */ protected function loadAllMappings() { - $loaded_mappings = $this->mappings; - $this->mappings = array(); - if ($this->breakpointGroup) { - foreach ($this->breakpointGroup->getBreakpoints() as $breakpoint_id => $breakpoint) { + $loaded_mappings = $this->getMappings(); + $all_mappings = array(); + if ($breakpoint_group = $this->getBreakpointGroup()) { + foreach ($breakpoint_group->getBreakpoints() as $breakpoint_id => $breakpoint) { // Get the components of the breakpoint ID to match the format of the // configuration file. list($source_type, $source, $name) = explode('.', $breakpoint_id); // Get the mapping for the default multiplier. - $this->mappings[$breakpoint_id]['1x'] = ''; + $all_mappings[$breakpoint_id]['1x'] = ''; if (isset($loaded_mappings[$source_type][$source][$name]['1x'])) { - $this->mappings[$breakpoint_id]['1x'] = $loaded_mappings[$source_type][$source][$name]['1x']; + $all_mappings[$breakpoint_id]['1x'] = $loaded_mappings[$source_type][$source][$name]['1x']; } // Get the mapping for the other multipliers. if (isset($breakpoint->multipliers) && !empty($breakpoint->multipliers)) { foreach ($breakpoint->multipliers as $multiplier => $status) { if ($status) { - $this->mappings[$breakpoint_id][$multiplier] = ''; + $all_mappings[$breakpoint_id][$multiplier] = ''; if (isset($loaded_mappings[$source_type][$source][$name][$multiplier])) { - $this->mappings[$breakpoint_id][$multiplier] = $loaded_mappings[$source_type][$source][$name][$multiplier]; + $all_mappings[$breakpoint_id][$multiplier] = $loaded_mappings[$source_type][$source][$name][$multiplier]; } } } } } } + $this->setMappings($all_mappings); } /** @@ -174,7 +176,7 @@ protected function loadAllMappings() { */ public function hasMappings() { $mapping_found = FALSE; - foreach ($this->mappings as $multipliers) { + foreach ($this->getMappings() as $multipliers) { $filtered_array = array_filter($multipliers); if (!empty($filtered_array)) { $mapping_found = TRUE; @@ -183,4 +185,52 @@ public function hasMappings() { } return $mapping_found; } + + /** + * {@inheritdoc} + */ + public function toArray() { + $names = array( + 'id', + 'uuid', + 'label', + 'mappings', + 'breakpointGroup', + ); + $properties = array(); + foreach ($names as $name) { + $properties[$name] = $this->get($name); + } + return $properties; + } + + /** + * {@inheritdoc} + */ + public function setMappings(array $mappings) { + $this->set('mappings', $mappings); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getMappings() { + return $this->get('mappings'); + } + + /** + * {@inheritdoc} + */ + public function setBreakpointGroup($breakpoint_group) { + $this->set('breakpointGroup', $breakpoint_group); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getBreakpointGroup() { + return $this->get('breakpointGroup'); + } } diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index 8f935c01f97d5d22e91493d56728c40247472f1a..efbec0f9a2163949ec19fb34414ab6cf760b4f31 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -129,16 +129,16 @@ public function viewElements(FieldItemListInterface $items) { $responsive_image_mapping = entity_load('responsive_image_mapping', $this->getSetting('responsive_image_mapping')); if ($responsive_image_mapping) { - foreach ($responsive_image_mapping->mappings as $breakpoint_name => $multipliers) { + foreach ($responsive_image_mapping->getMappings() as $breakpoint_name => $multipliers) { // Make sure there are multipliers. if (!empty($multipliers)) { // Make sure that the breakpoint exists and is enabled. // @todo add the following when breakpoint->status is added again: // $responsive_image_mapping->breakpointGroup->breakpoints[$breakpoint_name]->status - $breakpoint = $responsive_image_mapping->breakpointGroup->getBreakpointById($breakpoint_name); - if ($breakpoint) { + $breakpointGroup = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_name); + if ($breakpointGroup) { // Determine the enabled multipliers. - $multipliers = array_intersect_key($multipliers, $breakpoint->multipliers); + $multipliers = array_intersect_key($multipliers, $breakpointGroup->multipliers); foreach ($multipliers as $multiplier => $image_style) { // Make sure the multiplier still exists. if (!empty($image_style)) { diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php b/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php index d27db527626ea30f8522a334eb2e6304c772c1e6..9b5e9c359b79dd854b5e7026dc3bb00acb182863 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php @@ -36,6 +36,7 @@ public function form(array $form, array &$form_state) { $form['#title'] = $this->t('Edit responsive image mapping @label', array('@label' => $this->entity->label())); } + /** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */ $responsive_image_mapping = $this->entity; $form['label'] = array( '#type' => 'textfield', @@ -64,16 +65,16 @@ public function form(array $form, array &$form_state) { $form['breakpointGroup'] = array( '#type' => 'select', '#title' => $this->t('Breakpoint group'), - '#default_value' => !empty($responsive_image_mapping->breakpointGroup) ? $responsive_image_mapping->breakpointGroup->id() : '', + '#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->mappings as $breakpoint_id => $mapping) { + foreach ($responsive_image_mapping->getMappings() as $breakpoint_id => $mapping) { foreach ($mapping as $multiplier => $image_style) { - $breakpoint = $responsive_image_mapping->breakpointGroup->getBreakpointById($breakpoint_id); + $breakpoint = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_id); $label = $multiplier . ' ' . $breakpoint->name . ' [' . $breakpoint->mediaQuery . ']'; $form['mappings'][$breakpoint_id][$multiplier] = array( '#type' => 'select', @@ -113,11 +114,12 @@ protected function actions(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityFormController::validate(). */ 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->mappings = $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']) { @@ -135,6 +137,7 @@ public function validate(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityFormController::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(); diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingInterface.php b/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingInterface.php index 2ec9832a1cdcb33bf38669026f25bad13e2b1519..bb8b535256e074cdbb565d58d5d8a6cf5aef7df5 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingInterface.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingInterface.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\responsive_image\Entity\ResponsiveImageMappingInterface. + * Contains \Drupal\responsive_image\ResponsiveImageMappingInterface. */ namespace Drupal\responsive_image; @@ -10,13 +10,55 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; /** - * Provides an interface defining a responsive image mapping entity. + * Provides an interface defining a responsive_image mapping entity. */ interface ResponsiveImageMappingInterface extends ConfigEntityInterface { /** - * Checks if there's at least one mapping defined. + * Checks if there is at least one mapping defined. + * + * return bool + * Whether the entity has any responsive_image mappings. */ public function hasMappings(); + /** + * Sets the mappings for the responsive_image mapping. + * + * The array is keyed by the Breakpoint Group Id and then then by each + * Breakpoints multipliers within the Breakpoint Group. + * + * @param array[] $mappings + * The mappings the responsive_image mapping will be set with. + * + * @return $this + */ + public function setMappings(array $mappings); + + /** + * Returns the mappings for the responsive_image mapping. + * + * @return array[] + * The responsive_imagemappings. + */ + public function getMappings(); + + /** + * Sets the breakpoint group for the responsive_image mapping. + * + * @param \Drupal\breakpoint\Entity\BreakpointGroup $breakpoint_group + * The responsive_image mappings breakpoint group. + * + * @return $this + */ + public function setBreakpointGroup($breakpoint_group); + + /** + * Returns the breakpoint group for the responsive_image mapping. + * + * @return \Drupal\breakpoint\Entity\BreakpointGroup + * The responsive_image mappings breakpoint group. + */ + public function getBreakpointGroup(); + } diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/Tests/ResponsiveImageFieldDisplayTest.php b/core/modules/responsive_image/lib/Drupal/responsive_image/Tests/ResponsiveImageFieldDisplayTest.php index c643774358baeea2c993b05c61eb57801ff7cfb9..f58085884fbc0b01952070803fea05d8d3e48a95 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Tests/ResponsiveImageFieldDisplayTest.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Tests/ResponsiveImageFieldDisplayTest.php @@ -88,9 +88,11 @@ public function setUp() { 'breakpointGroup' => 'atestset', )); $responsive_image_mapping->save(); - $responsive_image_mapping->mappings['custom.user.small']['1x'] = 'thumbnail'; - $responsive_image_mapping->mappings['custom.user.medium']['1x'] = 'medium'; - $responsive_image_mapping->mappings['custom.user.large']['1x'] = 'large'; + $mappings = array(); + $mappings['custom.user.small']['1x'] = 'thumbnail'; + $mappings['custom.user.medium']['1x'] = 'medium'; + $mappings['custom.user.large']['1x'] = 'large'; + $responsive_image_mapping->setMappings($mappings); $responsive_image_mapping->save(); } diff --git a/core/modules/responsive_image/tests/Drupal/responsive_image/Tests/PictureMappingEntityTest.php b/core/modules/responsive_image/tests/Drupal/responsive_image/Tests/PictureMappingEntityTest.php index f14c2fea379c6d44eff41ae7b2bf28eb08e10391..9a22ada810799cf0785cc1e3bd4363902a35358a 100644 --- a/core/modules/responsive_image/tests/Drupal/responsive_image/Tests/PictureMappingEntityTest.php +++ b/core/modules/responsive_image/tests/Drupal/responsive_image/Tests/PictureMappingEntityTest.php @@ -125,7 +125,7 @@ public function testCalculateDependencies() { $picture_mapping = new ResponsiveImageMapping(array(), $this->entityTypeId); // Set the breakpoint group after creating the entity to avoid the calls // in the constructor. - $picture_mapping->breakpointGroup = $this->breakpointGroupId; + $picture_mapping->setBreakpointGroup($this->breakpointGroupId); $this->breakpointGroup->expects($this->once()) ->method('getConfigDependencyName') ->will($this->returnValue('breakpoint.breakpoint_group.' . $this->breakpointGroupId));