<?php
use Drupal\breakpoint\Plugin\Core\Entity\Breakpoint;
use Drupal\breakpoint\Plugin\Core\Entity\BreakpointGroup;
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;
}
}
function breakpoint_themes_enabled($theme_list) {
_breakpoint_theme_enabled($theme_list);
}
function breakpoint_themes_disabled($theme_list) {
_breakpoint_delete_breakpoints($theme_list, Breakpoint::SOURCE_TYPE_THEME);
}
function breakpoint_modules_enabled($modules) {
_breakpoint_modules_enabled($modules);
}
function breakpoint_modules_uninstalled($modules) {
_breakpoint_delete_breakpoints($modules, Breakpoint::SOURCE_TYPE_MODULE);
}
function _breakpoint_theme_enabled($theme_list) {
$themes = list_themes();
foreach ($theme_list as $theme_key) {
$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);
_breakpoint_import_breakpoint_groups($theme_key, Breakpoint::SOURCE_TYPE_THEME);
}
}
function _breakpoint_modules_enabled($modules) {
foreach ($modules as $module) {
$media_queries = breakpoint_get_module_media_queries($module);
_breakpoint_import_media_queries($module, $module, Breakpoint::SOURCE_TYPE_MODULE, $media_queries);
_breakpoint_import_breakpoint_groups($module, Breakpoint::SOURCE_TYPE_MODULE);
}
}
function _breakpoint_import_media_queries($group_name, $label, $source_type, $media_queries) {
if (!empty($media_queries)) {
$breakpoint_group = _breakpoint_group_create_or_load($group_name, $label, $group_name, $source_type);
foreach ($media_queries as $name => $media_query) {
$breakpoint_group
->addBreakpointFromMediaQuery($name, $media_query);
}
$breakpoint_group
->save();
}
}
function _breakpoint_import_breakpoint_groups($source, $source_type) {
$breakpoint_groups = config($source . '.breakpoint_groups');
if ($breakpoint_groups) {
foreach ($breakpoint_groups
->get() as $group_name => $data) {
if (isset($data['breakpoints']) && !empty($data['breakpoints'])) {
$breakpoint_group = _breakpoint_group_create_or_load($group_name, isset($data['label']) ? $data['label'] : $group_name, $source, $source_type);
$breakpoint_group
->addBreakpoints($data['breakpoints']);
$breakpoint_group
->save();
}
else {
throw new \Exception('Illegal config file detected.');
}
}
}
}
function _breakpoint_delete_breakpoints($list, $source_type) {
$ids = config_get_storage_names_with_prefix('breakpoint.breakpoint_group.' . $source_type . '.');
$entity_info = entity_get_info('breakpoint_group');
foreach ($ids as &$id) {
$id = drupal_substr($id, drupal_strlen($entity_info['config_prefix']) + 1);
}
$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)) {
$breakpoint_group
->delete();
$breakpoint_ids = drupal_container()
->get('config.storage')
->listAll('breakpoint.breakpoint.' . $source_type . '.' . $breakpoint_group
->id() . '.');
$entity_info = entity_get_info('breakpoint');
foreach ($breakpoint_ids as &$breakpoint_id) {
$breakpoint_id = drupal_substr($breakpoint_id, drupal_strlen($entity_info['config_prefix']) + 1);
}
$breakpoints = entity_load_multiple('breakpoint', $breakpoint_ids);
foreach ($breakpoints as $breakpoint) {
if ($breakpoint->sourceType == $source_type && $breakpoint->source == $breakpoint_group->name) {
$breakpoint
->delete();
}
}
}
}
foreach ($ids as $id) {
_breakpoint_delete_breakpoint_groups($id, $source_type);
}
}
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();
}
}
}
function breakpoint_get_theme_media_queries($theme_key) {
$themes = list_themes();
if (!isset($themes[$theme_key])) {
throw new \Exception('Illegal theme_key passed.');
}
$config = config($theme_key . '.breakpoints');
if ($config) {
return $config
->get();
}
return array();
}
function breakpoint_get_module_media_queries($module) {
if (!module_exists($module)) {
throw new \Exception('Illegal module name passed.');
}
$config = config($module . '.breakpoints');
if ($config) {
return $config
->get();
}
return array();
}
function breakpoint_group_load($id) {
return entity_load('breakpoint_group', $id);
}
function breakpoint_load($id) {
return entity_load('breakpoint', $id);
}
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;
}
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;
}
function _breakpoint_group_create_or_load($name, $label, $source, $source_type) {
$breakpoint_group = entity_load('breakpoint_group', $source_type . '.' . $source . '.' . $name);
if (!$breakpoint_group) {
$breakpoint_group = entity_create('breakpoint_group', array(
'name' => $name,
'label' => $label,
'source' => $source,
'sourceType' => $source_type,
));
}
return $breakpoint_group;
}