183 lines
7.6 KiB
Plaintext
183 lines
7.6 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Manage breakpoints and breakpoint groups for responsive designs.
|
|
*/
|
|
|
|
use Drupal\breakpoint\Entity\Breakpoint;
|
|
use Drupal\breakpoint\Entity\BreakpointGroup;
|
|
use Drupal\Core\Config\Entity\ConfigStorageController;
|
|
|
|
/**
|
|
* Implements hook_help().
|
|
*/
|
|
function breakpoint_help($path, $arg) {
|
|
switch ($path) {
|
|
case 'admin/help#breakpoint':
|
|
$output = '';
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
|
$output .= '<p>' . t('The Breakpoints module keeps track of the height, width, and resolution breakpoints where a responsive design needs to change in order to respond to different devices being used to view the site.') . '</p>';
|
|
$output .= '<h3>' . t('Uses') . '</h3>';
|
|
$output .= '<dl>';
|
|
$output .= '<dt>' . t('Defining breakpoints') . '</dt>';
|
|
$output .= '<dd><p>' . t('The Breakpoint module can be used by themes and other modules to define breakpoints, which separate the height or width of viewports (screens, printers, and other media output types) into steps. For instance, a width breakpoint of 40em creates two steps: one for widths up to 40em and one for widths above 40em. Breakpoints can be used to define when layouts should shift from one form to another, when images should be resized, and other changes that need to respond to changes in viewport height or width.') . '</p>';
|
|
$output .= '<p>' . t('<a href="http://www.w3.org/TR/css3-mediaqueries/">Media queries</a> are a formal way to encode breakpoints. For instance, a width breakpoint at 40em would be written as the media query "(min-width: 40em)". Breakpoints are really just media queries with some additional meta-data, such as a name and multiplier information.') . '</p></dd>';
|
|
$output .= '<dt>' . t('Assigning resolution multipliers to breakpoints') . '</dt>';
|
|
$output .= '<dd>' . t('Multipliers are a measure of the viewport\'s device resolution, defined to be the ratio between the physical pixel size of the active device and the <a href="http://en.wikipedia.org/wiki/Device_independent_pixel">device-independent pixel</a> size. The Breakpoint module defines multipliers of 1, 1.5, and 2; when defining breakpoints, modules and themes can define which multipliers apply to each breakpoint.') . '</dd>';
|
|
$output .= '<dt>' . t('Defining breakpoint groups') . '</dt>';
|
|
$output .= '<dd>' . t('Breakpoints can be organized into groups. Modules and themes should use groups to separate out breakpoints that are meant to be used for different purposes, such as breakpoints for layouts or breakpoints for image sizing.') . '</dd>';
|
|
$output .= '</dl>';
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_themes_disabled().
|
|
*
|
|
* @param array $theme_list
|
|
* An array of theme names.
|
|
*
|
|
* @see _breakpoint_delete_breakpoints()
|
|
*
|
|
* @todo: This should be removed if https://drupal.org/node/1813110 is resolved.
|
|
*/
|
|
function breakpoint_themes_disabled($theme_list) {
|
|
_breakpoint_delete_breakpoints($theme_list, Breakpoint::SOURCE_TYPE_THEME);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_modules_uninstalled().
|
|
*
|
|
* @param array $modules
|
|
* An array of the modules that were uninstalled.
|
|
*
|
|
* @see _breakpoint_delete_breakpoints()
|
|
*
|
|
* @todo: This should be removed if https://drupal.org/node/1813110 is resolved.
|
|
*/
|
|
function breakpoint_modules_uninstalled($modules) {
|
|
_breakpoint_delete_breakpoints($modules, Breakpoint::SOURCE_TYPE_MODULE);
|
|
}
|
|
|
|
/**
|
|
* 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. All groups created by the theme or module will be
|
|
* deleted as well.
|
|
*
|
|
* @param array $list
|
|
* A list of modules or themes that are disabled.
|
|
* @param string $source_type
|
|
* Either Breakpoint::SOURCE_TYPE_THEME or Breakpoint::SOURCE_TYPE_MODULE.
|
|
*/
|
|
function _breakpoint_delete_breakpoints($list, $source_type) {
|
|
$ids = config_get_storage_names_with_prefix('breakpoint.breakpoint_group.' . $source_type . '.');
|
|
$entity_manager = \Drupal::entityManager();
|
|
$entity_info = $entity_manager->getDefinition('breakpoint_group');
|
|
|
|
// Remove the breakpoint.breakpoint part of the breakpoint identifier.
|
|
foreach ($ids as &$id) {
|
|
$id = ConfigStorageController::getIDFromConfigName($id, $entity_info->getConfigPrefix());
|
|
}
|
|
$breakpoint_groups = entity_load_multiple('breakpoint_group', $ids);
|
|
|
|
foreach ($breakpoint_groups as $breakpoint_group) {
|
|
if ($breakpoint_group->sourceType == $source_type && in_array($breakpoint_group->source, $list)) {
|
|
// Delete the automatically created breakpoint group.
|
|
$breakpoint_group->delete();
|
|
|
|
// Get all breakpoints defined by this theme/module.
|
|
$breakpoint_ids = \Drupal::service('config.storage')->listAll('breakpoint.breakpoint.' . $source_type . '.' . $breakpoint_group->id() . '.');
|
|
$entity_info = $entity_manager->getDefinition('breakpoint');
|
|
|
|
// Remove the breakpoint.breakpoint part of the breakpoint identifier.
|
|
foreach ($breakpoint_ids as &$breakpoint_id) {
|
|
$breakpoint_id = ConfigStorageController::getIDFromConfigName($breakpoint_id, $entity_info->getConfigPrefix());
|
|
}
|
|
$breakpoints = entity_load_multiple('breakpoint', $breakpoint_ids);
|
|
|
|
// Make sure we only delete breakpoints defined by this theme/module.
|
|
foreach ($breakpoints as $breakpoint) {
|
|
if ($breakpoint->sourceType == $source_type && $breakpoint->source == $breakpoint_group->name) {
|
|
$breakpoint->delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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.
|
|
_breakpoint_delete_breakpoint_groups($id, $source_type);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove breakpoint groups from all disabled themes or uninstalled modules.
|
|
*
|
|
* @param array $group_id
|
|
* Machine readable name of the breakpoint group.
|
|
* @param string $source_type
|
|
* Either Breakpoint::SOURCE_TYPE_THEME or Breakpoint::SOURCE_TYPE_MODULE.
|
|
*/
|
|
function _breakpoint_delete_breakpoint_groups($group_id, $source_type) {
|
|
$breakpoint_groups = entity_load_multiple('breakpoint_group');
|
|
foreach ($breakpoint_groups as $breakpoint_group) {
|
|
if ($breakpoint_group->sourceType == $source_type && $breakpoint_group->source == $group_id) {
|
|
$breakpoint_group->delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load one breakpoint by its identifier.
|
|
*
|
|
* @param int $id
|
|
* The id of the breakpoint to load.
|
|
*
|
|
* @return \Drupal\breakpoint\Entity\Breakpoint|null
|
|
* The entity object, or NULL if there is no entity with the given id.
|
|
*
|
|
* @todo Remove this in a follow-up issue.
|
|
* @see http://drupal.org/node/1798214
|
|
*/
|
|
function breakpoint_load($id) {
|
|
return entity_load('breakpoint', $id);
|
|
}
|
|
|
|
/**
|
|
* Load all breakpoint groups as select options.
|
|
*
|
|
* @return array
|
|
* An array containing breakpoint group labels indexed by their ids.
|
|
*/
|
|
function breakpoint_group_select_options() {
|
|
$options = array();
|
|
$breakpoint_groups = entity_load_multiple('breakpoint_group');
|
|
foreach ($breakpoint_groups as $breakpoint_group) {
|
|
$options[$breakpoint_group->id()] = $breakpoint_group->label();
|
|
}
|
|
asort($options);
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Load all breakpoints as select options.
|
|
*
|
|
* @return array
|
|
* An array containing breakpoints indexed by their ids.
|
|
*/
|
|
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 . ']';
|
|
}
|
|
asort($options);
|
|
return $options;
|
|
}
|