diff --git a/core/modules/breakpoint/breakpoint.api.php b/core/modules/breakpoint/breakpoint.api.php new file mode 100644 index 0000000..6b2101a --- /dev/null +++ b/core/modules/breakpoint/breakpoint.api.php @@ -0,0 +1,57 @@ +condition('breakpoint_group_id', $breakpoint_group['id']) + ->execute(); +} + +/** + * Executes code after a breakpoint group is inserted. + * + * @param array $breakpoint_group + * The breakpoint group. + */ +function hook_breakpoint_group_insert($breakpoint_group) { + db_insert('my_table') + ->fields( + array( + 'breakpoint_group_id' => $breakpoint_group['id'], + 'label' => $breakpoint_group['label'], + ) + ) + ->execute(); +} + +/** + * Executes code after a breakpoint group is updated. + * + * @param array $breakpoint_group + * The breakpoint group. + */ +function hook_breakpoint_group_update($breakpoint_group) { + db_update('my_table') + ->condition('breakpoint_group_id', $breakpoint_group['id']) + ->fields(array('label' => $breakpoint_group['label'])) + ->execute(); +} + +/** + * @} End of "addtogroup hooks". + */ diff --git a/core/modules/breakpoint/breakpoint.install b/core/modules/breakpoint/breakpoint.install new file mode 100644 index 0000000..5528dad --- /dev/null +++ b/core/modules/breakpoint/breakpoint.install @@ -0,0 +1,21 @@ +getModuleList(); $options = array(); - $breakpoint_groups = BreakpointGroup::loadMultiple(); - foreach ($breakpoint_groups as $breakpoint_group) { - $options[$breakpoint_group->id()] = $breakpoint_group->label(); + foreach ($modules as $module) { + $group_id = breakpoint_breakpoint_group_id($module); + $breakpoint_group = \Drupal::state()->get($group_id); + if ($breakpoint_group) { + $options[$group_id] = $breakpoint_group['label']; + } + } + $themes = \Drupal::service('theme_handler')->listInfo(); + foreach ($themes as $theme) { + $group_id = breakpoint_breakpoint_group_id($theme); + $breakpoint_group = \Drupal::state()->get($group_id); + if ($breakpoint_group) { + $options[$group_id] = $breakpoint_group['label']; + } } asort($options); return $options; } /** - * Load all breakpoints as select options. + * Loads all breakpoints as select options. * * @return array * An array containing breakpoints indexed by their ids. */ function breakpoint_select_options() { + $modules = \Drupal::moduleHandler()->getModuleList(); $options = array(); - $breakpoints = Breakpoint::loadMultiple(); - foreach ($breakpoints as $breakpoint) { - $options[$breakpoint->id()] = $breakpoint->label() . ' (' . $breakpoint->source . ' - ' . $breakpoint->sourceType . ') [' . $breakpoint->mediaQuery . ']'; + foreach ($modules as $module) { + $breakpoint_group = \Drupal::state()->get(breakpoint_breakpoint_group_id($module)); + if ($breakpoint_group) { + foreach ($breakpoint_group['breakpoints'] as $breakpoint) { + $options[$breakpoint['name']] = $breakpoint['label'] . ' (' . $module->info['name'] . ' - ' . $module->getType() . ') [' . $breakpoint['mediaQuery'] . ']'; + } + } + } + $themes = \Drupal::service('theme_handler')->listInfo(); + foreach ($themes as $theme) { + $breakpoint_group = \Drupal::state()->get(breakpoint_breakpoint_group_id($theme)); + if ($breakpoint_group) { + foreach ($breakpoint_group['breakpoints'] as $breakpoint) { + $options[$breakpoint['name']] = $breakpoint['label'] . ' (' . $theme->info['name'] . ' - ' . $theme->getType() . ') [' . $breakpoint['mediaQuery'] . ']'; + } + } } asort($options); return $options; } + +/** + * Implements hook_modules_installed(). + */ +function breakpoint_modules_installed($installed_modules) { + $modules = array_intersect_key(system_rebuild_module_data(), array_combine($installed_modules, $installed_modules)); + foreach ($modules as $module) { + breakpoint_save_extension_breakpoints($module); + } +} + +/** + * Implements hook_modules_uninstalled(). + */ +function breakpoint_modules_uninstalled($uninstalled_modules) { + $modules = array_intersect_key(system_rebuild_module_data(), array_combine($uninstalled_modules, $uninstalled_modules)); + foreach ($modules as $module) { + breakpoint_delete_extension_breakpoints($module); + } +} + +/** + * Implements hook_themes_enabled() + */ +function breakpoint_themes_enabled($theme_list) { + $themes = array_intersect_key(\Drupal::service('theme_handler')->rebuildThemeData(), array_combine($theme_list, $theme_list)); + foreach ($themes as $theme) { + breakpoint_save_extension_breakpoints($theme); + } +} + +/** + * Implements hook_themes_disabled() + */ +function breakpoint_themes_disabled($theme_list) { + $themes = array_intersect_key(\Drupal::service('theme_handler')->rebuildThemeData(), array_combine($theme_list, $theme_list)); + foreach ($themes as $theme) { + breakpoint_delete_extension_breakpoints($theme); + } +} + +/** + * Saves breakpoints from an extension (module or theme). + * + * @param \Drupal\Core\Extension\Extension $extension + * The extension (module or theme) to save breakpoints from. + */ +function breakpoint_save_extension_breakpoints(\Drupal\Core\Extension\Extension $extension) { + if ($extension->status && isset($extension->info['breakpoints'])) { + $breakpoints = array(); + foreach ($extension->info['breakpoints'] as $breakpoint) { + $breakpoints[$breakpoint['name']] = $breakpoint; + } + uasort($breakpoints, array('Drupal\Component\Utility\SortArray', 'sortByWeightElement')); + $breakpoint_group = array( + 'label' => $extension->info['name'], + 'id' => breakpoint_breakpoint_group_id($extension), + 'breakpoints' => $breakpoints, + ); + $action = \Drupal::state()->get($breakpoint_group['id']) ? 'update' : 'insert'; + \Drupal::state()->set($breakpoint_group['id'], $breakpoint_group); + \Drupal::moduleHandler()->invokeAll('breakpoint_group_' . $action, array($breakpoint_group)); + } +} + +/** + * Deletes breakpoints from an extension (module or theme). + * + * @param \Drupal\Core\Extension\Extension $extension + * The extension (module or theme) to delete breakpoints from. + */ +function breakpoint_delete_extension_breakpoints(\Drupal\Core\Extension\Extension $extension) { + $breakpoint_group = breakpoint_load_breakpoint_group(breakpoint_breakpoint_group_id($extension)); + \Drupal::state()->delete($breakpoint_group['id']); + \Drupal::moduleHandler()->invokeAll('breakpoint_group_delete', array($breakpoint_group)); +} + +/** + * Loads a breakpoint group. + * + * @param string $breakpoint_group_id + * The breakpoint group id. + * + * @return array + * The breakpoint group, or NULL if the breakpoint group doesn't exist. + */ +function breakpoint_load_breakpoint_group($breakpoint_group_id) { + return \Drupal::state()->get($breakpoint_group_id); +} + +/** + * Implements hook_rebuild(). + */ +function breakpoint_rebuild() { + // Delete breakpoint groups of themes or modules that no longer define them. + $modules = system_rebuild_module_data(); + foreach ($modules as $module) { + if (!isset($module->info['breakpoints'])) { + breakpoint_delete_extension_breakpoints($module); + } + } + $themes = \Drupal::service('theme_handler')->rebuildThemeData(); + foreach ($themes as $theme) { + if (!isset($theme->info['breakpoints'])) { + breakpoint_delete_extension_breakpoints($theme); + } + } + breakpoint_read_breakpoint_groups(); +} + +/** + * Reads breakpoint groups from all extensions. + */ +function breakpoint_read_breakpoint_groups() { + $modules = system_rebuild_module_data(); + foreach ($modules as $module) { + breakpoint_save_extension_breakpoints($module); + } + $themes = \Drupal::service('theme_handler')->rebuildThemeData(); + foreach ($themes as $theme) { + breakpoint_save_extension_breakpoints($theme); + } +} + +/** + * Removes all breakpoint groups. + */ +function breakpoint_remove_breakpoint_groups() { + $modules = system_rebuild_module_data(); + foreach ($modules as $module) { + breakpoint_delete_extension_breakpoints($module); + } + $themes = \Drupal::service('theme_handler')->rebuildThemeData(); + foreach ($themes as $theme) { + breakpoint_delete_extension_breakpoints($theme); + } +} + +/** + * Returns the id of the breakpoint group of the given module or theme. + * + * @param \Drupal\Core\Extension\Extension $extension + * The extension (module or theme) to get the breakpoint group id for. + */ +function breakpoint_breakpoint_group_id(\Drupal\Core\Extension\Extension $extension) { + return $extension->getType() . '.' . $extension->getName() . '.breakpointgroup'; +} diff --git a/core/modules/breakpoint/config/schema/breakpoint.schema.yml b/core/modules/breakpoint/config/schema/breakpoint.schema.yml deleted file mode 100644 index c44ef4c..0000000 --- a/core/modules/breakpoint/config/schema/breakpoint.schema.yml +++ /dev/null @@ -1,58 +0,0 @@ -# Schema for the configuration files of the Breakpoint module. -breakpoint.breakpoint.*.*.*: - type: config_entity - label: 'Defines the Breakpoint entity' - mapping: - id: - type: string - label: 'ID' - label: - type: label - label: 'Label' - name: - type: string - label: 'Machine name' - mediaQuery: - type: string - label: 'Media query' - source: - type: string - label: 'Source' - sourceType: - type: string - label: 'Source type' - weight: - type: integer - label: 'Weight' - multipliers: - type: sequence - label: 'Multipliers' - sequence: - - type: string - label: 'Multiplier' - -breakpoint.breakpoint_group.*.*.*: - type: config_entity - label: 'Breakpoint group settings' - mapping: - id: - type: string - label: 'ID' - label: - type: label - label: 'Label' - name: - type: string - label: 'Machine name' - breakpoint_ids: - type: sequence - label: 'Breakpoints' - sequence: - - type: string - label: 'Breakpoint name' - source: - type: string - label: 'Group source: theme or module name' - sourceType: - type: string - label: 'Group source type' diff --git a/core/modules/breakpoint/src/BreakpointGroupInterface.php b/core/modules/breakpoint/src/BreakpointGroupInterface.php deleted file mode 100644 index ae4f2e9..0000000 --- a/core/modules/breakpoint/src/BreakpointGroupInterface.php +++ /dev/null @@ -1,70 +0,0 @@ -id)) { - $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name; - } - return $this->id; - } - - /** - * Overrides Drupal\config\ConfigEntityBase::save(). - */ - public function save() { - // Check if everything is valid. - if (!$this->isValid()) { - throw new InvalidBreakpointException('Invalid data detected.'); - } - - // Set the label if none is set. - if (empty($this->label)) { - $this->label = $this->name; - } - - // Remove unused multipliers. - $this->multipliers = array_filter($this->multipliers); - - // 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; - } - return parent::save(); - } - - /** - * {@inheritdoc} - */ - public function isValid() { - // Check for illegal values in breakpoint 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 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))); - } - // 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))); - } - return $this::isValidMediaQuery($this->mediaQuery); - } - - /** - * {@inheritdoc} - */ - 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', - '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', - 'scan' => array('progressive', 'interlace'), - 'grid' => 'integer', - ); - if ($media_query) { - // Strip new lines and trim. - $media_query = str_replace(array("\r", "\n"), ' ', trim($media_query)); - - // Remove comments /* ... */. - $media_query = preg_replace('/\/\*[\s\S]*?\*\//', '', $media_query); - - // Check media list. - $parts = explode(',', $media_query); - foreach ($parts as $part) { - // Split on ' and ' - $query_parts = explode(' and ', trim($part)); - $media_type_found = FALSE; - foreach ($query_parts as $query_part) { - $matches = array(); - // Try to match: '(media_feature: value)' and variants. - if (preg_match('/^\(([\w\-]+)(:\s?([\w\-\.]+))?\)/', trim($query_part), $matches)) { - // Single expression like '(color)'. - if (isset($matches[1]) && !isset($matches[2])) { - if (!array_key_exists($matches[1], $media_features)) { - throw new InvalidBreakpointMediaQueryException('Invalid media feature detected.'); - } - } - // Full expression like '(min-width: 20em)'. - elseif (isset($matches[3]) && !isset($matches[4])) { - $value = trim($matches[3]); - if (!array_key_exists($matches[1], $media_features)) { - // We need to allow vendor prefixed media features and make sure - // we are future proof, so only check allowed characters. - if (!preg_match('/^[a-zA-Z0-9\:\-\\ ]+$/i', trim($matches[1]))) { - throw new InvalidBreakpointMediaQueryException('Invalid media query detected.'); - } - } - elseif (is_array($media_features[$matches[1]])) { - // Check if value is allowed. - if (!array_key_exists($value, $media_features[$matches[1]])) { - throw new InvalidBreakpointMediaQueryException('Value is not allowed.'); - } - } - elseif (isset ($media_features[$matches[1]])) { - switch ($media_features[$matches[1]]) { - case 'length': - $length_matches = array(); - // Check for a valid number and an allowed unit. - if (preg_match('/^(\-)?(\d+(?:\.\d+)?)?((?:|em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|dpi|dpcm))$/i', trim($value), $length_matches)) { - // Only -0 is allowed. - if ($length_matches[1] === '-' && $length_matches[2] !== '0') { - throw new InvalidBreakpointMediaQueryException('Invalid length detected.'); - } - // If there's a unit, a number is needed as well. - if ($length_matches[2] === '' && $length_matches[3] !== '') { - throw new InvalidBreakpointMediaQueryException('Unit found, value is missing.'); - } - } - else { - throw new InvalidBreakpointMediaQueryException('Invalid unit detected.'); - } - break; - } - } - } - } - - // Check for screen, only screen, not screen and variants. - elseif (preg_match('/^((?:only|not)?\s?)([\w\-]+)$/i', trim($query_part), $matches)) { - if ($media_type_found) { - throw new InvalidBreakpointMediaQueryException('Only one media type is allowed.'); - } - $media_type_found = TRUE; - } - // Check for (scan), (only scan), (not scan) and variants. - elseif (preg_match('/^((?:only|not)\s?)\(([\w\-]+)\)$/i', trim($query_part), $matches)) { - throw new InvalidBreakpointMediaQueryException('Invalid media query detected.'); - } - else { - // We need to allow vendor prefixed media fetures and make sure we - // are future proof, so only check allowed characters. - if (!preg_match('/^[a-zA-Z0-9\-\\ ]+$/i', trim($query_part), $matches)) { - throw new InvalidBreakpointMediaQueryException('Invalid media query detected.'); - } - } - } - } - return TRUE; - } - throw new InvalidBreakpointMediaQueryException('Media query is empty.'); - } - - /** - * {@inheritdoc} - */ - public function calculateDependencies() { - parent::calculateDependencies(); - $this->dependencies = array(); - if ($this->sourceType == static::SOURCE_TYPE_MODULE) { - $this->addDependency('module', $this->source); - } - elseif ($this->sourceType == static::SOURCE_TYPE_THEME) { - $this->addDependency('theme', $this->source); - } - return $this->dependencies; - } - -} diff --git a/core/modules/breakpoint/src/Entity/BreakpointGroup.php b/core/modules/breakpoint/src/Entity/BreakpointGroup.php deleted file mode 100644 index 0b585e2..0000000 --- a/core/modules/breakpoint/src/Entity/BreakpointGroup.php +++ /dev/null @@ -1,234 +0,0 @@ -isValid()) { - throw new InvalidBreakpointException('Invalid data detected.'); - } - parent::save(); - } - - /** - * {@inheritdoc} - */ - public function id() { - // If no ID is specified, build one from the properties that uniquely define - // this breakpoint group. - if (!isset($this->id)) { - $this->id = $this->sourceType . '.' . $this->source . '.' . $this->name; - } - return $this->id; - } - - /** - * {@inheritdoc} - */ - 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; - } - - /** - * {@inheritdoc} - */ - public function addBreakpointFromMediaQuery($name, $media_query) { - // Use the existing breakpoint if it exists. - $breakpoint = Breakpoint::load($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->breakpoint_ids), - )); - $breakpoint->save(); - } - return $this->addBreakpoints(array($breakpoint)); - } - - /** - * {@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; - } - } - } - return $this->breakpoints; - } - - /** - * {@inheritdoc} - */ - public function getBreakpointById($id) { - $breakpoints = $this->getBreakpoints(); - if (isset($breakpoints[$id])) { - return $breakpoints[$id]; - } - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function calculateDependencies() { - parent::calculateDependencies(); - - $this->dependencies = array(); - if ($this->sourceType == Breakpoint::SOURCE_TYPE_MODULE) { - $this->addDependency('module', $this->source); - } - elseif ($this->sourceType == Breakpoint::SOURCE_TYPE_THEME) { - $this->addDependency('theme', $this->source); - } - $breakpoints = $this->getBreakpoints(); - foreach ($breakpoints as $breakpoint) { - $this->addDependency('entity', $breakpoint->getConfigDependencyName()); - } - return $this->dependencies; - } - -} diff --git a/core/modules/breakpoint/src/InvalidBreakpointException.php b/core/modules/breakpoint/src/InvalidBreakpointException.php deleted file mode 100644 index 6889fa2..0000000 --- a/core/modules/breakpoint/src/InvalidBreakpointException.php +++ /dev/null @@ -1,13 +0,0 @@ -id()) : $compare_set; + $compare_set = is_null($compare_set) ? breakpoint_load_breakpoint_group($group['id']) : $compare_set; foreach ($properties as $property) { $t_args = array( - '%group' => $group->label(), + '%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[$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'); } else { $t_args = array( - '%group' => $group->label(), + '%group' => $group['label'], '%property' => $property, - '%property1' => $compare_set->{$property}, - '%property2' => $group->{$property}, + '%property1' => $compare_set[$property], + '%property2' => $group[$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[$property], $group[$property], format_string('breakpoint_group_load: Proper %property: %property1 == %property2 for breakpoint group %group.', $t_args), 'Breakpoint API'); } } - - // Ensure that the breakpoint group has the expected breakpoints. - $this->assertEqual(array_keys($compare_set->getBreakpoints()), array_keys($group->getBreakpoints())); } } diff --git a/core/modules/breakpoint/src/Tests/BreakpointTestBase.php b/core/modules/breakpoint/src/Tests/BreakpointTestBase.php deleted file mode 100644 index aac96c9..0000000 --- a/core/modules/breakpoint/src/Tests/BreakpointTestBase.php +++ /dev/null @@ -1,51 +0,0 @@ -id()) : $compare_breakpoint; - foreach ($properties as $property) { - $t_args = array( - '%breakpoint' => $breakpoint->label(), - '%property' => $property, - ); - $this->assertEqual($compare_breakpoint->{$property}, $breakpoint->{$property}, format_string('Proper %property for breakpoint %breakpoint.', $t_args), 'Breakpoint API'); - } - } -} diff --git a/core/modules/breakpoint/src/Tests/BreakpointThemeTest.php b/core/modules/breakpoint/src/Tests/BreakpointThemeTest.php index f5d3e82..6ea6a91 100644 --- a/core/modules/breakpoint/src/Tests/BreakpointThemeTest.php +++ b/core/modules/breakpoint/src/Tests/BreakpointThemeTest.php @@ -7,8 +7,6 @@ namespace Drupal\breakpoint\Tests; use Drupal\breakpoint\Tests\BreakpointGroupTestBase; -use Drupal\breakpoint\Entity\BreakpointGroup; -use Drupal\breakpoint\Entity\Breakpoint; /** * Thoroughly test the breakpoints provided by a theme. @@ -23,52 +21,55 @@ public function setUp() { } /** - * Test the breakpoints provided by a theme. + * Test the breakpoint group created for a theme. */ public function testThemeBreakpoints() { // Verify the breakpoint group for breakpoint_test_theme was created. - $breakpoint_group_obj = entity_create('breakpoint_group', array( + $breakpoint_group = array( 'label' => 'Breakpoint test theme', - 'name' => 'breakpoint_test_theme', - 'source' => 'breakpoint_test_theme', - 'sourceType' => Breakpoint::SOURCE_TYPE_THEME, - 'id' => Breakpoint::SOURCE_TYPE_THEME . '.breakpoint_test_theme.breakpoint_test_theme', - )); - $breakpoint_group_obj->addBreakpoints(entity_load_multiple('breakpoint', - array( - 'theme.breakpoint_test_theme.mobile', - 'theme.breakpoint_test_theme.narrow', - 'theme.breakpoint_test_theme.wide', - 'theme.breakpoint_test_theme.tv', + 'id' => 'theme.breakpoint_test_theme.breakpointgroup', + 'breakpoints' => array( + 'mobile' => array( + 'name' => 'mobile', + 'label' => 'mobile', + 'mediaQuery' => '(min-width: 0px)', + 'weight' => 0, + 'multipliers' => array( + '1x' => '1x', + ), + ), + 'narrow' => array( + 'name' => 'narrow', + 'label' => 'narrow', + 'mediaQuery' => '(min-width: 560px)', + 'weight' => 1, + 'multipliers' => array( + '1x' => '1x', + ), + ), + 'wide' => array( + 'name' => 'wide', + 'label' => 'wide', + 'mediaQuery' => '(min-width: 851px)', + 'weight' => 2, + 'multipliers' => array( + '1x' => '1x', + ), + ), + 'tv' => array( + 'name' => 'tv', + 'label' => 'tv', + 'mediaQuery' => 'only screen and (min-width 3456px)', + 'weight' => 3, + 'multipliers' => array( + '1x' => '1x', + ), + ), ) - )); + ); // Verify we can load this breakpoint defined by the theme. - $this->verifyBreakpointGroup($breakpoint_group_obj); - } - - /** - * Test the breakpoints defined by the custom group. - */ - public function testThemeBreakpointGroup() { - // Verify the breakpoint group 'test' was created by breakpoint_test_theme. - $breakpoint_group_obj = entity_create('breakpoint_group', array( - 'label' => 'Test Theme', - 'name' => 'test', - 'sourceType' => Breakpoint::SOURCE_TYPE_THEME, - 'source' => 'breakpoint_test_theme', - 'id' => Breakpoint::SOURCE_TYPE_THEME . '.breakpoint_test_theme.test', - )); - $breakpoint_group_obj->addBreakpoints(entity_load_multiple('breakpoint', - array( - 'theme.breakpoint_test_theme.mobile', - 'theme.breakpoint_test_theme.narrow', - 'theme.breakpoint_test_theme.wide', - ) - )); - - // Verify we can load this breakpoint defined by the theme. - $this->verifyBreakpointGroup($breakpoint_group_obj); + $this->verifyBreakpointGroup($breakpoint_group); } } diff --git a/core/modules/breakpoint/tests/src/BreakpointMediaQueryTest.php b/core/modules/breakpoint/tests/src/BreakpointMediaQueryTest.php deleted file mode 100644 index c8ab81d..0000000 --- a/core/modules/breakpoint/tests/src/BreakpointMediaQueryTest.php +++ /dev/null @@ -1,117 +0,0 @@ -assertTrue(Breakpoint::isValidMediaQuery($media_query), $media_query . ' is valid.'); - } - } - - /** - * Test invalid media queries. - */ - public function testInvalidMediaQueries() { - $media_queries = array( - '', - 'not (orientation)', - 'only (orientation)', - 'all and not all', - 'screen and (width: 0xx)', - 'screen and (width: -8xx)', - 'screen and (width: -xx)', - 'screen and (width: xx)', - 'screen and (width: px)', - 'screen and (width: -8px)', - 'screen and (width: -0.8px)', - 'screen and (height: 0xx)', - 'screen and (height: -8xx)', - 'screen and (height: -xx)', - 'screen and (height: xx)', - 'screen and (height: px)', - 'screen and (height: -8px)', - 'screen and (height: -0.8px)', - 'screen and (device-width: 0xx)', - 'screen and (device-width: -8xx)', - 'screen and (device-width: -xx)', - 'screen and (device-width: xx)', - 'screen and (device-width: px)', - 'screen and (device-width: -8px)', - 'screen and (device-width: -0.8px)', - 'screen and (device-height: 0xx)', - 'screen and (device-height: -8xx)', - 'screen and (device-height: -xx)', - 'screen and (device-height: xx)', - 'screen and (device-height: px)', - 'screen and (device-height: -8px)', - 'screen and (device-height: -0.8px)', - 'screen and (min-orientation)', - 'screen and (max-orientation)', - 'screen and (orientation: bogus)', - '(orientation: bogus)', - 'screen and (ori"entation: bogus)', - ); - - foreach ($media_queries as $media_query) { - try { - $this->assertFalse(Breakpoint::isValidMediaQuery($media_query), $media_query . ' is not valid.'); - } - catch (InvalidBreakpointMediaQueryException $e) { - $this->assertTrue(TRUE, sprintf('%s is not valid.', $media_query)); - } - } - } -} diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/breakpoint_test_theme.info.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/breakpoint_test_theme.info.yml index 779f997..25e8b4a 100644 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/breakpoint_test_theme.info.yml +++ b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/breakpoint_test_theme.info.yml @@ -4,3 +4,32 @@ description: 'Test theme for breakpoint.' version: VERSION core: 8.x 'base theme': bartik +breakpoints: + mobile: + name: mobile + label: mobile + mediaQuery: '(min-width: 0px)' + weight: 0 + multipliers: + 1x: 1x + narrow: + name: narrow + label: narrow + mediaQuery: '(min-width: 560px)' + weight: 1 + multipliers: + 1x: 1x + wide: + name: wide + label: wide + mediaQuery: '(min-width: 851px)' + weight: 2 + multipliers: + 1x: 1x + tv: + name: tv + label: tv + mediaQuery: 'only screen and (min-width: 3456px)' + weight: 3 + multipliers: + 1x: 1x diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.mobile.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.mobile.yml deleted file mode 100644 index 8e5e3c4..0000000 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.mobile.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.breakpoint_test_theme.mobile -name: mobile -label: mobile -mediaQuery: '(min-width: 0px)' -source: breakpoint_test_theme -sourceType: theme -weight: 0 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.narrow.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.narrow.yml deleted file mode 100644 index a34b582..0000000 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.narrow.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.breakpoint_test_theme.narrow -name: narrow -label: narrow -mediaQuery: '(min-width: 560px)' -source: breakpoint_test_theme -sourceType: theme -weight: 1 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.tv.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.tv.yml deleted file mode 100644 index d594ff4..0000000 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.tv.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.breakpoint_test_theme.tv -name: tv -label: tv -mediaQuery: 'only screen and (min-width: 3456px)' -source: breakpoint_test_theme -sourceType: theme -weight: 3 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.wide.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.wide.yml deleted file mode 100644 index 34e5f4f..0000000 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint.theme.breakpoint_test_theme.wide.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.breakpoint_test_theme.wide -name: wide -label: wide -mediaQuery: '(min-width: 851px)' -source: breakpoint_test_theme -sourceType: theme -weight: 2 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint_group.theme.breakpoint_test_theme.breakpoint_test_theme.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint_group.theme.breakpoint_test_theme.breakpoint_test_theme.yml deleted file mode 100644 index c335fb0..0000000 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint_group.theme.breakpoint_test_theme.breakpoint_test_theme.yml +++ /dev/null @@ -1,12 +0,0 @@ -id: theme.breakpoint_test_theme.breakpoint_test_theme -name: breakpoint_test_theme -label: 'Breakpoint test theme' -breakpoint_ids: - - theme.breakpoint_test_theme.mobile - - theme.breakpoint_test_theme.narrow - - theme.breakpoint_test_theme.wide - - theme.breakpoint_test_theme.tv -source: breakpoint_test_theme -sourceType: theme -status: true -langcode: en diff --git a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint_group.theme.breakpoint_test_theme.test.yml b/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint_group.theme.breakpoint_test_theme.test.yml deleted file mode 100644 index e074998..0000000 --- a/core/modules/breakpoint/tests/themes/breakpoint_test_theme/config/install/breakpoint.breakpoint_group.theme.breakpoint_test_theme.test.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.breakpoint_test_theme.test -name: test -label: 'Test Theme' -breakpoint_ids: - - theme.breakpoint_test_theme.mobile - - theme.breakpoint_test_theme.narrow - - theme.breakpoint_test_theme.wide -source: breakpoint_test_theme -sourceType: theme -status: true -langcode: en diff --git a/core/modules/responsive_image/responsive_image.module b/core/modules/responsive_image/responsive_image.module index 63f25e6..81648b5 100644 --- a/core/modules/responsive_image/responsive_image.module +++ b/core/modules/responsive_image/responsive_image.module @@ -5,7 +5,6 @@ * Responsive image display formatter for image fields. */ -use Drupal\breakpoint\Entity\Breakpoint; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Routing\RouteMatchInterface; use \Drupal\Core\Template\Attribute; @@ -90,7 +89,7 @@ function responsive_image_theme() { 'alt' => '', 'title' => NULL, 'attributes' => array(), - 'breakpoints' => array(), + 'mapping_id' => array(), ), ), 'responsive_image_formatter' => array( @@ -98,7 +97,7 @@ function responsive_image_theme() { 'item' => NULL, 'path' => NULL, 'image_style' => NULL, - 'breakpoints' => array(), + 'mapping_id' => array(), ), ), 'responsive_image_source' => array( @@ -126,7 +125,7 @@ function responsive_image_theme() { */ function theme_responsive_image_formatter($variables) { $item = $variables['item']; - if (!isset($variables['breakpoints']) || empty($variables['breakpoints'])) { + if (!isset($variables['mapping_id']) || empty($variables['mapping_id'])) { $image_formatter = array( '#theme' => 'image_formatter', '#item' => $item, @@ -141,7 +140,7 @@ function theme_responsive_image_formatter($variables) { '#width' => $item->width, '#height' => $item->height, '#style_name' => $variables['image_style'], - '#breakpoints' => $variables['breakpoints'], + '#mapping_id' => $variables['mapping_id'], ); if (isset($item->uri)) { $responsive_image['#uri'] = $item->uri; @@ -202,10 +201,11 @@ function theme_responsive_image($variables) { 'src' => _responsive_image_image_style_url($variables['style_name'], $variables['uri']), 'dimensions' => responsive_image_get_image_dimensions($variables), ); - + $responsive_image_mapping = entity_load('responsive_image_mapping', $variables['mapping_id']); // All breakpoints and multipliers. - foreach ($variables['breakpoints'] as $breakpoint_name => $multipliers) { - $breakpoint = Breakpoint::load($breakpoint_name); + foreach ($responsive_image_mapping->getFilteredMappings() as $breakpoint_name => $multipliers) { + $breakpoint_group = $responsive_image_mapping->getBreakpointGroup(); + $breakpoint = isset($breakpoint_group['breakpoints'][$breakpoint_name]) ? $breakpoint_group['breakpoints'][$breakpoint_name] : FALSE; if ($breakpoint) { $new_sources = array(); foreach ($multipliers as $multiplier => $image_style) { @@ -220,7 +220,7 @@ function theme_responsive_image($variables) { $sources[] = array( 'src' => _responsive_image_image_style_url($new_sources[0]['style_name'], $new_sources[0]['uri']), 'dimensions' => responsive_image_get_image_dimensions($new_sources[0]), - 'media' => $breakpoint->mediaQuery, + 'media' => $breakpoint['mediaQuery'], ); } else { @@ -232,7 +232,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['mediaQuery'], ); } } diff --git a/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php b/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php index 0780a0c..6c6ce15 100644 --- a/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php +++ b/core/modules/responsive_image/src/Entity/ResponsiveImageMapping.php @@ -65,7 +65,7 @@ class ResponsiveImageMapping extends ConfigEntityBase implements ResponsiveImage /** * The responsive image breakpoint group. * - * @var Drupal\breakpoint\Entity\BreakpointGroup + * @var array */ protected $breakpointGroup = ''; @@ -79,39 +79,14 @@ public function __construct(array $values, $entity_type) { } /** - * {@inheritdoc} - */ - public function calculateDependencies() { - parent::calculateDependencies(); - if (isset($this->breakpointGroup)) { - // @todo Implement toArray() so we do not have reload the - // entity since this property is changed in - // \Drupal\responsive_image\Entity\ResponsiveImageMapping::save(). - $breakpoint_group = \Drupal::entityManager()->getStorage('breakpoint_group')->load($this->breakpointGroup); - $this->addDependency('entity', $breakpoint_group->getConfigDependencyName()); - } - return $this->dependencies; - } - - /** * Overrides Drupal\Core\Entity::save(). */ public function save() { // Only save the keys, but return the full objects. $breakpoint_group = $this->getBreakpointGroup(); - if ($breakpoint_group && is_object($breakpoint_group)) { - $this->setBreakpointGroup($breakpoint_group->id()); - } - - // Split the breakpoint ids into their different parts, as dots as - // identifiers are not possible. - $loaded_mappings = $this->mappings; - $this->mappings = array(); - foreach ($loaded_mappings as $breakpoint_id => $mapping) { - list($source_type, $source, $name) = explode('.', $breakpoint_id); - $this->mappings[$source_type][$source][$name] = $mapping; + if ($breakpoint_group && is_array($breakpoint_group)) { + $this->setBreakpointGroup($breakpoint_group['id']); } - parent::save(); $this->loadBreakpointGroup(); $this->loadAllMappings(); @@ -133,7 +108,7 @@ public function createDuplicate() { */ protected function loadBreakpointGroup() { if ($this->getBreakpointGroup()) { - $breakpoint_group = entity_load('breakpoint_group', $this->getBreakpointGroup()); + $breakpoint_group = breakpoint_load_breakpoint_group($this->getBreakpointGroup()); $this->setBreakpointGroup($breakpoint_group); } } @@ -145,24 +120,21 @@ protected function loadAllMappings() { $loaded_mappings = $this->getMappings(); $all_mappings = array(); if ($breakpoint_group = $this->getBreakpointGroup()) { - foreach ($breakpoint_group->getBreakpoints() as $breakpoint_id => $breakpoint) { - // Get the components of the breakpoint ID to match the format of the - // configuration file. - list($source_type, $source, $name) = explode('.', $breakpoint_id); + foreach ($breakpoint_group['breakpoints'] as $breakpoint_id => $breakpoint) { // Get the mapping for the default multiplier. $all_mappings[$breakpoint_id]['1x'] = ''; - if (isset($loaded_mappings[$source_type][$source][$name]['1x'])) { - $all_mappings[$breakpoint_id]['1x'] = $loaded_mappings[$source_type][$source][$name]['1x']; + if (isset($loaded_mappings[$breakpoint_id]['1x'])) { + $all_mappings[$breakpoint_id]['1x'] = $loaded_mappings[$breakpoint_id]['1x']; } // Get the mapping for the other multipliers. - if (isset($breakpoint->multipliers) && !empty($breakpoint->multipliers)) { - foreach ($breakpoint->multipliers as $multiplier => $status) { + if (isset($breakpoint['multipliers']) && !empty($breakpoint['multipliers'])) { + foreach ($breakpoint['multipliers'] as $multiplier => $status) { if ($status) { $all_mappings[$breakpoint_id][$multiplier] = ''; - if (isset($loaded_mappings[$source_type][$source][$name][$multiplier])) { - $all_mappings[$breakpoint_id][$multiplier] = $loaded_mappings[$source_type][$source][$name][$multiplier]; + if (isset($loaded_mappings[$breakpoint_id][$multiplier])) { + $all_mappings[$breakpoint_id][$multiplier] = $loaded_mappings[$breakpoint_id][$multiplier]; } } } @@ -190,6 +162,38 @@ public function hasMappings() { /** * {@inheritdoc} */ + public function getFilteredMappings() { + $current_mappings = $this->get('mappings'); + $mappings = array(); + foreach ($current_mappings as $breakpoint_name => $multipliers) { + // Make sure there are multipliers. + if (!empty($multipliers)) { + // Make sure that the breakpoint exists and is enabled. + // @todo add the following when breakpoint->status is added again: + // $responsive_image_mapping->breakpointGroup->breakpoints[$breakpoint_name]->status + $breakpoint_group = $this->getBreakpointGroup(); + $breakpoint = isset($breakpoint_group['breakpoints'][$breakpoint_name]) ? $breakpoint_group['breakpoints'][$breakpoint_name] : FALSE; + if ($breakpoint) { + // Determine the enabled multipliers. + $multipliers = array_intersect_key($multipliers, $breakpoint['multipliers']); + foreach ($multipliers as $multiplier => $image_style) { + // Make sure the multiplier still exists. + if (!empty($image_style)) { + if (!isset($mappings[$breakpoint_name])) { + $mappings[$breakpoint_name] = array(); + } + $mappings[$breakpoint_name][$multiplier] = $image_style; + } + } + } + } + } + return $mappings; + } + + /** + * {@inheritdoc} + */ public function setMappings(array $mappings) { $this->set('mappings', $mappings); return $this; diff --git a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index 24a80d4..983c1b8 100644 --- a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -132,50 +132,24 @@ public function viewElements(FieldItemListInterface $items) { $link_file = TRUE; } - $breakpoint_styles = array(); $fallback_image_style = ''; - $responsive_image_mapping = entity_load('responsive_image_mapping', $this->getSetting('responsive_image_mapping')); - if ($responsive_image_mapping) { - foreach ($responsive_image_mapping->getMappings() as $breakpoint_name => $multipliers) { - // Make sure there are multipliers. - if (!empty($multipliers)) { - // Make sure that the breakpoint exists and is enabled. - // @todo add the following when breakpoint->status is added again: - // $responsive_image_mapping->breakpointGroup->breakpoints[$breakpoint_name]->status - $breakpoint = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_name); - if ($breakpoint) { - // Determine the enabled multipliers. - $multipliers = array_intersect_key($multipliers, $breakpoint->multipliers); - foreach ($multipliers as $multiplier => $image_style) { - // Make sure the multiplier still exists. - if (!empty($image_style)) { - // First mapping found is used as fallback. - if (empty($fallback_image_style)) { - $fallback_image_style = $image_style; - } - if (!isset($breakpoint_styles[$breakpoint_name])) { - $breakpoint_styles[$breakpoint_name] = array(); - } - $breakpoint_styles[$breakpoint_name][$multiplier] = $image_style; - } - } - } - } - } - } - // Check if the user defined a custom fallback image style. if ($this->getSetting('fallback_image_style')) { $fallback_image_style = $this->getSetting('fallback_image_style'); } // Collect cache tags to be added for each item in the field. + $responsive_image_mapping = entity_load('responsive_image_mapping', $this->getSetting('responsive_image_mapping')); $all_cache_tags = array(); if ($responsive_image_mapping) { $all_cache_tags[] = $responsive_image_mapping->getCacheTag(); - foreach ($breakpoint_styles as $breakpoint_name => $style_per_multiplier) { + foreach ($responsive_image_mapping->getFilteredMappings() as $breakpoint_name => $style_per_multiplier) { foreach ($style_per_multiplier as $multiplier => $image_style_name) { + // First mapping found is used as fallback. + if (empty($fallback_image_style)) { + $fallback_image_style = $image_style_name; + } $image_style = entity_load('image_style', $image_style_name); $all_cache_tags[] = $image_style->getCacheTag(); } @@ -203,7 +177,7 @@ public function viewElements(FieldItemListInterface $items) { ), '#item' => $item, '#image_style' => $fallback_image_style, - '#breakpoints' => $breakpoint_styles, + '#mapping_id' => $responsive_image_mapping ? $responsive_image_mapping->id() : '', '#path' => isset($uri) ? $uri : '', '#cache' => array( 'tags' => $cache_tags, diff --git a/core/modules/responsive_image/src/ResponsiveImageMappingForm.php b/core/modules/responsive_image/src/ResponsiveImageMappingForm.php index 180c75a..5e59872 100644 --- a/core/modules/responsive_image/src/ResponsiveImageMappingForm.php +++ b/core/modules/responsive_image/src/ResponsiveImageMappingForm.php @@ -67,7 +67,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['breakpointGroup'] = array( '#type' => 'select', '#title' => $this->t('Breakpoint group'), - '#default_value' => ($responsive_image_mapping->getBreakpointGroup() != '') ? $responsive_image_mapping->getBreakpointGroup()->id() : '', + '#default_value' => ($responsive_image_mapping->getBreakpointGroup() != '') ? $responsive_image_mapping->getBreakpointGroup()['id'] : '', '#options' => breakpoint_group_select_options(), '#required' => TRUE, '#description' => $description, @@ -77,8 +77,8 @@ public function form(array $form, FormStateInterface $form_state) { $image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -'); foreach ($responsive_image_mapping->getMappings() as $breakpoint_id => $mapping) { foreach ($mapping as $multiplier => $image_style) { - $breakpoint = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_id); - $label = $multiplier . ' ' . $breakpoint->name . ' [' . $breakpoint->mediaQuery . ']'; + $breakpoint = $responsive_image_mapping->getBreakpointGroup()['breakpoints'][$breakpoint_id]; + $label = $multiplier . ' ' . $breakpoint['name'] . ' [' . $breakpoint['mediaQuery'] . ']'; $form['mappings'][$breakpoint_id][$multiplier] = array( '#type' => 'select', '#title' => String::checkPlain($label), diff --git a/core/modules/responsive_image/src/ResponsiveImageMappingInterface.php b/core/modules/responsive_image/src/ResponsiveImageMappingInterface.php index bb8b535..766efe0 100644 --- a/core/modules/responsive_image/src/ResponsiveImageMappingInterface.php +++ b/core/modules/responsive_image/src/ResponsiveImageMappingInterface.php @@ -23,6 +23,14 @@ public function hasMappings(); /** + * Returns the non-empty mappings for the responsive_image mapping. + * + * @return array[] + * The responsive_imagemappings. + */ + public function getFilteredMappings(); + + /** * Sets the mappings for the responsive_image mapping. * * The array is keyed by the Breakpoint Group Id and then then by each @@ -46,7 +54,7 @@ public function getMappings(); /** * Sets the breakpoint group for the responsive_image mapping. * - * @param \Drupal\breakpoint\Entity\BreakpointGroup $breakpoint_group + * @param array $breakpoint_group * The responsive_image mappings breakpoint group. * * @return $this @@ -56,7 +64,7 @@ public function setBreakpointGroup($breakpoint_group); /** * Returns the breakpoint group for the responsive_image mapping. * - * @return \Drupal\breakpoint\Entity\BreakpointGroup + * @return array * The responsive_image mappings breakpoint group. */ public function getBreakpointGroup(); diff --git a/core/modules/responsive_image/src/Tests/ResponsiveImageAdminUITest.php b/core/modules/responsive_image/src/Tests/ResponsiveImageAdminUITest.php index ca65b1d..767e303 100644 --- a/core/modules/responsive_image/src/Tests/ResponsiveImageAdminUITest.php +++ b/core/modules/responsive_image/src/Tests/ResponsiveImageAdminUITest.php @@ -8,7 +8,6 @@ namespace Drupal\responsive_image\Tests; use Drupal\simpletest\WebTestBase; -use Drupal\breakpoint\Entity\Breakpoint; /** * Thoroughly test the administrative interface of the Responsive Image module. @@ -20,7 +19,7 @@ class ResponsiveImageAdminUITest extends WebTestBase { /** * The breakpoint group for testing. * - * @var \Drupal\breakpoint\Entity\BreakpointGroupInterface + * @var array */ protected $breakpointGroup; @@ -29,7 +28,7 @@ class ResponsiveImageAdminUITest extends WebTestBase { * * @var array */ - public static $modules = array('responsive_image'); + public static $modules = array('responsive_image', 'responsive_image_test_module'); /** * Drupal\simpletest\WebTestBase\setUp(). @@ -43,32 +42,7 @@ public function setUp() { )); $this->drupalLogin($this->admin_user); - - // Add breakpoint_group and breakpoints. - $this->breakpointGroup = entity_create('breakpoint_group', array( - 'name' => 'atestset', - 'label' => 'A test set', - 'sourceType' => Breakpoint::SOURCE_TYPE_USER_DEFINED, - )); - - $breakpoint_names = array('small', 'medium', 'large'); - for ($i = 0; $i < 3; $i++) { - $width = ($i + 1) * 200; - $breakpoint = entity_create('breakpoint', array( - 'name' => $breakpoint_names[$i], - 'mediaQuery' => "(min-width: {$width}px)", - 'source' => 'user', - 'sourceType' => Breakpoint::SOURCE_TYPE_USER_DEFINED, - 'multipliers' => array( - '1.5x' => 0, - '2x' => '2x', - ), - )); - $breakpoint->save(); - $this->breakpointGroup->addBreakpoints(array($breakpoint)); - } - $this->breakpointGroup->save(); - + $this->breakpointGroup = breakpoint_load_breakpoint_group('module.responsive_image_test_module.breakpointgroup'); } /** @@ -81,13 +55,13 @@ public function testResponsiveImageAdmin() { // Add a new responsive image mapping, our breakpoint set should be selected. $this->drupalGet('admin/config/media/responsive-image-mapping/add'); - $this->assertFieldByName('breakpointGroup', $this->breakpointGroup->id()); + $this->assertFieldByName('breakpointGroup', $this->breakpointGroup['id']); // Create a new group. $edit = array( 'label' => 'Mapping One', 'id' => 'mapping_one', - 'breakpointGroup' => $this->breakpointGroup->id(), + 'breakpointGroup' => $this->breakpointGroup['id'], ); $this->drupalPostForm('admin/config/media/responsive-image-mapping/add', $edit, t('Save')); @@ -101,32 +75,32 @@ public function testResponsiveImageAdmin() { // Edit the group. $this->drupalGet('admin/config/media/responsive-image-mapping/mapping_one'); $this->assertFieldByName('label', 'Mapping One'); - $this->assertFieldByName('breakpointGroup', $this->breakpointGroup->id()); + $this->assertFieldByName('breakpointGroup', $this->breakpointGroup['id']); // Check if the dropdows are present for the mappings. - $this->assertFieldByName('mappings[custom.user.small][1x]', ''); - $this->assertFieldByName('mappings[custom.user.small][2x]', ''); - $this->assertFieldByName('mappings[custom.user.medium][1x]', ''); - $this->assertFieldByName('mappings[custom.user.medium][2x]', ''); - $this->assertFieldByName('mappings[custom.user.large][1x]', ''); - $this->assertFieldByName('mappings[custom.user.large][2x]', ''); + $this->assertFieldByName('mappings[mobile][1x]', ''); + $this->assertFieldByName('mappings[mobile][2x]', ''); + $this->assertFieldByName('mappings[narrow][1x]', ''); + $this->assertFieldByName('mappings[narrow][2x]', ''); + $this->assertFieldByName('mappings[wide][1x]', ''); + $this->assertFieldByName('mappings[wide][2x]', ''); // Save mappings for 1x variant only. $edit = array( 'label' => 'Mapping One', - 'breakpointGroup' => $this->breakpointGroup->id(), - 'mappings[custom.user.small][1x]' => 'thumbnail', - 'mappings[custom.user.medium][1x]' => 'medium', - 'mappings[custom.user.large][1x]' => 'large', + 'breakpointGroup' => $this->breakpointGroup['id'], + 'mappings[mobile][1x]' => 'thumbnail', + 'mappings[narrow][1x]' => 'medium', + 'mappings[wide][1x]' => 'large', ); $this->drupalPostForm('admin/config/media/responsive-image-mapping/mapping_one', $edit, t('Save')); $this->drupalGet('admin/config/media/responsive-image-mapping/mapping_one'); - $this->assertFieldByName('mappings[custom.user.small][1x]', 'thumbnail'); - $this->assertFieldByName('mappings[custom.user.small][2x]', ''); - $this->assertFieldByName('mappings[custom.user.medium][1x]', 'medium'); - $this->assertFieldByName('mappings[custom.user.medium][2x]', ''); - $this->assertFieldByName('mappings[custom.user.large][1x]', 'large'); - $this->assertFieldByName('mappings[custom.user.large][2x]', ''); + $this->assertFieldByName('mappings[mobile][1x]', 'thumbnail'); + $this->assertFieldByName('mappings[mobile][2x]', ''); + $this->assertFieldByName('mappings[narrow][1x]', 'medium'); + $this->assertFieldByName('mappings[narrow][2x]', ''); + $this->assertFieldByName('mappings[wide][1x]', 'large'); + $this->assertFieldByName('mappings[wide][2x]', ''); // Delete the mapping. $this->drupalGet('admin/config/media/responsive-image-mapping/mapping_one/delete'); diff --git a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php index 27450e6..54eba7e 100644 --- a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php +++ b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php @@ -7,7 +7,6 @@ namespace Drupal\responsive_image\Tests; -use Drupal\breakpoint\Entity\Breakpoint; use Drupal\image\Tests\ImageFieldTestBase; /** @@ -24,7 +23,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase { * * @var array */ - public static $modules = array('field_ui', 'responsive_image'); + public static $modules = array('field_ui', 'responsive_image', 'responsive_image_test_module'); /** * Drupal\simpletest\WebTestBase\setUp(). @@ -47,43 +46,18 @@ public function setUp() { 'administer image styles' )); $this->drupalLogin($this->admin_user); - - // Add breakpoint_group and breakpoints. - $breakpoint_group = entity_create('breakpoint_group', array( - 'name' => 'atestset', - 'label' => 'A test set', - 'sourceType' => Breakpoint::SOURCE_TYPE_USER_DEFINED, - )); - - $breakpoint_names = array('small', 'medium', 'large'); - for ($i = 0; $i < 3; $i++) { - $width = ($i + 1) * 200; - $breakpoint = entity_create('breakpoint', array( - 'name' => $breakpoint_names[$i], - 'mediaQuery' => "(min-width: {$width}px)", - 'source' => 'user', - 'sourceType' => Breakpoint::SOURCE_TYPE_USER_DEFINED, - 'multipliers' => array( - '1.5x' => 0, - '2x' => '2x', - ), - )); - $breakpoint->save(); - $breakpoint_group->addBreakpoints(array($breakpoint)); - } - $breakpoint_group->save(); - + $breakpoint_group = breakpoint_load_breakpoint_group('module.responsive_image_test_module.breakpointgroup'); // Add responsive image mapping. $responsive_image_mapping = entity_create('responsive_image_mapping', array( 'id' => 'mapping_one', 'label' => 'Mapping One', - 'breakpointGroup' => $breakpoint_group->id(), + 'breakpointGroup' => $breakpoint_group['id'], )); $responsive_image_mapping->save(); $mappings = array(); - $mappings['custom.user.small']['1x'] = 'thumbnail'; - $mappings['custom.user.medium']['1x'] = 'medium'; - $mappings['custom.user.large']['1x'] = 'large'; + $mappings['mobile']['1x'] = 'thumbnail'; + $mappings['narrow']['1x'] = 'medium'; + $mappings['wide']['1x'] = 'large'; $responsive_image_mapping->setMappings($mappings); $responsive_image_mapping->save(); } @@ -130,7 +104,9 @@ public function _testResponsiveImageFieldFormatters($scheme) { $display_options = array( 'type' => 'responsive_image', 'module' => 'responsive_image', - 'settings' => array('image_link' => 'file'), + 'settings' => array( + 'image_link' => 'file' + ), ); $display = entity_get_display('node', 'article', 'default'); $display->setComponent($field_name, $display_options) @@ -178,9 +154,9 @@ public function _testResponsiveImageFieldFormatters($scheme) { $this->assertRaw('/styles/thumbnail/'); $this->assertRaw('/styles/medium/'); $this->assertRaw('/styles/large/'); - $this->assertRaw('media="(min-width: 200px)"'); - $this->assertRaw('media="(min-width: 400px)"'); - $this->assertRaw('media="(min-width: 600px)"'); + $this->assertRaw('media="(min-width: 0px)"'); + $this->assertRaw('media="(min-width: 560px)"'); + $this->assertRaw('media="(min-width: 851px)"'); $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); $this->assertTrue(in_array('responsive_image_mapping:mapping_one', $cache_tags)); $this->assertTrue(in_array('image_style:thumbnail', $cache_tags)); diff --git a/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.info.yml b/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.info.yml new file mode 100644 index 0000000..3a35b46 --- /dev/null +++ b/core/modules/responsive_image/tests/modules/responsive_image_test_module/responsive_image_test_module.info.yml @@ -0,0 +1,30 @@ +name: 'Responsive image test theme' +type: module +description: 'Test theme for responsive image.' +version: VERSION +core: 8.x +breakpoints: + mobile: + name: mobile + label: mobile + mediaQuery: '(min-width: 0px)' + weight: 0 + multipliers: + 1x: 1x + 2x: 2x + narrow: + name: narrow + label: narrow + mediaQuery: '(min-width: 560px)' + weight: 1 + multipliers: + 1x: 1x + 2x: 2x + wide: + name: wide + label: wide + mediaQuery: '(min-width: 851px)' + weight: 2 + multipliers: + 1x: 1x + 2x: 2x diff --git a/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.narrow.yml b/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.narrow.yml deleted file mode 100644 index 5fae446..0000000 --- a/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.narrow.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: module.toolbar.narrow -name: narrow -label: narrow -mediaQuery: 'only screen and (min-width: 16.5em)' -source: toolbar -sourceType: module -weight: 0 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.standard.yml b/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.standard.yml deleted file mode 100644 index feda076..0000000 --- a/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.standard.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: module.toolbar.standard -name: standard -label: standard -mediaQuery: 'only screen and (min-width: 38.125em)' -source: toolbar -sourceType: module -weight: 1 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.wide.yml b/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.wide.yml deleted file mode 100644 index 554b286..0000000 --- a/core/modules/toolbar/config/install/breakpoint.breakpoint.module.toolbar.wide.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: module.toolbar.wide -name: wide -label: wide -mediaQuery: 'only screen and (min-width: 61em)' -source: toolbar -sourceType: module -weight: 2 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/modules/toolbar/config/install/breakpoint.breakpoint_group.module.toolbar.toolbar.yml b/core/modules/toolbar/config/install/breakpoint.breakpoint_group.module.toolbar.toolbar.yml deleted file mode 100644 index 148cd20..0000000 --- a/core/modules/toolbar/config/install/breakpoint.breakpoint_group.module.toolbar.toolbar.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: module.toolbar.toolbar -name: toolbar -label: toolbar -breakpoint_ids: - - module.toolbar.narrow - - module.toolbar.standard - - module.toolbar.wide -source: toolbar -sourceType: module -status: true -langcode: en diff --git a/core/modules/toolbar/js/toolbar.js b/core/modules/toolbar/js/toolbar.js index 909708d..0a84773 100644 --- a/core/modules/toolbar/js/toolbar.js +++ b/core/modules/toolbar/js/toolbar.js @@ -11,9 +11,9 @@ var options = $.extend( { breakpoints: { - 'module.toolbar.narrow': '', - 'module.toolbar.standard': '', - 'module.toolbar.wide': '' + 'narrow': '', + 'standard': '', + 'wide': '' } }, drupalSettings.toolbar, @@ -156,7 +156,7 @@ */ mediaQueryChangeHandler: function (model, label, mql) { switch (label) { - case 'module.toolbar.narrow': + case 'narrow': model.set({ 'isOriented': mql.matches, 'isTrayToggleVisible': false @@ -168,12 +168,12 @@ model.set({'orientation': 'vertical'}, {validate: true}); } break; - case 'module.toolbar.standard': + case 'standard': model.set({ 'isFixed': mql.matches }); break; - case 'module.toolbar.wide': + case 'wide': model.set({ 'orientation': ((mql.matches) ? 'horizontal' : 'vertical') }, {validate: true}); diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index e07e8f9..c0c5e95 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'); + $breakpoints = breakpoint_load_breakpoint_group('module.toolbar.breakpointgroup'); if (!empty($breakpoints)) { $media_queries = array(); $media_queries['toolbar']['breakpoints'] = array_map( - function ($object) { - return $object->mediaQuery; + function ($breakpoint) { + return $breakpoint['mediaQuery']; }, - $breakpoints->getBreakpoints() + $breakpoints['breakpoints'] ); $element['#attached']['js'][] = array( diff --git a/core/themes/bartik/bartik.info.yml b/core/themes/bartik/bartik.info.yml index bd0bf89..657def1 100644 --- a/core/themes/bartik/bartik.info.yml +++ b/core/themes/bartik/bartik.info.yml @@ -38,3 +38,26 @@ regions: settings: shortcut_module_link: false + +breakpoints: + mobile: + name: mobile + label: mobile + mediaQuery: '(min-width: 0px)' + weight: 0 + multipliers: + 1x: 1x + narrow: + name: narrow + label: narrow + mediaQuery: 'all and (min-width: 560px) and (max-width: 850px)' + weight: 1 + multipliers: + 1x: 1x + wide: + name: wide + label: wide + mediaQuery: 'all and (min-width: 851px)' + weight: 2 + multipliers: + 1x: 1x diff --git a/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.mobile.yml b/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.mobile.yml deleted file mode 100644 index 7add4c0..0000000 --- a/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.mobile.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.bartik.mobile -name: mobile -label: mobile -mediaQuery: '(min-width: 0px)' -source: bartik -sourceType: theme -weight: 0 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.narrow.yml b/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.narrow.yml deleted file mode 100644 index 0736ab6..0000000 --- a/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.narrow.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.bartik.narrow -name: narrow -label: narrow -mediaQuery: 'all and (min-width: 560px) and (max-width: 850px)' -source: bartik -sourceType: theme -weight: 1 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.wide.yml b/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.wide.yml deleted file mode 100644 index d43f1ae..0000000 --- a/core/themes/bartik/config/install/breakpoint.breakpoint.theme.bartik.wide.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.bartik.wide -name: wide -label: wide -mediaQuery: 'all and (min-width: 851px)' -source: bartik -sourceType: theme -weight: 2 -multipliers: - 1x: 1x -status: true -langcode: en diff --git a/core/themes/bartik/config/install/breakpoint.breakpoint_group.theme.bartik.bartik.yml b/core/themes/bartik/config/install/breakpoint.breakpoint_group.theme.bartik.bartik.yml deleted file mode 100644 index a7d85fc..0000000 --- a/core/themes/bartik/config/install/breakpoint.breakpoint_group.theme.bartik.bartik.yml +++ /dev/null @@ -1,11 +0,0 @@ -id: theme.bartik.bartik -name: bartik -label: Bartik -breakpoint_ids: - - theme.bartik.mobile - - theme.bartik.narrow - - theme.bartik.wide -source: bartik -sourceType: theme -status: true -langcode: en