loadBreakpointGroup(); $this->loadAllMappings(); } /** * {@inheritdoc} */ public function calculateDependencies() { parent::calculateDependencies(); if (isset($this->breakpointGroup)) { // @todo Implement toArray() so we do not have reload the // entity since this property is changed in // \Drupal\responsive_image\Entity\ResponsiveImageMapping::save(). $breakpoint_group = \Drupal::entityManager()->getStorage('breakpoint_group')->load($this->breakpointGroup); $this->addDependency('entity', $breakpoint_group->getConfigDependencyName()); } return $this->dependencies; } /** * Overrides Drupal\Core\Entity::save(). */ public function save() { // Only save the keys, but return the full objects. $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 // identifiers are not possible. $loaded_mappings = $this->mappings; $this->mappings = array(); foreach ($loaded_mappings as $breakpoint_id => $mapping) { list($source_type, $source, $name) = explode('.', $breakpoint_id); $this->mappings[$source_type][$source][$name] = $mapping; } parent::save(); $this->loadBreakpointGroup(); $this->loadAllMappings(); } /** * Implements \Drupal\Core\Entity\EntityInterface::createDuplicate(). */ public function createDuplicate() { return entity_create('responsive_image_mapping', array( 'id' => '', 'label' => t('Clone of !label', array('!label' => String::checkPlain($this->label()))), 'mappings' => $this->getMappings(), )); } /** * Loads the breakpoint group. */ protected function loadBreakpointGroup() { if ($this->getBreakpointGroup()) { $breakpoint_group = entity_load('breakpoint_group', $this->getBreakpointGroup()); $this->setBreakpointGroup($breakpoint_group); } } /** * Loads all mappings and removes non-existing ones. */ protected function loadAllMappings() { $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. $all_mappings[$breakpoint_id]['1x'] = ''; if (isset($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) { $all_mappings[$breakpoint_id][$multiplier] = ''; if (isset($loaded_mappings[$source_type][$source][$name][$multiplier])) { $all_mappings[$breakpoint_id][$multiplier] = $loaded_mappings[$source_type][$source][$name][$multiplier]; } } } } } } $this->setMappings($all_mappings); } /** * {@inheritdoc} */ public function hasMappings() { $mapping_found = FALSE; foreach ($this->getMappings() as $multipliers) { $filtered_array = array_filter($multipliers); if (!empty($filtered_array)) { $mapping_found = TRUE; break; } } return $mapping_found; } /** * {@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'); } }