function skinr_preprocess in Skinr 8.2
Same name and namespace in other branches
- 6.2 skinr.module \skinr_preprocess()
- 6 skinr.module \skinr_preprocess()
- 7.2 skinr.module \skinr_preprocess()
Implements hook_preprocess().
@todo Optimize this function by removing dependencies on skinr_get_skin_info() and similar resource heavy functions. @todo Account for Drupal's caching being enabled and make it work.
File
- ./
skinr.module, line 142 - Handles core Skinr functionality.
Code
function skinr_preprocess(&$variables, $hook) {
// Fix for update script.
if (defined('MAINTENANCE_MODE')) {
return;
}
$data =& drupal_static(__FUNCTION__);
// Caching the data returned from the following functions is reported to
// improve performance.
if (!isset($data)) {
$data['current_theme'] = skinr_current_theme();
$data['skin_info'] = skinr_get_skin_info();
$data['theme_registry'] = \Drupal::service('theme.registry')
->getRuntime();
$data['skip_cache'] = FALSE;
$implementations = \Drupal::moduleHandler()
->getImplementations('skinr_preprocess_alter');
// skinr_panels is special case that only runs when skinr_context is enabled.
if (count($implementations) > 0 && $implementations[0] !== 'skinr_panels') {
// Skip caching whenever a module implements skinr_preprocess_alter.
$data['skip_cache'] = TRUE;
}
}
// @todo Use $variables['theme_hook_original'] ?
$original_hook = $hook;
// dpm($hook . '|' . $variables['theme_hook_original']);
// An array of $elements based on $module and $original_hook, derived from $variables.
$implementations = \Drupal::cache('bootstrap')
->get('skinr_elements');
$array_elements = skinr_invoke_all('skinr_elements', $variables, $original_hook, 'preprocess');
foreach ($array_elements as $element_type => $elements) {
$applied_skins = array();
foreach ($elements as $element) {
$cid = 'skinr_preprocess:' . $element_type . ':' . $element . ':' . $data['current_theme'];
if (!$data['skip_cache'] && ($cached = \Drupal::cache()
->get($cid))) {
// This type of caching is incompatible with skinr_context.
$applied_skins = $cached->data;
}
else {
$properties = array(
'theme' => $data['current_theme'],
'element_type' => $element_type,
'element' => $element,
'status' => 1,
);
$skins = entity_load_multiple_by_properties('skin', $properties);
// Invoke hook_skinr_preprocess_alter() in all modules.
$context = array(
'hook' => $hook,
'variables' => &$variables,
'theme' => $data['current_theme'],
'element_type' => $element_type,
'elements' => $elements,
);
\Drupal::moduleHandler()
->alter('skinr_preprocess', $skins, $context);
$applied_skins = array();
foreach ($skins as $skin) {
$applied_skins = array(
$skin->skin => $skin
->getOptions(),
) + $applied_skins;
}
// Cache data.
if (!$data['skip_cache']) {
\Drupal::cache('data')
->set($cid, $applied_skins, CacheBackendInterface::CACHE_PERMANENT, array(
'skinr:' . $element_type . ':' . $element,
));
}
}
}
// Attach JS and CSS, and add classes.
if (!empty($applied_skins)) {
foreach ($applied_skins as $skin_name => $skin_options) {
// Special case for _additional.
if ($skin_name == '_additional') {
continue;
}
$skin_info = $data['skin_info'][$skin_name];
// Make sure this skin is enabled for the current theme.
$status = skinr_skin_info_status_get($skin_info);
if (empty($status[$data['current_theme']])) {
continue;
}
if (isset($skin_info['attached'])) {
$variables['#attached']['library'][] = 'skinr/skinr.' . $skin_name;
}
if (!is_array($skin_options)) {
$skin_options = array(
$skin_options,
);
}
foreach ($skin_options as $skin_option) {
if (isset($data['skin_info'][$skin_name]['options'][$skin_option]['attached'])) {
$variables['#attached']['library'][] = 'skinr/skinr.' . $skin_name . '.' . $skin_option;
}
}
}
$flattened_skins_array = skinr_flatten_skins_array($applied_skins);
if (isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = array_merge($variables['attributes']['class'], $flattened_skins_array);
}
else {
$variables['attributes']['class'] = $flattened_skins_array;
}
}
}
}