BreakpointGroup.php 6.25 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\breakpoint\Plugin\Core\Entity\BreakpointGroup.
6 7
 */

8
namespace Drupal\breakpoint\Plugin\Core\Entity;
9 10 11 12

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\breakpoint\InvalidBreakpointSourceException;
use Drupal\breakpoint\InvalidBreakpointSourceTypeException;
13
use Drupal\Core\Entity\Annotation\EntityType;
14
use Drupal\Core\Annotation\Translation;
15 16 17

/**
 * Defines the BreakpointGroup entity.
18
 *
19
 * @EntityType(
20 21 22
 *   id = "breakpoint_group",
 *   label = @Translation("Breakpoint group"),
 *   module = "breakpoint",
23 24 25
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
 *   },
26 27 28 29 30 31 32
 *   config_prefix = "breakpoint.breakpoint_group",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   }
 * )
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */
class BreakpointGroup extends ConfigEntityBase {

  /**
   * The breakpoint group ID.
   *
   * @var string
   */
  public $id;

  /**
   * The breakpoint group UUID.
   *
   * @var string
   */
  public $uuid;

50
  /**
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
   * The breakpoint group machine name.
   *
   * @var string
   */
  public $name;

  /**
   * The breakpoint group label.
   *
   * @var string
   */
  public $label;

  /**
   * The breakpoint group breakpoints.
   *
   * @var array
   *   Array containing all breakpoints of this group.
   *
70
   * @see Drupal\breakpoint\Plugin\Core\Entity\Breakpoint
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   */
  public $breakpoints = array();

  /**
   * The breakpoint group source: theme or module name. Use 'user' for
   * user-created groups.
   *
   * @var string
   */
  public $source = 'user';

  /**
   * The breakpoint group source type.
   *
   * @var string
   *   Allowed values:
   *     Breakpoint::SOURCE_TYPE_THEME
   *     Breakpoint::SOURCE_TYPE_MODULE
   *     Breakpoint::SOURCE_TYPE_USER_DEFINED
   *
91
   * @see Drupal\breakpoint\Plugin\Core\Entity\Breakpoint
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
   */
  public $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED;

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

  /**
   * Overrides Drupal\Core\Entity\Entity::save().
   */
  public function save() {
    // Check if everything is valid.
    if (!$this->isValid()) {
      throw new InvalidBreakpointException('Invalid data detected.');
    }
    if (empty($this->id)) {
      $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name;
    }
    // Only save the keys, but return the full objects.
    $this->breakpoints = array_keys($this->breakpoints);
    parent::save();
    $this->loadAllBreakpoints();
  }

  /**
   * Checks if the breakpoint group is valid.
   *
   * @throws Drupal\breakpoint\InvalidBreakpointSourceTypeException
   * @throws Drupal\breakpoint\InvalidBreakpointSourceException
   *
   * @return true
   *   Returns true if the breakpoint group is valid.
   */
  public function isValid() {
    // Check for illegal values in breakpoint group source type.
    if (!in_array($this->sourceType, array(
        Breakpoint::SOURCE_TYPE_USER_DEFINED,
        Breakpoint::SOURCE_TYPE_MODULE,
        Breakpoint::SOURCE_TYPE_THEME)
      )) {
      throw new InvalidBreakpointSourceTypeException(format_string('Invalid source type @source_type', array(
        '@source_type' => $this->sourceType,
      )));
    }
    // Check for illegal characters in breakpoint group source.
    if (preg_match('/[^a-z_]+/', $this->source) || empty($this->source)) {
      throw new InvalidBreakpointSourceException(format_string("Invalid value '@source' for breakpoint group source property. Breakpoint group source property can only contain lowercase letters and underscores.", array('@source' => $this->source)));
    }
    // Check for illegal characters in breakpoint group name.
    if (preg_match('/[^a-z0-9_]+/', $this->name || empty($this->name))) {
      throw new InvalidBreakpointNameException(format_string("Invalid value '@name' for breakpoint group name property. Breakpoint group name property can only contain lowercase letters, numbers and underscores.", array('@name' => $this->name)));
    }
    return TRUE;
  }

  /**
   * Adds a breakpoint using a name and a media query.
   *
   * @param string $name
   *   The name of the breakpoint.
   * @param string $media_query
   *   Media query.
   */
  public function addBreakpointFromMediaQuery($name, $media_query) {
    // Use the existing breakpoint if it exists.
    $breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->name . '.' . $name);
    if (!$breakpoint) {
      // Build a new breakpoint.
      $breakpoint = entity_create('breakpoint', array(
        'name' => $name,
        'label' => $name,
        'mediaQuery' => $media_query,
        'source' => $this->name,
        'sourceType' => $this->sourceType,
        'weight' => count($this->breakpoints),
      ));
      $breakpoint->save();
    }
    $this->breakpoints[$breakpoint->id()] = $breakpoint;
  }

  /**
   * Adds one or more breakpoints to this group.
   *
   * The breakpoint name is either the machine_name or the id of a breakpoint.
   *
   * @param array $breakpoints
   *   Array containing breakpoints keyed by their id.
   */
  public function addBreakpoints($breakpoints) {
    foreach ($breakpoints as $breakpoint_name) {
      // Check if breakpoint exists, assume $breakpoint_name is a machine name.
      $breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->source . '.' . $breakpoint_name);
      // If the breakpoint doesn't exist, assume $breakpoint_name is an id.
      if (!$breakpoint) {
        $breakpoint = entity_load('breakpoint', $breakpoint_name);
      }
      // If the breakpoint doesn't exists, do not add it.
      if ($breakpoint) {
        // Add breakpoint to group.
        $this->breakpoints[$breakpoint->id()] = $breakpoint;
      }
    }
  }

  /**
   * Loads all breakpoints, remove non-existing ones.
   *
   * @return array
   *   Array containing breakpoints keyed by their id.
   */
  protected function loadAllBreakpoints() {
    $breakpoints = $this->breakpoints;
    $this->breakpoints = array();
    foreach ($breakpoints as $breakpoint_id) {
      $breakpoint = breakpoint_load($breakpoint_id);
      if ($breakpoint) {
        $this->breakpoints[$breakpoint_id] = $breakpoint;
      }
    }
  }
}