ResponsiveImageMapping.php 6.22 KB
Newer Older
1 2 3 4
<?php

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

8
namespace Drupal\responsive_image\Entity;
9

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Config\Entity\ConfigEntityBase;
12
use Drupal\responsive_image\ResponsiveImageMappingInterface;
13 14

/**
15
 * Defines the responsive image mapping entity.
16
 *
17
 * @ConfigEntityType(
18 19
 *   id = "responsive_image_mapping",
 *   label = @Translation("Responsive image mapping"),
20
 *   handlers = {
21
 *     "list_builder" = "Drupal\responsive_image\ResponsiveImageMappingListBuilder",
22
 *     "form" = {
23 24
 *       "edit" = "Drupal\responsive_image\ResponsiveImageMappingForm",
 *       "add" = "Drupal\responsive_image\ResponsiveImageMappingForm",
25
 *       "delete" = "Drupal\responsive_image\Form\ResponsiveImageMappingDeleteForm",
26
 *       "duplicate" = "Drupal\responsive_image\ResponsiveImageMappingForm"
27
 *     }
28
 *   },
29
 *   list_path = "admin/config/media/responsive-image-mapping",
30
 *   admin_permission = "administer responsive images",
31
 *   config_prefix = "mappings",
32 33
 *   entity_keys = {
 *     "id" = "id",
34
 *     "label" = "label"
35 36
 *   },
 *   links = {
37 38
 *     "edit-form" = "entity.responsive_image_mapping.edit_form",
 *     "duplicate-form" = "entity.responsive_image_mapping.duplicate_form"
39 40 41
 *   }
 * )
 */
42
class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImageMappingInterface {
43 44

  /**
45
   * The responsive image ID (machine name).
46 47 48 49 50 51
   *
   * @var string
   */
  public $id;

  /**
52
   * The responsive image label.
53 54 55 56 57 58
   *
   * @var string
   */
  public $label;

  /**
59
   * The responsive image mappings.
60 61 62
   *
   * @var array
   */
63
  protected $mappings = array();
64 65

  /**
66
   * The responsive image breakpoint group.
67
   *
68
   * @var Drupal\breakpoint\Entity\BreakpointGroup
69
   */
70
  protected $breakpointGroup = '';
71 72 73 74 75 76 77 78 79 80

  /**
   * Overrides Drupal\config\ConfigEntityBase::__construct().
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);
    $this->loadBreakpointGroup();
    $this->loadAllMappings();
  }

81 82 83 84 85 86 87 88 89
  /**
   * {@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().
90
      $breakpoint_group = \Drupal::entityManager()->getStorage('breakpoint_group')->load($this->breakpointGroup);
91 92 93 94 95
      $this->addDependency('entity', $breakpoint_group->getConfigDependencyName());
    }
    return $this->dependencies;
  }

96 97 98 99 100
  /**
   * Overrides Drupal\Core\Entity::save().
   */
  public function save() {
    // Only save the keys, but return the full objects.
101 102 103
    $breakpoint_group = $this->getBreakpointGroup();
    if ($breakpoint_group && is_object($breakpoint_group)) {
      $this->setBreakpointGroup($breakpoint_group->id());
104
    }
105 106 107 108 109 110 111 112 113 114

    // 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;
    }

115 116 117 118 119 120
    parent::save();
    $this->loadBreakpointGroup();
    $this->loadAllMappings();
  }

  /**
121
   * Implements \Drupal\Core\Entity\EntityInterface::createDuplicate().
122 123
   */
  public function createDuplicate() {
124
    return entity_create('responsive_image_mapping', array(
125
      'id' => '',
126
      'label' => t('Clone of !label', array('!label' => String::checkPlain($this->label()))),
127
      'mappings' => $this->getMappings(),
128 129 130 131
    ));
  }

  /**
132
   * Loads the breakpoint group.
133 134
   */
  protected function loadBreakpointGroup() {
135 136 137
    if ($this->getBreakpointGroup()) {
      $breakpoint_group = entity_load('breakpoint_group', $this->getBreakpointGroup());
      $this->setBreakpointGroup($breakpoint_group);
138 139 140 141
    }
  }

  /**
142
   * Loads all mappings and removes non-existing ones.
143 144
   */
  protected function loadAllMappings() {
145 146 147 148
    $loaded_mappings = $this->getMappings();
    $all_mappings = array();
    if ($breakpoint_group = $this->getBreakpointGroup()) {
      foreach ($breakpoint_group->getBreakpoints() as $breakpoint_id => $breakpoint) {
149 150 151 152
        // Get the components of the breakpoint ID to match the format of the
        // configuration file.
        list($source_type, $source, $name) = explode('.', $breakpoint_id);

153
        // Get the mapping for the default multiplier.
154
        $all_mappings[$breakpoint_id]['1x'] = '';
155
        if (isset($loaded_mappings[$source_type][$source][$name]['1x'])) {
156
          $all_mappings[$breakpoint_id]['1x'] = $loaded_mappings[$source_type][$source][$name]['1x'];
157 158 159 160 161 162
        }

        // Get the mapping for the other multipliers.
        if (isset($breakpoint->multipliers) && !empty($breakpoint->multipliers)) {
          foreach ($breakpoint->multipliers as $multiplier => $status) {
            if ($status) {
163
              $all_mappings[$breakpoint_id][$multiplier] = '';
164
              if (isset($loaded_mappings[$source_type][$source][$name][$multiplier])) {
165
                $all_mappings[$breakpoint_id][$multiplier] = $loaded_mappings[$source_type][$source][$name][$multiplier];
166 167 168 169 170 171
              }
            }
          }
        }
      }
    }
172
    $this->setMappings($all_mappings);
173 174 175
  }

  /**
176
   * {@inheritdoc}
177 178 179
   */
  public function hasMappings() {
    $mapping_found = FALSE;
180
    foreach ($this->getMappings() as $multipliers) {
181 182 183 184 185 186 187 188
      $filtered_array = array_filter($multipliers);
      if (!empty($filtered_array)) {
        $mapping_found = TRUE;
        break;
      }
    }
    return $mapping_found;
  }
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

  /**
   * {@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');
  }
219
}