diff --git a/core/modules/breakpoint/src/BreakpointGroupInterface.php b/core/modules/breakpoint/src/BreakpointGroupInterface.php index ae4f2e9..4537bd0 100644 --- a/core/modules/breakpoint/src/BreakpointGroupInterface.php +++ b/core/modules/breakpoint/src/BreakpointGroupInterface.php @@ -25,6 +25,99 @@ */ public function isValid(); + /** + * Returns the breakpoint group machine name. + * + * @return string + * The breakpoint group machine name + */ + public function getName(); + + /** + * Sets the breakpoint group machine name. + * + * @param string $name + * The breakpoint group machine name + * + * @return $this + * The called breakpoint group entity + */ + public function setName($name); + + /** + * Sets the breakpoint group label. + * + * @param string $label + * The breakpoint group label + * + * @return $this + * The called breakpoint group entity + */ + public function setLabel($label); + + /** + * Gets the array of breakpoints for the breakpoint group. + * + * @return array + * The array of breakpoints for the breakpoint group. + */ + public function getBreakpoints(); + + /** + * Sets the breakpoint group breakpoints. + * + * @param array $breakpoints + * The breakpoint group breakpoints + * + * @return $this + * The called breakpoint group entity + */ + public function setBreakpoints(array $breakpoints); + + /** + * Returns the breakpoint group source. + * + * @return string + * The breakpoint group source: theme or module name, or + * 'user' for user-created groups. + */ + public function getSource(); + + /** + * Sets the breakpoint group source: theme or module name, + * or 'user' for user-created groups. + * + * @param string $source + * The breakpoint group source + * + * @return $this + * The called breakpoint group entity + */ + public function setSource($source); + + /** + * Returns the breakpoint group source type. + * + * @return string + * The breakpoint group source type + */ + public function getSourceType(); + + /** + * Sets the breakpoint group source type. + * + * @param string $source_type + * The breakpoint group source + * Allowed values: + * Breakpoint::SOURCE_TYPE_THEME + * Breakpoint::SOURCE_TYPE_MODULE + * Breakpoint::SOURCE_TYPE_USER_DEFINED + * + * @return $this + * The called breakpoint group entity + */ + public function setSourceType($sourceType); + /** * Adds a breakpoint using a name and a media query. * @@ -41,7 +134,7 @@ public function addBreakpointFromMediaQuery($name, $media_query); * The breakpoint name is either the machine_name or the ID of a breakpoint. * * @param array $breakpoints - * Array containing breakpoint objects + * Array containing breakpoint objects or names * * @return \Drupal\breakpoint\Entity\BreakpointGroup * The breakpoint group object. @@ -49,14 +142,6 @@ public function addBreakpointFromMediaQuery($name, $media_query); public function addBreakpoints($breakpoints); /** - * Gets the array of breakpoints for the breakpoint group. - * - * @return \Drupal\breakpoint\Entity\BreakpointInterface[] - * The array of breakpoints for the breakpoint group. - */ - public function getBreakpoints(); - - /** * Gets a breakpoint from the breakpoint group by ID. * * @param string $id diff --git a/core/modules/breakpoint/src/Entity/BreakpointGroup.php b/core/modules/breakpoint/src/Entity/BreakpointGroup.php index 0b585e2..77d1d2b 100644 --- a/core/modules/breakpoint/src/Entity/BreakpointGroup.php +++ b/core/modules/breakpoint/src/Entity/BreakpointGroup.php @@ -7,12 +7,12 @@ namespace Drupal\breakpoint\Entity; -use Drupal\breakpoint\InvalidBreakpointNameException; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\breakpoint\BreakpointGroupInterface; +use Drupal\breakpoint\BreakpointInterface; use Drupal\breakpoint\InvalidBreakpointSourceException; use Drupal\breakpoint\InvalidBreakpointSourceTypeException; -use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\breakpoint\InvalidBreakpointNameException; /** * Defines the BreakpointGroup entity. @@ -40,7 +40,7 @@ class BreakpointGroup extends ConfigEntityBase implements BreakpointGroupInterfa * * @var string */ - public $name; + protected $name; /** * The breakpoint group label. @@ -75,20 +75,20 @@ class BreakpointGroup extends ConfigEntityBase implements BreakpointGroupInterfa * * @var string */ - public $source = 'user'; + protected $source = 'user'; /** * The breakpoint group source type. * * @var string * Allowed values: - * Breakpoint::SOURCE_TYPE_THEME - * Breakpoint::SOURCE_TYPE_MODULE - * Breakpoint::SOURCE_TYPE_USER_DEFINED + * Breakpoint::SOURCE_TYPE_THEME + * Breakpoint::SOURCE_TYPE_MODULE + * Breakpoint::SOURCE_TYPE_USER_DEFINED * * @see \Drupal\breakpoint\Entity\Breakpoint */ - public $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED; + protected $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED; /** * {@inheritdoc} @@ -104,6 +104,84 @@ public function __construct(array $values, $entity_type = 'breakpoint_group') { parent::__construct($values, $entity_type); } + /** + * {@inheritdoc} + */ + public function getName() { + return $this->get('name'); + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + $this->set('name', $name); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setLabel($label) { + $this->set('label', $label); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getBreakpoints() { + $breakpoints = $this->get('breakpoints'); + $breakpoint_ids = $this->get('breakpoint_ids'); + if (empty($breakpoints) && !empty($breakpoint_ids)) { + $this->setBreakpoints(entity_load_multiple('breakpoint', $breakpoint_ids)); + } + return $this->get('breakpoints'); + } + + /** + * {@inheritdoc} + */ + public function setBreakpoints(array $breakpoints = array()) { + $this->set('breakpoints', $breakpoints); + $breakpoint_ids = array(); + foreach ($breakpoints as $breakpoint) { + $breakpoint_ids[] = $breakpoint->id(); + } + $this->set('breakpoint_ids', $breakpoint_ids); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getSource() { + return $this->get('source'); + } + + /** + * {@inheritdoc} + */ + public function setSource($source = 'user') { + $this->set('source', $source); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getSourceType() { + return $this->get('sourceType'); + } + + /** + * {@inheritdoc} + */ + public function setSourceType($sourceType) { + $this->set('sourceType', $sourceType); + return $this; + } + /** * Overrides Drupal\Core\Entity\Entity::save(). */ @@ -132,22 +210,28 @@ public function id() { */ 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) - )) { + if (!in_array($this->getSourceType(), 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, + '@source_type' => $this->getSourceType(), ))); } // 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))); + $source = $this->getSource(); + if (preg_match('/[^a-z_]+/', $source) || empty($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->getSource(), + ))); } // 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))); + $name = $this->getName(); + if (preg_match('/[^a-z0-9_]+/', $name) || empty($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->getName(), + ))); } return TRUE; } @@ -157,16 +241,16 @@ public function isValid() { */ public function addBreakpointFromMediaQuery($name, $media_query) { // Use the existing breakpoint if it exists. - $breakpoint = Breakpoint::load($this->sourceType . '.' . $this->name . '.' . $name); + $breakpoint = entity_load('breakpoint', $this->getSourceType() . '.' . $this->getName() . '.' . $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->breakpoint_ids), + 'source' => $this->getName(), + 'sourceType' => $this->getSourceType(), + 'weight' => count($this->getBreakpoints()), )); $breakpoint->save(); } @@ -177,27 +261,29 @@ public function addBreakpointFromMediaQuery($name, $media_query) { * {@inheritdoc} */ public function addBreakpoints($breakpoints) { - foreach ($breakpoints as $breakpoint) { - // Add breakpoint to group. - $this->breakpoints[$breakpoint->id()] = $breakpoint; - $this->breakpoint_ids[] = $breakpoint->id(); - } - return $this; - } - - /** - * {@inheritdoc} - */ - public function getBreakpoints() { - if (empty($this->breakpoints)) { - foreach ($this->breakpoint_ids as $breakpoint_id) { - $breakpoint = Breakpoint::load($breakpoint_id); - if ($breakpoint) { - $this->breakpoints[$breakpoint_id] = $breakpoint; - } + $new_breakpoints = array(); + foreach ($breakpoints as $breakpoint_name) { + if ($breakpoint_name instanceof BreakpointInterface) { + // Don't add unsaved breakpoints, check if we can load them. + $breakpoint_name = $breakpoint_name->id(); } - } - return $this->breakpoints; + // Check if breakpoint exists, assume $breakpoint_name is a machine name. + $breakpoint = entity_load('breakpoint', $this->getSourceType() . '.' . $this->getSource() . '.' . $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 array holding new breakpoints. + $new_breakpoints[$breakpoint->id()] = $breakpoint; + } + } + + // Add new breakpoints. + $breakpoints = $this->getBreakpoints() + $new_breakpoints; + return $this->setBreakpoints($breakpoints); } /** diff --git a/core/modules/breakpoint/src/Tests/BreakpointGroupAPITest.php b/core/modules/breakpoint/src/Tests/BreakpointGroupAPITest.php index 52c2ef4..b94a21e 100644 --- a/core/modules/breakpoint/src/Tests/BreakpointGroupAPITest.php +++ b/core/modules/breakpoint/src/Tests/BreakpointGroupAPITest.php @@ -51,9 +51,9 @@ public function testConfigName() { // Try an invalid source. $breakpoint_group = $breakpoint_group->createDuplicate(); - $breakpoint_group->name = ''; - $breakpoint_group->sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED; - $breakpoint_group->source = 'custom*_module source'; + $breakpoint_group->setName(''); + $breakpoint_group->setSourceType(Breakpoint::SOURCE_TYPE_USER_DEFINED); + $breakpoint_group->setSource('custom*_module source'); $exception = FALSE; try { @@ -66,8 +66,8 @@ public function testConfigName() { // Try a valid breakpoint_group. $breakpoint_group = $breakpoint_group->createDuplicate(); - $breakpoint_group->name = 'test'; - $breakpoint_group->source = 'custom_module_source'; + $breakpoint_group->setName('test'); + $breakpoint_group->setSource('custom_module_source'); $exception = FALSE; try { diff --git a/core/modules/breakpoint/src/Tests/BreakpointGroupTestBase.php b/core/modules/breakpoint/src/Tests/BreakpointGroupTestBase.php index 95141e3..c0a8941 100644 --- a/core/modules/breakpoint/src/Tests/BreakpointGroupTestBase.php +++ b/core/modules/breakpoint/src/Tests/BreakpointGroupTestBase.php @@ -44,17 +44,17 @@ public function verifyBreakpointGroup(BreakpointGroup $group, BreakpointGroup $c '%group' => $group->label(), '%property' => $property, ); - if (is_array($compare_set->{$property})) { - $this->assertEqual(array_keys($compare_set->{$property}), array_keys($group->{$property}), format_string('breakpoint_group_load: Proper %property for breakpoint group %group.', $t_args), 'Breakpoint API'); + if (is_array($compare_set->get($property))) { + $this->assertEqual(array_keys($compare_set->get($property)), array_keys($group->get($property)), format_string('breakpoint_group_load: Proper %property for breakpoint group %group.', $t_args), 'Breakpoint API'); } else { $t_args = array( '%group' => $group->label(), '%property' => $property, - '%property1' => $compare_set->{$property}, - '%property2' => $group->{$property}, + '%property1' => $compare_set->get($property), + '%property2' => $group->get($property), ); - $this->assertEqual($compare_set->{$property}, $group->{$property}, format_string('breakpoint_group_load: Proper %property: %property1 == %property2 for breakpoint group %group.', $t_args), 'Breakpoint API'); + $this->assertEqual($compare_set->get($property), $group->get($property), format_string('breakpoint_group_load: Proper %property: %property1 == %property2 for breakpoint group %group.', $t_args), 'Breakpoint API'); } }