summaryrefslogtreecommitdiffstats
path: root/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php
blob: 3e8440ecc0e3afd501b4e6ee483d7b57e0dfedc8 (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
<?php

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

namespace Drupal\responsive_image\Entity;

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
   */
  protected $id;

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

  /**
   * The responsive image mappings.
   *
   * Each responsive mapping array contains the following keys:
   * - breakpoint_id
   * - multiplier
   * - image_style
   *
   * @var array
   */
  protected $mappings = array();

  /**
   * @var array
   */
  protected $keyedMappings;

  /**
   * The responsive image breakpoint group.
   *
   * @var string
   */
  protected $breakpointGroup = '';

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values, $entity_type_id = 'responsive_image_mapping') {
    parent::__construct($values, $entity_type_id);
  }

  /**
   * {@inheritdoc}
   */
  public function addMapping($breakpoint_id, $multiplier, $image_style) {
    foreach ($this->mappings as &$mapping) {
      if ($mapping['breakpoint_id'] === $breakpoint_id && $mapping['multiplier'] === $multiplier) {
        $mapping['image_style'] = $image_style;
        return $this;
      }
    }
    $this->mappings[] = array(
      'breakpoint_id' => $breakpoint_id,
      'multiplier' => $multiplier,
      'image_style' => $image_style,
    );
    $this->keyedMappings = NULL;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasMappings() {
    return !empty($this->mappings);
  }

  /**
   * {@inheritdoc}
   */
  public function getKeyedMappings() {
    if (!$this->keyedMappings) {
      $this->keyedMappings = array();
      foreach($this->mappings as $mapping) {
        $this->keyedMappings[$mapping['breakpoint_id']][$mapping['multiplier']] = $mapping['image_style'];
      }
    }
    return $this->keyedMappings;
  }

  /**
   * {@inheritdoc}
   */
  public function getImageStyle($breakpoint_id, $multiplier) {
    $map = $this->getKeyedMappings();
    if (isset($map[$breakpoint_id][$multiplier])) {
      return $map[$breakpoint_id][$multiplier];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getMappings() {
    return $this->get('mappings');
  }

  /**
   * {@inheritdoc}
   */
  public function setBreakpointGroup($breakpoint_group) {
    // If the breakpoint group is changed then the mappings are invalid.
    if ($breakpoint_group !== $this->breakpointGroup) {
      $this->removeMappings();
    }
    $this->set('breakpointGroup', $breakpoint_group);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getBreakpointGroup() {
    return $this->get('breakpointGroup');
  }

  /**
   * {@inheritdoc}
   */
  public function removeMappings() {
    $this->mappings = array();
    $this->keyedMappings = NULL;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
    $providers = \Drupal::service('breakpoint.manager')->getGroupProviders($this->breakpointGroup);
    foreach ($providers as $provider => $type) {
      $this->addDependency($type, $provider);
    }
    return $this->dependencies;
  }

}