You are here

function ds_preprocess_ds_layout in Display Suite 8.2

Same name and namespace in other branches
  1. 8.4 ds.module \ds_preprocess_ds_layout()
  2. 8.3 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 339
Display Suite core functions.

Code

function ds_preprocess_ds_layout(&$variables) {
  $layout_settings = $variables['settings'];

  // Fetch the entity type.
  $bundle = FALSE;
  $entity_type = FALSE;
  if (isset($variables['content']['#entity_type'])) {
    $entity_type = $variables['content']['#entity_type'];
  }
  if (isset($variables['content']['#bundle'])) {
    $bundle = $variables['content']['#bundle'];
  }

  // Template layout.
  if (!isset($variables['attributes']['class'])) {
    $variables['attributes']['class'] = array();
  }

  // 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.
  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)) {
          $replaced_attribute = \Drupal::service('token')
            ->replace($layout_attribute, array(
            $variables['content']['#entity_type'] => $variables['content']['#' . $entity_type],
          ), array(
            'clear' => TRUE,
          ));
        }
        list($key, $attribute_value) = explode('|', $replaced_attribute);
        $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) {
          $url = $variables['content']['#' . $entity_type]
            ->toUrl()
            ->getInternalPath();
        }
        break;
      case 'custom':
        $url = $layout_settings['link_custom'];
        break;
      case 'tokens':
        if ($entity_type) {
          $url = \Drupal::service('token')
            ->replace($layout_settings['link_custom'], array(
            $layout_settings => $variables['content']['#' . $entity_type],
          ), array(
            'clear' => TRUE,
          ));
        }
        break;
    }
    if (!empty($url)) {
      $url = Url::fromUri('internal:/' . $url);
      $variables['attributes']['onclick'] = 'location.href=\'' . $url
        ->toString() . '\'';
    }
  }
  if ($entity_type) {
    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);
          $variables['attributes']['class'][] = Html::cleanCssIdentifier($entity_type) . '--type-' . Html::cleanCssIdentifier($bundle);
          $variables['attributes']['class'][] = Html::cleanCssIdentifier($entity_type) . '--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 == 'node' && function_exists('rdf_preprocess_node')) {
        $entity = isset($variables[$entity_type]) ? $variables[$entity_type] : (isset($variables['content']['#' . $entity_type]) ? $variables['content']['#' . $entity_type] : '');
        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 = array(
        'entity' => isset($variables[$entity_type]) ? $variables[$entity_type] : (isset($variables['content']['#' . $entity_type]) ? $variables['content']['#' . $entity_type] : ''),
        'entity_type' => $variables['content']['#entity_type'],
        'bundle' => $variables['content']['#bundle'],
        'view_mode' => $variables['content']['#view_mode'],
      );
      \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'];
  }
}