Issue #2030653 by Eli-T, mike.davis, JayeshSolanki, Xano, JeroenT: Expand ResponsiveImageMapping with methods.
parent
add1e1ae2d
commit
ed5d0556cb
|
@ -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 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage
|
|||
*/
|
||||
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 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage
|
|||
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 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage
|
|||
* 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 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage
|
|||
* 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 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage
|
|||
*/
|
||||
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 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage
|
|||
}
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -129,16 +129,16 @@ class ResponsiveImageFormatter extends ImageFormatterBase {
|
|||
|
||||
$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)) {
|
||||
|
|
|
@ -36,6 +36,7 @@ class ResponsiveImageMappingFormController extends EntityFormController {
|
|||
$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',
|
||||
|
@ -64,16 +65,16 @@ class ResponsiveImageMappingFormController extends EntityFormController {
|
|||
$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 @@ class ResponsiveImageMappingFormController extends EntityFormController {
|
|||
* 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 @@ class ResponsiveImageMappingFormController extends EntityFormController {
|
|||
* 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();
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\responsive_image\Entity\ResponsiveImageMappingInterface.
|
||||
* Contains \Drupal\responsive_image\ResponsiveImageMappingInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\responsive_image;
|
||||
|
@ -10,13 +10,55 @@ namespace Drupal\responsive_image;
|
|||
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();
|
||||
|
||||
}
|
||||
|
|
|
@ -88,9 +88,11 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
|
|||
'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();
|
||||
}
|
||||
|
||||
|
|
|
@ -125,7 +125,7 @@ class ResponsiveImageMappingEntityTest extends UnitTestCase {
|
|||
$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));
|
||||
|
|
Loading…
Reference in New Issue