diff --git a/core/modules/breakpoint/breakpoint.module b/core/modules/breakpoint/breakpoint.module index be06abd..b2caa0b 100644 --- a/core/modules/breakpoint/breakpoint.module +++ b/core/modules/breakpoint/breakpoint.module @@ -80,7 +80,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..0902453 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointInterface.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointInterface.php @@ -40,4 +40,137 @@ public function isValid(); */ public static function isValidMediaQuery($media_query); + /** + * Sets the name of the breakpoint. + * + * @param string + * The name of the breakpoint. + * + * @return self + * The modified breakpoint + */ + public function setName($name); + + /** + * Returns the name from the breakpoint. + * + * @return string + * The name of the breakpoint. + */ + public function getName(); + + /** + * Sets the label of the breakpoint. + * + * @param string $label + * The label the breakpoint will use. + * + * @return self + * The modified breakpoint + */ + public function setLabel($label); + + /** + * Returns the label of the breakpoint. + * + * @return string + * The breakpoint's label. + */ + public function getLabel(); + + /** + * Sets the mediaquery of the breakpoint. + * + * @param string $mediaQuery + * The media query the breakpoint will use. + * + * @return self + * The modified breakpoint + */ + public function setMediaQuery($mediaQuery); + + /** + * Returns the mediaquery of the breakpoint. + * + * @return string + * The breakpoint's media query. + */ + public function getMediaQuery(); + + /** + * Sets the source of the breakpoint. + * + * @param string $source + * The source the breakpoint will use. + * + * @return self + * The modified breakpoint + */ + public function setSource($source); + + /** + * Returns the source of the breakpoint. + * + * @return string + * The breakpoint's source. + */ + public function getSource(); + + /** + * Sets the sourcetype of the breakpoint. + * + * @param string $sourceType + * The source type the breakpoint will use. + * + * @return self + * The modified breakpoint + */ + public function setSourceType($sourceType); + + /** + * Returns the sourcetype of the breakpoint. + * + * @return string + * The breakpoint's source type. + */ + public function getSourceType(); + + /** + * Returns the weight of the breakpoint. + * + * @return integer + * The breakpoint's weight. + */ + public function getWeight(); + + /** + * Sets the weight of the breakpoint. + * + * @param integer $weight + * The weight the breakpoint will be set with. + * + * @return self + * The modified breakpoint + */ + public function setWeight($weight); + + /** + * Returns the multipliers of the breakpoint. + * + * @return array + * The breakpoint's multipliers. + */ + public function getMultipliers(); + + /** + * Sets the multipliers of the breakpoint. + * + * @param array $multipliers + * An array of multipliers. + * + * @return self + * The modified breakpoint + */ + public function setMultipliers($multipliers); + } diff --git a/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php b/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php index eab45d9..bc0b504 100644 --- a/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/Entity/Breakpoint.php @@ -22,6 +22,7 @@ * @ConfigEntityType( * id = "breakpoint", * label = @Translation("Breakpoint"), + * module = "breakpoint", * entity_keys = { * "id" = "id", * "label" = "label" @@ -57,7 +58,7 @@ class Breakpoint extends ConfigEntityBase implements BreakpointInterface { * * @var string */ - public $name; + protected $name; /** * The breakpoint label. @@ -71,14 +72,14 @@ class Breakpoint extends ConfigEntityBase implements BreakpointInterface { * * @var string */ - public $mediaQuery = ''; + protected $mediaQuery = ''; /** * The breakpoint source. * * @var string */ - public $source = 'user'; + protected $source = 'user'; /** * The breakpoint source type. @@ -89,21 +90,21 @@ class Breakpoint extends ConfigEntityBase implements BreakpointInterface { * Breakpoint::SOURCE_TYPE_MODULE * Breakpoint::SOURCE_TYPE_USER_DEFINED */ - public $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED; + protected $sourceType = Breakpoint::SOURCE_TYPE_USER_DEFINED; /** * The breakpoint weight. * * @var weight */ - public $weight = 0; + protected $weight = 0; /** * The breakpoint multipliers. * * @var multipliers */ - public $multipliers = array(); + protected $multipliers = array(); /** * Overrides Drupal\config\ConfigEntityBase::save(). @@ -117,22 +118,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(); } @@ -142,24 +143,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()); } /** @@ -169,17 +170,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', ); @@ -286,4 +307,131 @@ public function calculateDependencies() { return $this->dependencies; } + /** + * 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/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php b/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php index 658ad5d..e314b4c 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Entity/ResponsiveImageMapping.php @@ -155,8 +155,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[$source_type][$source][$name][$multiplier])) { diff --git a/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index 8f935c0..a692bb6 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -138,7 +138,7 @@ public function viewElements(FieldItemListInterface $items) { $breakpoint = $responsive_image_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/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php b/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php index d27db52..81c6cf7 100644 --- a/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php +++ b/core/modules/responsive_image/lib/Drupal/responsive_image/ResponsiveImageMappingFormController.php @@ -74,7 +74,7 @@ public function form(array $form, array &$form_state) { foreach ($responsive_image_mapping->mappings as $breakpoint_id => $mapping) { foreach ($mapping as $multiplier => $image_style) { $breakpoint = $responsive_image_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/responsive_image/responsive_image.module b/core/modules/responsive_image/responsive_image.module index 6865a81..2fe88dc 100644 --- a/core/modules/responsive_image/responsive_image.module +++ b/core/modules/responsive_image/responsive_image.module @@ -245,7 +245,7 @@ function theme_responsive_image($variables) { $sources[] = array( 'src' => entity_load('image_style', $new_sources[0]['style_name'])->buildUrl($new_sources[0]['uri']), 'dimensions' => responsive_image_get_image_dimensions($new_sources[0]), - 'media' => $breakpoint->mediaQuery, + 'media' => $breakpoint->getMediaQuery(), ); } else { @@ -257,7 +257,7 @@ function theme_responsive_image($variables) { $sources[] = array( 'srcset' => implode(', ', $srcset), 'dimensions' => responsive_image_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 feadc08c..7569c6e 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -175,14 +175,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(