diff -u b/core/modules/breakpoint/breakpoint.module b/core/modules/breakpoint/breakpoint.module --- b/core/modules/breakpoint/breakpoint.module +++ b/core/modules/breakpoint/breakpoint.module @@ -33,30 +33,8 @@ $themes = list_themes(); foreach ($theme_list as $theme_key) { if ($themes[$theme_key]->status) { - $theme_breakpoints = breakpoint_get_theme_breakpoint_list($theme_key); - if (!empty($theme_breakpoints)) { - if ($breakpoint_group = BreakpointGroup::loadFromTheme($theme_key, $themes[$theme_key]->info['name'], $theme_breakpoints)) { - $breakpoint_group->save(); - $uri = $breakpoint_group->uri(); - if ($uri) { - $uri_options = $uri; - unset($uri_options['path']); - $uri = $uri['path']; - } - $message = t('The breakpoints from theme %theme are imported.', array( - '%theme' => check_plain($themes[$theme_key]->info['name']), - )); - if (module_exists('breakpoint_ui') && $uri) { - $message .= '

' . l(t('A new breakpoint group is created for theme %theme.', array( - '%theme' => check_plain($themes[$theme_key]->info['name']), - )), $uri, $uri_options); - } - drupal_set_message($message, 'status'); - } - } - - // Import custom groups. - _breakpoint_import_breakpoint_groups($theme_key, Breakpoint::SOURCE_TYPE_THEME); + $media_queries = breakpoint_get_theme_media_queries($theme_key); + _breakpoint_import_media_queries($theme_key, $themes[$theme_key]->info['name'], Breakpoint::SOURCE_TYPE_THEME, $media_queries); } } } @@ -83,30 +61,8 @@ */ function breakpoint_modules_enabled($modules) { foreach ($modules as $module) { - $module_breakpoints = breakpoint_get_module_breakpoint_list($module); - if (!empty($module_breakpoints)) { - if ($breakpoint_group = BreakpointGroup::loadFromModule($module, $module, $module_breakpoints)) { - $breakpoint_group->save(); - $uri = $breakpoint_group->uri(); - if ($uri) { - $uri_options = $uri; - unset($uri_options['path']); - $uri = $uri['path']; - } - $message = t('The breakpoints from module %module are imported.', array( - '%module' => $module, - )); - if (module_exists('breakpoint_ui') && $uri) { - $message .= '

' . l(t('A new breakpoint group is created for module %module.', array( - '%module' => $module, - )), $uri, $uri_options); - } - drupal_set_message($message, 'status'); - } - } - - // Import custom groups. - _breakpoint_import_breakpoint_groups($module, Breakpoint::SOURCE_TYPE_MODULE); + $media_queries = breakpoint_get_module_media_queries($module); + _breakpoint_import_media_queries($module, $module, Breakpoint::SOURCE_TYPE_MODULE, $media_queries); } } @@ -123,6 +79,44 @@ } /** + * Import media queries from a theme or module and create a default group. + * + * @param string $id + * Name of the breakpoint group. + * @param string $label + * Human readable name of the breakpoint group. + * @param string $sourceType + * Either Breakpoint::SOURCE_TYPE_THEME or Breakpoint::SOURCE_TYPE_MODULE. + * @param array $media_queries + * Array of media queries keyed by id. + */ +function _breakpoint_import_media_queries($id, $label, $source_type, $media_queries) { + if (!empty($media_queries)) { + if ($breakpoint_group = BreakpointGroup::ImportMediaQueries($id, $label, $source_type, $media_queries)) { + $breakpoint_group->save(); + $uri = $breakpoint_group->uri(); + if ($uri) { + $uri_options = $uri; + unset($uri_options['path']); + $uri = $uri['path']; + } + $message = t('The breakpoints from %label are imported.', array( + '%label' => $label, + )); + if (module_exists('breakpoint_ui') && $uri) { + $message .= '

' . l(t('A new breakpoint group is created for %label.', array( + '%label' => $label, + )), $uri, $uri_options); + } + drupal_set_message($message, 'status'); + } + } + + // Import custom groups. + _breakpoint_import_breakpoint_groups($id, $source_type); +} + +/** * Import breakpoint groups from theme or module. * * @param string $group_id @@ -136,7 +130,7 @@ $breakpoint_groups = config($source . '.breakpoint_groups'); if ($breakpoint_groups) { foreach ($breakpoint_groups->get() as $id => $data) { - // Breakpoints is mandatory. + // Breakpoints is mandatory, extra check since this is coming from config. if (isset($data['breakpoints']) && !empty($data['breakpoints'])) { if ($breakpoint_group = BreakpointGroup::ImportBreakpointGroup($source, $source_type, $id, isset($data['label']) ? $data['label'] : drupal_ucfirst($data[$id]), $data['breakpoints'])) { $breakpoint_group->save(); @@ -148,6 +142,8 @@ /** * Remove breakpoints from all disabled themes or uninstalled modules. + * The source type has to match the original source type, otherwise the group + * will not be deleted. * * @param array $ids * Id's of the breakpoint group. @@ -159,18 +155,20 @@ $breakpoint_groups = entity_load_multiple('breakpoint_group', $ids); foreach ($breakpoint_groups as $breakpoint_group) { if ($breakpoint_group->sourceType == $source_type) { - // delete the default group. + // Delete the automatically created breakpoint group. $breakpoint_group->delete(); - // delete all breakpoints defined by this theme/module. + // Get all breakpoints defined by this theme/module. $names = drupal_container()->get('config.storage')->listAll('breakpoint.breakpoint.' . $source_type . '.' . $breakpoint_group->id() . '.'); $entity_info = entity_get_info('breakpoint'); + // Remove the breakpoint.breakpoint part of the names, before loading. foreach ($names as &$name) { $name = drupal_substr($name, drupal_strlen($entity_info['config prefix']) + 1); } $breakpoints = entity_load_multiple('breakpoint', $names); + // Make sure we only delete breakpoints defined by this theme/module. foreach ($breakpoints as $breakpoint) { if ($breakpoint->sourceType == $source_type && $breakpoint->source = $breakpoint_group->id) { $breakpoint->delete(); @@ -178,10 +176,11 @@ } } } + // Delete groups defined by a module/theme even if that module/theme didn't // define any breakpoints. foreach ($ids as $id) { - // delete all breakpoint groups defined by the theme or module. + // Delete all breakpoint groups defined by the theme or module. _breakpoint_delete_breakpoint_groups($id, $source_type); } } @@ -205,32 +204,6 @@ } /** - * Load general settings. - * - * @return array - * array containing general breakpoint settings. - */ -function breakpoint_settings() { - $config = config('breakpoint'); - if ($config->isNew()) { - return FALSE; - } - return (object)$config->get(); -} - -/** - * Save multipliers to settings. - * - * @param array $multipliers - * array containing multipliers. - */ -function breakpoint_settings_save_multipliers($multipliers) { - $config = config('breakpoint'); - $config->set('multipliers', $multipliers); - $config->save(); -} - -/** * Reload breakpoint groups as they were defined in the theme. * * @param string $theme_key @@ -246,9 +219,9 @@ $themes = list_themes(); if (isset($themes[$theme_key]) && $themes[$theme_key]->status) { - $theme_breakpoints = breakpoint_get_theme_breakpoint_list($theme_key); + $theme_breakpoints = breakpoint_get_theme_media_queries($theme_key); if (!empty($theme_breakpoints)) { - return BreakpointGroup::loadFromTheme($theme_key, $themes[$theme_key]->info['name'], $theme_breakpoints); + return BreakpointGroup::ImportMediaQueries($theme_key, $themes[$theme_key]->info['name'], Breakpoint::SOURCE_TYPE_THEME, $theme_breakpoints); } } return FALSE; @@ -263,7 +236,7 @@ * @return array * An array of breakpoints in the form $breakpoint['name'] = 'media query'. */ -function breakpoint_get_theme_breakpoint_list($theme_key) { +function breakpoint_get_theme_media_queries($theme_key) { $themes = list_themes(); if (!isset($themes[$theme_key])) { return array(); @@ -285,7 +258,7 @@ * @return array * An array of breakpoints in the form $breakpoint['name'] = 'media query'. */ -function breakpoint_get_module_breakpoint_list($module) { +function breakpoint_get_module_media_queries($module) { if (!module_exists($module)) { return array(); } @@ -371,7 +344,7 @@ * @return array * An array containing breakpoint group labels indexed by their ids. */ -function breakpoint_group_select_options() { +function breakpoint_group_labels() { $options = array(); $breakpoint_groups = entity_load_multiple('breakpoint_group'); foreach ($breakpoint_groups as $breakpoint_group) { @@ -387,7 +360,7 @@ * @return array * An array containing breakpoints indexed by their ids. */ -function breakpoint_select_options() { +function breakpoint_labels() { $options = array(); $breakpoints = entity_load_multiple('breakpoint'); foreach ($breakpoints as $breakpoint) { diff -u b/core/modules/breakpoint/lib/Drupal/breakpoint/Breakpoint.php b/core/modules/breakpoint/lib/Drupal/breakpoint/Breakpoint.php --- b/core/modules/breakpoint/lib/Drupal/breakpoint/Breakpoint.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/Breakpoint.php @@ -60,6 +60,8 @@ /** * The original media query. + * This is tracked separately, because a user can override a single breakpoint + * and reloading the media query from the theme/module is expensive. * * @var string */ @@ -123,20 +125,20 @@ */ public function save() { if (empty($this->id)) { - $this->id = $this->buildConfigName(); + $this->id = $this->getConfigName(); } if (empty($this->label)) { $this->label = drupal_ucfirst($this->name); } - // Check the media query. + // Check if everything is valid. if (!$this->isValid()) { - throw new Exception(t('Invalid media query detected.')); + throw new Exception(t('Invalid data detected.')); } // Remove ununsed multipliers. $this->multipliers = array_filter($this->multipliers); - // Add '1x' multiplier. + // Always add '1x' multiplier. if (!array_key_exists('1x', $this->multipliers)) { $this->multipliers = array('1x' => '1x') + $this->multipliers; } @@ -147,44 +149,7 @@ * Get config name. */ public function getConfigName() { - return $this->sourceType - . '.' . $this->source - . '.' . $this->name; - } - - /** - * Build config name. - */ - protected function buildConfigName() { - // Check for illegal values in breakpoint source type. - if (!in_array($this->sourceType, array( - Breakpoint::SOURCE_TYPE_CUSTOM, - Breakpoint::SOURCE_TYPE_MODULE, - Breakpoint::SOURCE_TYPE_THEME) - )) { - throw new Exception( - t( - "Expected one of '@custom', '@module' or '@theme' for breakpoint sourceType property but got '@sourcetype'.", - array( - '@custom' => Breakpoint::SOURCE_TYPE_CUSTOM, - '@module' => Breakpoint::SOURCE_TYPE_MODULE, - '@theme' => Breakpoint::SOURCE_TYPE_THEME, - '@sourcetype' => $this->sourceType, - ) - ) - ); - } - // Check for illegal characters in breakpoint source. - if (preg_match('/[^a-z_]+/', $this->source)) { - throw new Exception(t("Invalid value '@source' for breakpoint source property. Breakpoint source property can only contain lowercase letters and underscores.", array('@source' => $this->source))); - } - // Check for illegal characters in breakpoint names. - if (preg_match('/[^0-9a-z_\-]/', $this->name)) { - throw new Exception(t("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->sourceType - . '.' . $this->source - . '.' . $this->name; + return $this->sourceType . '.' . $this->source . '.' . $this->name; } /** @@ -253,6 +218,24 @@ * @see isValidMediaQuery() */ public function isValid() { + // Check for illegal values in breakpoint source type. + if (!in_array($this->sourceType, array( + Breakpoint::SOURCE_TYPE_CUSTOM, + Breakpoint::SOURCE_TYPE_MODULE, + Breakpoint::SOURCE_TYPE_THEME) + )) { + throw new Exception(format_string('Invalid source type @source_type', array( + '@sourcetype' => $this->sourceType, + ))); + } + // Check for illegal characters in breakpoint source. + if (preg_match('/[^a-z_]+/', $this->source)) { + throw new Exception(t("Invalid value '@source' for breakpoint source property. Breakpoint source property can only contain lowercase letters and underscores.", array('@source' => $this->source))); + } + // Check for illegal characters in breakpoint names. + if (preg_match('/[^0-9a-z_\-]/', $this->name)) { + throw new Exception(t("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); } diff -u b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointGroup.php b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointGroup.php --- b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointGroup.php +++ b/core/modules/breakpoint/lib/Drupal/breakpoint/BreakpointGroup.php @@ -192,40 +192,6 @@ } /** - * Load breakpoints from a theme and build a default group. - * - * @param string $theme_key - * The name of the theme. - * @param string $name - * The human readable name of the theme. - * @param array $media_queries - * Array of media queries keyed by id. - * - * @return \Drupal\breakpoint\BreakpointGroup|false - * Return the new breakpoint group containing all breakpoints. - */ - public static function loadFromTheme($theme_key, $name, $media_queries) { - return BreakpointGroup::loadFromThemeOrModule($theme_key, $name, Breakpoint::SOURCE_TYPE_THEME, $media_queries); - } - - /** - * Load breakpoints from a module and build a default group. - * - * @param string $module - * The name of the theme. - * @param string $name - * The human readable name of the module. - * @param array $media_queries - * Array of media queries keyed by id. - * - * @return \Drupal\breakpoint\BreakpointGroup|false - * Return the new breakpoint group containing all breakpoints. - */ - public static function loadFromModule($module, $name, $media_queries) { - return BreakpointGroup::loadFromThemeOrModule($module, $name, Breakpoint::SOURCE_TYPE_MODULE, $media_queries); - } - - /** * Load breakpoints from a theme/module and build a default group. * * @param string $id @@ -239,11 +205,8 @@ * * @return \Drupal\breakpoint\BreakpointGroup|false * Return the new breakpoint group containing all breakpoints. - * - * @see Drupal\breakpoint\BreakpointGroup::loadFromTheme() - * @see Drupal\breakpoint\BreakpointGroup::loadFromModule() */ - protected static function loadFromThemeOrModule($id, $label, $source_type, $media_queries) { + public static function ImportMediaQueries($id, $label, $source_type, $media_queries) { $breakpoint_group = entity_load('breakpoint_group', $source_type . '.' . $id); /* @var $breakpoint_group \Drupal\breakpoint\BreakpointGroup */ if (!$breakpoint_group) { reverted: --- b/core/modules/breakpoint/lib/Drupal/breakpoint/Tests/BreakpointApiTest.php +++ /dev/null @@ -1,88 +0,0 @@ - 'Breakpoint general API functions', - 'description' => 'Test general API functions of the breakpoint module.', - 'group' => 'Breakpoint', - ); - } - - /** - * Test Breakpoint::buildConfigName(). - */ - public function testConfigName() { - $breakpoint = entity_create('breakpoint', array( - 'label' => drupal_strtolower($this->randomName()), - 'source' => 'custom_module', - // Try an invalid sourceType. - 'sourceType' => 'oops', - )); - - try { - $breakpoint->save(); - } - catch (Exception $e) { - $exception = TRUE; - } - $this->assertTrue($exception, t('breakpoint_config_name: An exception is thrown when an invalid sourceType is entered.')); - $this->assertEqual((string) $breakpoint->id(), '', t('breakpoint_config_name: No id is set when an invalid sourceType is entered.')); - - // Try an invalid source. - $breakpoint->sourceType = Breakpoint::SOURCE_TYPE_CUSTOM; - $breakpoint->source = 'custom*_module source'; - $exception = FALSE; - try { - $breakpoint->save(); - } - catch (Exception $e) { - $exception = TRUE; - } - $this->assertTrue($exception, t('breakpoint_config_name: An exception is thrown when an invalid source is entered.')); - $this->assertEqual((string) $breakpoint->id(), '', t('breakpoint_config_name: No id is set when an invalid sourceType is entered.')); - - // Try an invalid name (make sure there is at least once capital letter). - $breakpoint->source = 'custom_module'; - $breakpoint->name = drupal_ucfirst($this->randomName()); - $exception = FALSE; - try { - $breakpoint->save(); - } - catch (Exception $e) { - $exception = TRUE; - } - $this->assertTrue($exception, t('breakpoint_config_name: An exception is thrown when an invalid name is entered.')); - $this->assertEqual((string) $breakpoint->id(), '', t('breakpoint_config_name: No id is set when an invalid sourceType is entered.')); - - // Try a valid breakpoint. - $breakpoint->name = drupal_strtolower($this->randomName()); - $breakpoint->mediaQuery = 'all'; - $exception = FALSE; - try { - $breakpoint->save(); - } - catch (Exception $e) { - $exception = TRUE; - } - $this->assertFalse($exception, t('breakpoint_config_name: No exception is thrown when a valid breakpoint is passed.')); - $this->assertEqual($breakpoint->id(), Breakpoint::SOURCE_TYPE_CUSTOM . '.custom_module.' . $breakpoint->name, t('breakpoint_config_name: A id is set when a valid breakpoint is passed.')); - } -}