function ds_preprocess_ds_layout in Display Suite 8.3
Same name and namespace in other branches
- 8.4 ds.module \ds_preprocess_ds_layout()
- 8.2 ds.module \ds_preprocess_ds_layout()
Process layout.
This function is added in ds_theme_registry_alter().
1 string reference to 'ds_preprocess_ds_layout'
- ds_theme_registry_alter in ./
ds.module - Implements hook_theme_registry_alter().
File
- ./
ds.module, line 477 - Display Suite core functions.
Code
function ds_preprocess_ds_layout(&$variables) {
$layout_settings = $variables['settings'];
$layout_settings += [
'wrappers' => [],
];
// Fetch the entity type.
$bundle = FALSE;
$entity_type_id = FALSE;
if (isset($variables['content']['#entity_type'])) {
$entity_type_id = $variables['content']['#entity_type'];
}
if (isset($variables['content']['#bundle'])) {
$bundle = $variables['content']['#bundle'];
}
// Template layout.
if (!isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
// If the layout has wrapper class lets add it.
if (!empty($layout_settings['classes']['layout_class'])) {
foreach ($layout_settings['classes']['layout_class'] as $layout_class) {
$variables['attributes']['class'][] = $layout_class;
}
}
// Create region variables based on the layout settings.
if (!empty($layout_settings['wrappers']) && is_array($layout_settings['wrappers'])) {
foreach ($layout_settings['wrappers'] as $region_name => $wrapper) {
// @todo remove from D9, This is deprecated
$variables[$region_name . '_classes'] = !empty($layout_settings['classes'][$region_name]) ? ' ' . implode(' ', $layout_settings['classes'][$region_name]) : '';
// The new way of doing stuff is creating an attributes object.
if (!empty($layout_settings['classes'][$region_name])) {
$variables[$region_name . '_attributes'] = new Attribute([
'class' => $layout_settings['classes'][$region_name],
]);
}
else {
$variables[$region_name . '_attributes'] = new Attribute();
}
$variables[$region_name . '_wrapper'] = !empty($layout_settings['wrappers'][$region_name]) ? $layout_settings['wrappers'][$region_name] : 'div';
}
}
// Add a layout wrapper.
$variables['outer_wrapper'] = isset($layout_settings['outer_wrapper']) ? $layout_settings['outer_wrapper'] : 'div';
// Add custom attributes if any.
if (!empty($layout_settings['attributes'])) {
$layout_attributes = explode(',', $layout_settings['attributes']);
foreach ($layout_attributes as $layout_attribute) {
$replaced_attribute = $layout_attribute;
if (strpos($layout_attribute, '|') !== FALSE) {
if (isset($entity_type_id) && isset($variables['content']['#entity_type'])) {
$replaced_attribute = \Drupal::service('token')
->replace($layout_attribute, [
$variables['content']['#entity_type'] => $variables['content']['#' . $entity_type_id],
], [
'clear' => TRUE,
]);
}
list($key, $attribute_value) = explode('|', $replaced_attribute);
// Handle the class attribute as an array and others as strings.
$key == 'class' ? $variables['attributes'][$key][] = $attribute_value : ($variables['attributes'][$key] = $attribute_value);
}
}
}
// Add an onclick attribute on the wrapper.
if (!empty($layout_settings['link_attribute'])) {
$url = '';
switch ($layout_settings['link_attribute']) {
case 'content':
if ($entity_type_id) {
$url = $variables['content']['#' . $entity_type_id]
->toUrl()
->getInternalPath();
}
break;
case 'custom':
$url = $layout_settings['link_custom'];
break;
case 'tokens':
if ($entity_type_id) {
$url = \Drupal::service('token')
->replace($layout_settings['link_custom'], [
$entity_type_id => $variables['content']['#' . $entity_type_id],
], [
'clear' => TRUE,
]);
}
break;
}
if (!empty($url)) {
$uri_parts = parse_url($url);
if (empty($uri_parts['scheme'])) {
$url = 'internal:/' . ltrim($url, '/');
}
$url = Url::fromUri($url);
$variables['attributes']['onclick'] = 'location.href=\'' . $url
->toString() . '\'';
}
}
if ($entity_type_id) {
if (isset($variables['content']['#ds_configuration'])) {
// Add theming-classes to template.
$entity_classes = !empty($variables['content']['#ds_configuration']['layout']['entity_classes']) ? $variables['content']['#ds_configuration']['layout']['entity_classes'] : 'old_view_mode';
if ($entity_classes != 'no_classes') {
if ($entity_classes == 'all_classes') {
$variables['attributes']['class'][] = Html::cleanCssIdentifier($entity_type_id);
$variables['attributes']['class'][] = Html::cleanCssIdentifier($entity_type_id) . '--type-' . Html::cleanCssIdentifier($bundle);
$variables['attributes']['class'][] = Html::cleanCssIdentifier($entity_type_id) . '--view-mode-' . Html::cleanCssIdentifier($variables['content']['#view_mode']);
}
elseif ($entity_classes == 'old_view_mode') {
// Add (old style, non cleaned) view-mode-{name} to classes.
if (!in_array('view-mode-' . $variables['content']['#view_mode'], $variables['attributes']['class'])) {
$variables['attributes']['class'][] = 'view-mode-' . $variables['content']['#view_mode'];
}
}
}
// RDF support.
if ($entity_type_id == 'node' && function_exists('rdf_preprocess_node')) {
$entity = isset($variables[$entity_type_id]) ? $variables[$entity_type_id] : (isset($variables['content']['#' . $entity_type_id]) ? $variables['content']['#' . $entity_type_id] : '');
if ($entity) {
$variables['node'] = $entity;
// We disable the date feature for now as it throws notices.
$variables['display_submitted'] = FALSE;
rdf_preprocess_node($variables);
}
}
// Let other modules know we have rendered.
$variables['rendered_by_ds'] = TRUE;
// Let other modules alter the ds array before rendering.
$context = [
'entity' => NULL,
'entity_type' => $variables['content']['#entity_type'],
'bundle' => $variables['content']['#bundle'],
'view_mode' => $variables['content']['#view_mode'],
];
// Special case for User entity, becase $variables['user'] contains the currently logged in user
if ($entity_type_id !== 'user' && isset($variables[$entity_type_id])) {
$context['entity'] = $variables[$entity_type_id];
}
elseif (isset($variables['content']['#' . $entity_type_id])) {
$context['entity'] = $variables['content']['#' . $entity_type_id];
}
\Drupal::moduleHandler()
->alter('ds_pre_render', $variables['content'], $context, $variables);
}
}
// Copy the regions from 'content' into the top-level.
foreach (Element::children($variables['content']) as $name) {
$variables[$name] = $variables['content'][$name];
}
// Copy entity to top level to improve theme experience.
if (isset($variables['content']['#entity'])) {
$variables[$variables['content']['#entity_type']] = $variables['content']['#entity'];
}
}