PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/breakpoint/lib/Drupal/breakpoint/Plugin/Core/Entity/BreakpointGroup.php

https://github.com/jessebeach/drupal
PHP | 215 lines | 85 code | 17 blank | 113 comment | 13 complexity | 9979e4556a5670e9669eb06ccab8049a MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\breakpoint\Plugin\Core\Entity\BreakpointGroup.
  5. */
  6. namespace Drupal\breakpoint\Plugin\Core\Entity;
  7. use Drupal\Core\Config\Entity\ConfigEntityBase;
  8. use Drupal\breakpoint\InvalidBreakpointSourceException;
  9. use Drupal\breakpoint\InvalidBreakpointSourceTypeException;
  10. use Drupal\Core\Annotation\Plugin;
  11. use Drupal\Core\Annotation\Translation;
  12. /**
  13. * Defines the BreakpointGroup entity.
  14. *
  15. * @Plugin(
  16. * id = "breakpoint_group",
  17. * label = @Translation("Breakpoint group"),
  18. * module = "breakpoint",
  19. * controller_class = "Drupal\Core\Config\Entity\ConfigStorageController",
  20. * config_prefix = "breakpoint.breakpoint_group",
  21. * entity_keys = {
  22. * "id" = "id",
  23. * "label" = "label",
  24. * "uuid" = "uuid"
  25. * }
  26. * )
  27. */
  28. class BreakpointGroup extends ConfigEntityBase {
  29. /**
  30. * The breakpoint group ID.
  31. *
  32. * @var string
  33. */
  34. public $id;
  35. /**
  36. * The breakpoint group UUID.
  37. *
  38. * @var string
  39. */
  40. public $uuid;
  41. /**
  42. * The breakpoint group machine name.
  43. *
  44. * @var string
  45. */
  46. public $name;
  47. /**
  48. * The breakpoint group label.
  49. *
  50. * @var string
  51. */
  52. public $label;
  53. /**
  54. * The breakpoint group breakpoints.
  55. *
  56. * @var array
  57. * Array containing all breakpoints of this group.
  58. *
  59. * @see Drupal\breakpoint\Plugin\Core\Entity\Breakpoint
  60. */
  61. public $breakpoints = array();
  62. /**
  63. * The breakpoint group source: theme or module name. Use 'user' for
  64. * user-created groups.
  65. *
  66. * @var string
  67. */
  68. public $source = 'user';
  69. /**
  70. * The breakpoint group source type.
  71. *
  72. * @var string
  73. * Allowed values:
  74. * Breakpoint::SOURCE_TYPE_THEME
  75. * Breakpoint::SOURCE_TYPE_MODULE
  76. * Breakpoint::SOURCE_TYPE_USER_DEFINED
  77. *
  78. * @see Drupal\breakpoint\Plugin\Core\Entity\Breakpoint
  79. */
  80. public $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED;
  81. /**
  82. * Overrides Drupal\config\ConfigEntityBase::__construct().
  83. */
  84. public function __construct(array $values, $entity_type) {
  85. parent::__construct($values, $entity_type);
  86. $this->loadAllBreakpoints();
  87. }
  88. /**
  89. * Overrides Drupal\Core\Entity\Entity::save().
  90. */
  91. public function save() {
  92. // Check if everything is valid.
  93. if (!$this->isValid()) {
  94. throw new InvalidBreakpointException('Invalid data detected.');
  95. }
  96. if (empty($this->id)) {
  97. $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name;
  98. }
  99. // Only save the keys, but return the full objects.
  100. $this->breakpoints = array_keys($this->breakpoints);
  101. parent::save();
  102. $this->loadAllBreakpoints();
  103. }
  104. /**
  105. * Checks if the breakpoint group is valid.
  106. *
  107. * @throws Drupal\breakpoint\InvalidBreakpointSourceTypeException
  108. * @throws Drupal\breakpoint\InvalidBreakpointSourceException
  109. *
  110. * @return true
  111. * Returns true if the breakpoint group is valid.
  112. */
  113. public function isValid() {
  114. // Check for illegal values in breakpoint group source type.
  115. if (!in_array($this->sourceType, array(
  116. Breakpoint::SOURCE_TYPE_USER_DEFINED,
  117. Breakpoint::SOURCE_TYPE_MODULE,
  118. Breakpoint::SOURCE_TYPE_THEME)
  119. )) {
  120. throw new InvalidBreakpointSourceTypeException(format_string('Invalid source type @source_type', array(
  121. '@source_type' => $this->sourceType,
  122. )));
  123. }
  124. // Check for illegal characters in breakpoint group source.
  125. if (preg_match('/[^a-z_]+/', $this->source) || empty($this->source)) {
  126. 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)));
  127. }
  128. // Check for illegal characters in breakpoint group name.
  129. if (preg_match('/[^a-z0-9_]+/', $this->name || empty($this->name))) {
  130. 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)));
  131. }
  132. return TRUE;
  133. }
  134. /**
  135. * Adds a breakpoint using a name and a media query.
  136. *
  137. * @param string $name
  138. * The name of the breakpoint.
  139. * @param string $media_query
  140. * Media query.
  141. */
  142. public function addBreakpointFromMediaQuery($name, $media_query) {
  143. // Use the existing breakpoint if it exists.
  144. $breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->name . '.' . $name);
  145. if (!$breakpoint) {
  146. // Build a new breakpoint.
  147. $breakpoint = entity_create('breakpoint', array(
  148. 'name' => $name,
  149. 'label' => $name,
  150. 'mediaQuery' => $media_query,
  151. 'source' => $this->name,
  152. 'sourceType' => $this->sourceType,
  153. 'weight' => count($this->breakpoints),
  154. ));
  155. $breakpoint->save();
  156. }
  157. $this->breakpoints[$breakpoint->id()] = $breakpoint;
  158. }
  159. /**
  160. * Adds one or more breakpoints to this group.
  161. *
  162. * The breakpoint name is either the machine_name or the id of a breakpoint.
  163. *
  164. * @param array $breakpoints
  165. * Array containing breakpoints keyed by their id.
  166. */
  167. public function addBreakpoints($breakpoints) {
  168. foreach ($breakpoints as $breakpoint_name) {
  169. // Check if breakpoint exists, assume $breakpoint_name is a machine name.
  170. $breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->source . '.' . $breakpoint_name);
  171. // If the breakpoint doesn't exist, assume $breakpoint_name is an id.
  172. if (!$breakpoint) {
  173. $breakpoint = entity_load('breakpoint', $breakpoint_name);
  174. }
  175. // If the breakpoint doesn't exists, do not add it.
  176. if ($breakpoint) {
  177. // Add breakpoint to group.
  178. $this->breakpoints[$breakpoint->id()] = $breakpoint;
  179. }
  180. }
  181. }
  182. /**
  183. * Loads all breakpoints, remove non-existing ones.
  184. *
  185. * @return array
  186. * Array containing breakpoints keyed by their id.
  187. */
  188. protected function loadAllBreakpoints() {
  189. $breakpoints = $this->breakpoints;
  190. $this->breakpoints = array();
  191. foreach ($breakpoints as $breakpoint_id) {
  192. $breakpoint = breakpoint_load($breakpoint_id);
  193. if ($breakpoint) {
  194. $this->breakpoints[$breakpoint_id] = $breakpoint;
  195. }
  196. }
  197. }
  198. }