diff --git a/core/modules/breakpoint/breakpoint.module b/core/modules/breakpoint/breakpoint.module index a2450f9..63a6bac 100644 --- a/core/modules/breakpoint/breakpoint.module +++ b/core/modules/breakpoint/breakpoint.module @@ -100,7 +100,7 @@ function _breakpoint_delete_breakpoints($list, $source_type) { // Make sure we only delete breakpoints defined by this theme/module. foreach ($breakpoints as $breakpoint) { - if ($breakpoint->sourceType == $source_type && $breakpoint->source == $breakpoint_group->name) { + if ($breakpoint->getSourceType() == $source_type && $breakpoint->getSource() == $breakpoint_group->name) { $breakpoint->delete(); } } @@ -174,7 +174,7 @@ function breakpoint_select_options() { $options = array(); $breakpoints = entity_load_multiple('breakpoint'); foreach ($breakpoints as $breakpoint) { - $options[$breakpoint->id()] = $breakpoint->label() . ' (' . $breakpoint->source . ' - ' . $breakpoint->sourceType . ') [' . $breakpoint->mediaQuery . ']'; + $options[$breakpoint->id()] = $breakpoint->getLabel() . ' (' . $breakpoint->getSource() . ' - ' . $breakpoint->getSourceType() . ') [' . $breakpoint->getMediaQuery() . ']'; } asort($options); return $options; diff --git a/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointInterface.php b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointInterface.php index cf62bcd..4f4e4fc 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointInterface.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointInterface.php @@ -40,4 +40,109 @@ public function isValid(); */ public static function isValidMediaQuery($media_query); + /** + * @param string + * The name of the breakpoint. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setName($name); + + /** + * @return string + * The name of the breakpoint. + */ + public function getName(); + + /** + * @param string $label + * The label the breakpoint will use. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setLabel($label); + + /** + * @return string + * The breakpoint's label. + */ + public function getLabel(); + + /** + * @param string $mediaQuery + * The media query the breakpoint will use. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setMediaQuery($mediaQuery); + + /** + * @return string + * The breakpoint's media query. + */ + public function getMediaQuery(); + + /** + * @param string $source + * The source the breakpoint will use. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setSource($source); + + /** + * @return string + * The breakpoint's source. + */ + public function getSource(); + + /** + * @param string $sourceType + * The source type the breakpoint will use. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setSourceType($sourceType); + + /** + * @return string + * The breakpoint's source type. + */ + public function getSourceType(); + + /** + * @param integer $weight + * The weight the breakpoint will be set with. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setWeight($weight); + + /** + * @return integer + * The breakpoint's weight. + */ + public function getWeight(); + + /** + * @param array $multipliers + * An array of multipliers. + * + * @return \Drupal\breakpoint\BreakpointInterface + * The modified breakpoint + */ + public function setMultipliers($multipliers); + + /** + * @return array + * The breakpoint's multipliers. + */ + public function getMultipliers(); + } diff --git a/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php b/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php index 0736131..b2d95dd 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php @@ -23,6 +23,7 @@ * @EntityType( * id = "breakpoint", * label = @Translation("Breakpoint"), + * module = "breakpoint", * controllers = { * "storage" = "Drupal\Core\Config\Entity\ConfigStorageController" * }, @@ -130,22 +131,22 @@ public function save() { // Build an id if none is set. // Since a particular name can be used by multiple theme/modules we need // to make a unique id. - if (empty($this->id)) { - $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name; + if (!$this->id()) { + $this->id = ($this->getSourceType() . '.' . $this->getSource() . '.' . $this->getName()); } // Set the label if none is set. - if (empty($this->label)) { - $this->label = $this->name; + if (!$this->getLabel()) { + $this->setLabel($this->getName()); } // Remove unused multipliers. - $this->multipliers = array_filter($this->multipliers); + $this->setMultipliers(array_filter($this->getMultipliers())); // Always add '1x' multiplier, use array_key_exists since the value might // be NULL. - if (!array_key_exists('1x', $this->multipliers)) { - $this->multipliers = array('1x' => '1x') + $this->multipliers; + if (!array_key_exists('1x', $this->getMultipliers())) { + $this->setMultipliers(array('1x' => '1x') + $this->getMultipliers()); } return parent::save(); } @@ -155,24 +156,24 @@ public function save() { */ public function isValid() { // Check for illegal values in breakpoint source type. - if (!in_array($this->sourceType, array( + 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 source. - if (preg_match('/[^0-9a-z_]+/', $this->source)) { - throw new InvalidBreakpointSourceException(format_string("Invalid value '@source' for breakpoint source property. Breakpoint source property can only contain lowercase alphanumeric characters and underscores.", array('@source' => $this->source))); + if (preg_match('/[^0-9a-z_]+/', $this->getSource())) { + throw new InvalidBreakpointSourceException(format_string("Invalid value '@source' for breakpoint source property. Breakpoint source property can only contain lowercase alphanumeric characters and underscores.", array('@source' => $this->getSource()))); } // Check for illegal characters in breakpoint names. - if (preg_match('/[^0-9a-z_\-]/', $this->name)) { - throw new InvalidBreakpointNameException(format_string("Invalid value '@name' for breakpoint name property. Breakpoint name property can only contain lowercase alphanumeric characters, underscores (_), and hyphens (-).", array('@name' => $this->name))); + if (preg_match('/[^0-9a-z_\-]/', $this->getName())) { + throw new InvalidBreakpointNameException(format_string("Invalid value '@name' for breakpoint name property. Breakpoint name property can only contain lowercase alphanumeric characters, underscores (_), and hyphens (-).", array('@name' => $this->getName()))); } - return $this::isValidMediaQuery($this->mediaQuery); + return $this::isValidMediaQuery($this->getMediaQuery()); } /** @@ -182,17 +183,37 @@ public static function isValidMediaQuery($media_query) { // Array describing all known media features and the expected value type or // an array containing the allowed values. $media_features = array( - 'width' => 'length', 'min-width' => 'length', 'max-width' => 'length', - 'height' => 'length', 'min-height' => 'length', 'max-height' => 'length', - 'device-width' => 'length', 'min-device-width' => 'length', 'max-device-width' => 'length', - 'device-height' => 'length', 'min-device-height' => 'length', 'max-device-height' => 'length', + 'width' => 'length', + 'min-width' => 'length', + 'max-width' => 'length', + 'height' => 'length', + 'min-height' => 'length', + 'max-height' => 'length', + 'device-width' => 'length', + 'min-device-width' => 'length', + 'max-device-width' => 'length', + 'device-height' => 'length', + 'min-device-height' => 'length', + 'max-device-height' => 'length', 'orientation' => array('portrait', 'landscape'), - 'aspect-ratio' => 'ratio', 'min-aspect-ratio' => 'ratio', 'max-aspect-ratio' => 'ratio', - 'device-aspect-ratio' => 'ratio', 'min-device-aspect-ratio' => 'ratio', 'max-device-aspect-ratio' => 'ratio', - 'color' => 'integer', 'min-color' => 'integer', 'max-color' => 'integer', - 'color-index' => 'integer', 'min-color-index' => 'integer', 'max-color-index' => 'integer', - 'monochrome' => 'integer', 'min-monochrome' => 'integer', 'max-monochrome' => 'integer', - 'resolution' => 'resolution', 'min-resolution' => 'resolution', 'max-resolution' => 'resolution', + 'aspect-ratio' => 'ratio', + 'min-aspect-ratio' => 'ratio', + 'max-aspect-ratio' => 'ratio', + 'device-aspect-ratio' => 'ratio', + 'min-device-aspect-ratio' => 'ratio', + 'max-device-aspect-ratio' => 'ratio', + 'color' => 'integer', + 'min-color' => 'integer', + 'max-color' => 'integer', + 'color-index' => 'integer', + 'min-color-index' => 'integer', + 'max-color-index' => 'integer', + 'monochrome' => 'integer', + 'min-monochrome' => 'integer', + 'max-monochrome' => 'integer', + 'resolution' => 'resolution', + 'min-resolution' => 'resolution', + 'max-resolution' => 'resolution', 'scan' => array('progressive', 'interlace'), 'grid' => 'integer', ); @@ -283,4 +304,132 @@ public static function isValidMediaQuery($media_query) { } throw new InvalidBreakpointMediaQueryException('Media query is empty.'); } + + /** + * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties(); + */ + public function getExportProperties() { + $names = array( + 'id', + 'uuid', + 'name', + 'label', + 'mediaQuery', + 'source', + 'sourceType', + 'weight', + 'multipliers', + ); + $properties = array(); + foreach ($names as $name) { + $properties[$name] = $this->get($name); + } + return $properties; + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + $this->set('name', $name); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getName() { + return $this->get('name'); + } + + /** + * {@inheritdoc} + */ + public function setLabel($label) { + $this->set('label', $label); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getLabel() { + return $this->get('label'); + } + + /** + * {@inheritdoc} + */ + public function setMediaQuery($mediaQuery) { + $this->set('mediaQuery', $mediaQuery); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getMediaQuery() { + return $this->get('mediaQuery'); + } + + /** + * {@inheritdoc} + */ + public function setSource($source) { + $this->set('source', $source); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getSource() { + return $this->get('source'); + } + + /** + * {@inheritdoc} + */ + public function setSourceType($sourceType) { + $this->set('sourceType', $sourceType); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getSourceType() { + return $this->get('sourceType'); + } + + /** + * {@inheritdoc} + */ + public function setWeight($weight) { + $this->set('weight', $weight); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getWeight() { + return $this->get('weight'); + } + + /** + * {@inheritdoc} + */ + public function setMultipliers($multipliers) { + $this->set('multipliers', $multipliers); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getMultipliers() { + return $this->get('multipliers'); + } + } diff --git a/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointAPITest.php b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointAPITest.php index 85d0711..5135797 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointAPITest.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointAPITest.php @@ -46,9 +46,9 @@ public function testConfigName() { $this->assertTrue($exception, 'breakpoint_config_name: An exception is thrown when an invalid sourceType is entered.'); // Try an invalid source. - $breakpoint->id = ''; - $breakpoint->sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED; - $breakpoint->source = 'custom*_module source'; + $breakpoint->enforceIsNew(); + $breakpoint->setSourceType(Breakpoint::SOURCE_TYPE_USER_DEFINED) + ->setSource('custom*_module source'); $exception = FALSE; try { @@ -60,9 +60,9 @@ public function testConfigName() { $this->assertTrue($exception, 'breakpoint_config_name: An exception is thrown when an invalid source is entered.'); // Try an invalid name (make sure there is at least once capital letter). - $breakpoint->id = ''; - $breakpoint->source = 'custom_module'; - $breakpoint->name = drupal_ucfirst($this->randomName()); + $breakpoint->enforceIsNew(); + $breakpoint->setSource('custom_module') + ->setName(drupal_ucfirst($this->randomName())); $exception = FALSE; try { @@ -74,9 +74,9 @@ public function testConfigName() { $this->assertTrue($exception, 'breakpoint_config_name: An exception is thrown when an invalid name is entered.'); // Try a valid breakpoint. - $breakpoint->id = ''; - $breakpoint->name = drupal_strtolower($this->randomName()); - $breakpoint->mediaQuery = 'all'; + $breakpoint->enforceIsNew(); + $breakpoint->setName(drupal_strtolower($this->randomName())) + ->setMediaQuery('all'); $exception = FALSE; try { @@ -86,6 +86,6 @@ public function testConfigName() { $exception = TRUE; } $this->assertFalse($exception, 'breakpoint_config_name: No exception is thrown when a valid breakpoint is passed.'); - $this->assertEqual($breakpoint->id(), Breakpoint::SOURCE_TYPE_USER_DEFINED . '.custom_module.' . $breakpoint->name, 'breakpoint_config_name: A id is set when a valid breakpoint is passed.'); + $this->assertEqual($breakpoint->id(), Breakpoint::SOURCE_TYPE_USER_DEFINED . '.custom_module.' . $breakpoint->getName(), 'breakpoint_config_name: A id is set when a valid breakpoint is passed.'); } } diff --git a/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointCRUDTest.php b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointCRUDTest.php index 4358869..ceef678 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointCRUDTest.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointCRUDTest.php @@ -42,8 +42,8 @@ public function testBreakpointCRUD() { $this->verifyBreakpoint($breakpoint, $all_breakpoints[$config_name]); // Update the breakpoint. - $breakpoint->weight = 1; - $breakpoint->multipliers['2x'] = '2x'; + $breakpoint->setWeight(1) + ->setMultipliers(array('2x' => '2x')); $breakpoint->save(); $this->verifyBreakpoint($breakpoint); diff --git a/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointTestBase.php b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointTestBase.php index 89d0cc1..4b59d3b 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointTestBase.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointTestBase.php @@ -30,22 +30,22 @@ public function setUp() { */ public function verifyBreakpoint(Breakpoint $breakpoint, Breakpoint $compare_breakpoint = NULL) { $properties = array( - 'label', - 'mediaQuery', - 'source', - 'sourceType', - 'weight', - 'multipliers', + 'Label', + 'MediaQuery', + 'Source', + 'SourceType', + 'Weight', + 'Multipliers', ); // Verify breakpoint_load(). $compare_breakpoint = is_null($compare_breakpoint) ? breakpoint_load($breakpoint->id()) : $compare_breakpoint; foreach ($properties as $property) { $t_args = array( - '%breakpoint' => $breakpoint->label(), + '%breakpoint' => $breakpoint->getLabel(), '%property' => $property, ); - $this->assertEqual($compare_breakpoint->{$property}, $breakpoint->{$property}, format_string('breakpoint_load: Proper %property for breakpoint %breakpoint.', $t_args), 'Breakpoint API'); + $this->assertEqual($compare_breakpoint->{'get' . $property}(), $breakpoint->{'get' . $property}(), format_string('breakpoint_load: Proper %property for breakpoint %breakpoint.', $t_args), 'Breakpoint API'); } } } diff --git a/core/modules/picture/lib/Drupal/picture/Entity/PictureMapping.php b/core/modules/picture/lib/Drupal/picture/Entity/PictureMapping.php index e6ce436..c8390bf 100644 --- a/core/modules/picture/lib/Drupal/picture/Entity/PictureMapping.php +++ b/core/modules/picture/lib/Drupal/picture/Entity/PictureMapping.php @@ -136,8 +136,8 @@ protected function loadAllMappings() { } // Get the mapping for the other multipliers. - if (isset($breakpoint->multipliers) && !empty($breakpoint->multipliers)) { - foreach ($breakpoint->multipliers as $multiplier => $status) { + if ($breakpoint->getMultipliers()) { + foreach ($breakpoint->getMultipliers() as $multiplier => $status) { if ($status) { $this->mappings[$breakpoint_id][$multiplier] = ''; if (isset($loaded_mappings[$breakpoint_id][$multiplier])) { diff --git a/core/modules/picture/lib/Drupal/picture/PictureMappingFormController.php b/core/modules/picture/lib/Drupal/picture/PictureMappingFormController.php index 121f6a7..53c11dd 100644 --- a/core/modules/picture/lib/Drupal/picture/PictureMappingFormController.php +++ b/core/modules/picture/lib/Drupal/picture/PictureMappingFormController.php @@ -74,7 +74,7 @@ public function form(array $form, array &$form_state) { foreach ($picture_mapping->mappings as $breakpoint_id => $mapping) { foreach ($mapping as $multiplier => $image_style) { $breakpoint = $picture_mapping->breakpointGroup->getBreakpointById($breakpoint_id); - $label = $multiplier . ' ' . $breakpoint->name . ' [' . $breakpoint->mediaQuery . ']'; + $label = $multiplier . ' ' . $breakpoint->getName() . ' [' . $breakpoint->getMediaQuery() . ']'; $form['mappings'][$breakpoint_id][$multiplier] = array( '#type' => 'select', '#title' => check_plain($label), diff --git a/core/modules/picture/lib/Drupal/picture/Plugin/Field/FieldFormatter/PictureFormatter.php b/core/modules/picture/lib/Drupal/picture/Plugin/Field/FieldFormatter/PictureFormatter.php index a28e5fb..2e033cc 100644 --- a/core/modules/picture/lib/Drupal/picture/Plugin/Field/FieldFormatter/PictureFormatter.php +++ b/core/modules/picture/lib/Drupal/picture/Plugin/Field/FieldFormatter/PictureFormatter.php @@ -136,7 +136,7 @@ public function viewElements(FieldItemListInterface $items) { $breakpoint = $picture_mapping->breakpointGroup->getBreakpointById($breakpoint_name); if ($breakpoint) { // Determine the enabled multipliers. - $multipliers = array_intersect_key($multipliers, $breakpoint->multipliers); + $multipliers = array_intersect_key($multipliers, $breakpoint->getMultipliers()); foreach ($multipliers as $multiplier => $image_style) { // Make sure the multiplier still exists. if (!empty($image_style)) { diff --git a/core/modules/picture/picture.module b/core/modules/picture/picture.module index b9b3b01..05e53bb 100644 --- a/core/modules/picture/picture.module +++ b/core/modules/picture/picture.module @@ -254,7 +254,7 @@ function theme_picture($variables) { $sources[] = array( 'src' => entity_load('image_style', $new_sources[0]['style_name'])->buildUrl($new_sources[0]['uri']), 'dimensions' => picture_get_image_dimensions($new_sources[0]), - 'media' => $breakpoint->mediaQuery, + 'media' => $breakpoint->getMediaQuery(), ); } else { @@ -266,7 +266,7 @@ function theme_picture($variables) { $sources[] = array( 'srcset' => implode(', ', $srcset), 'dimensions' => picture_get_image_dimensions($new_sources[0]), - 'media' => $breakpoint->mediaQuery, + 'media' => $breakpoint->getMediaQuery(), ); } } diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index b8b0dcb..1d109c9 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -185,14 +185,14 @@ function toolbar_pre_render($element) { // Get the configured breakpoints to switch from vertical to horizontal // toolbar presentation. - $breakpoints = entity_load('breakpoint_group', 'module.toolbar.toolbar'); - if (!empty($breakpoints)) { + $breakpoint_group = entity_load('breakpoint_group', 'module.toolbar.toolbar'); + if (!empty($breakpoint_group)) { $media_queries = array(); $media_queries['toolbar']['breakpoints'] = array_map( function ($object) { - return $object->mediaQuery; + return $object->getMediaQuery(); }, - $breakpoints->getBreakpoints() + $breakpoint_group->getBreakpoints() ); $element['#attached']['js'][] = array(