summaryrefslogtreecommitdiffstats
path: root/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php
blob: ffa63af9fe37ad5846d8dc5a21a6b8cea44ec5de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
219
<?php

/**
 * @file
 * Contains \Drupal\responsive_image\Entity\ResponsiveImageMapping.
 */

namespace Drupal\responsive_image\Entity;

use Drupal\Component\Utility\String;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\responsive_image\ResponsiveImageMappingInterface;

/**
 * Defines the responsive image mapping entity.
 *
 * @ConfigEntityType(
 *   id = "responsive_image_mapping",
 *   label = @Translation("Responsive image mapping"),
 *   handlers = {
 *     "list_builder" = "Drupal\responsive_image\ResponsiveImageMappingListBuilder",
 *     "form" = {
 *       "edit" = "Drupal\responsive_image\ResponsiveImageMappingForm",
 *       "add" = "Drupal\responsive_image\ResponsiveImageMappingForm",
 *       "delete" = "Drupal\responsive_image\Form\ResponsiveImageMappingDeleteForm",
 *       "duplicate" = "Drupal\responsive_image\ResponsiveImageMappingForm"
 *     }
 *   },
 *   list_path = "admin/config/media/responsive-image-mapping",
 *   admin_permission = "administer responsive images",
 *   config_prefix = "mappings",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label"
 *   },
 *   links = {
 *     "edit-form" = "entity.responsive_image_mapping.edit_form",
 *     "duplicate-form" = "entity.responsive_image_mapping.duplicate_form"
 *   }
 * )
 */
class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImageMappingInterface {

  /**
   * The responsive image ID (machine name).
   *
   * @var string
   */
  public $id;

  /**
   * The responsive image label.
   *
   * @var string
   */
  public $label;

  /**
   * The responsive image mappings.
   *
   * @var array
   */
  protected $mappings = array();

  /**
   * The responsive image breakpoint group.
   *
   * @var Drupal\breakpoint\Entity\BreakpointGroup
   */
  protected $breakpointGroup = '';

  /**
   * Overrides Drupal\config\ConfigEntityBase::__construct().
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);
    $this->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');
  }
}