You are here

function ds_entity_variables in Display Suite 7.2

Same name and namespace in other branches
  1. 7 ds.module \ds_entity_variables()

Add variables to an entity.

This function is added in ds_theme_registry_alter().

1 call to ds_entity_variables()
ds_extras_preprocess_view_layout in modules/ds_extras/ds_extras.module
Implements hook_preprocess_views_view().
1 string reference to 'ds_entity_variables'
_ds_theme_registry_alter in includes/ds.registry.inc
Implements hook_theme_registry_alter().

File

./ds.module, line 601
Display Suite core functions.

Code

function ds_entity_variables(&$vars) {
  if (isset($vars['elements']) && isset($vars['elements']['#bundle']) && ($layout = ds_get_layout($vars['elements']['#entity_type'], $vars['elements']['#bundle'], $vars['elements']['#view_mode']))) {
    $render_container = 'content';

    // User uses user_profile as container.
    if ($vars['elements']['#entity_type'] == 'user') {
      $render_container = 'user_profile';
    }

    // Move any preprocess fields to render container.
    // Inconsistency in taxonomy term naming.
    $entity_type = $vars['elements']['#entity_type'];
    if ($vars['elements']['#entity_type'] == 'taxonomy_term') {
      $entity_type = 'term';
    }

    // Get entity id and bundle
    $id = NULL;
    $bundle = $vars['elements']['#bundle'];
    $entity = isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : NULL);
    list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
    if (isset($vars[$entity_type]->preprocess_fields)) {
      foreach ($vars[$entity_type]->preprocess_fields as $field) {

        // Process RDF if the module is enabled before moving preprocess fields.
        if (module_exists('rdf')) {
          rdf_process($vars, $field);

          // Remove it so we can unset the field later on.
          unset($vars['rdf_template_variable_attributes_array'][$field]);
        }

        // Move the field to content so it renders, remove it
        // because we don't need it anymore.
        if (isset($vars[$field])) {
          $vars[$render_container][$field] = array(
            '#markup' => $vars[$field],
          );
          if (!isset($vars['preprocess_keep'])) {
            unset($vars[$field]);
          }
        }
      }
    }

    // Check if this is a flexible panels layout.
    if (!empty($layout['flexible'])) {
      ctools_include('plugins', 'panels');
      $vars['css_id'] = $vars['settings'] = $vars['display'] = $vars['renderer'] = '';
      $vars['layout'] = panels_get_layout($layout['panels']['name']);
      $vars['theme_hook_suggestion'] = 'panels_flexible';
    }
    else {

      // Template layout.
      if (!isset($vars['classes_array'])) {
        $vars['classes_array'] = array();
      }

      // Add view-mode-{name} to classes.
      if (!in_array('view-mode-' . $vars['elements']['#view_mode'], $vars['classes_array'])) {
        $vars['classes_array'][] = 'view-mode-' . $vars['elements']['#view_mode'];
      }

      // In case this is a panels layout, use panels info array.
      if (isset($layout['module']) && $layout['module'] == 'panels') {
        $layout['layout'] = $layout['panels']['theme'];
      }
      $bundle = strtr($bundle, '-', '_');
      $vars['theme_hook_suggestions'][] = $layout['layout'];
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'];
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#view_mode'];
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle;
      $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle . '_' . $vars['elements']['#view_mode'];
      if (!empty($id)) {
        $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '__' . $id;
      }
    }

    // If the layout has wrapper class lets add it.
    if (isset($layout['settings']['classes']['layout_class'])) {
      foreach ($layout['settings']['classes']['layout_class'] as $layout_class) {
        $vars['classes_array'][] = $layout_class;
      }
    }
    $layout_render_array = array();

    // Create region variables based on the layout settings.
    foreach ($layout['regions'] as $region_name => $region) {

      // Create the region content.
      $layout_render_array[$region_name] = array();
      if (isset($layout['settings']['regions'][$region_name])) {
        foreach ($layout['settings']['regions'][$region_name] as $key => $field) {

          // Make sure the field exists.
          if (!isset($vars[$render_container][$field])) {
            continue;
          }
          if (!isset($vars[$render_container][$field]['#weight'])) {
            $vars[$render_container][$field]['#weight'] = $key;
          }
          $layout_render_array[$region_name][$key] = $vars[$render_container][$field];
        }
      }

      // Add extras classes to the region.
      if (empty($layout['flexible'])) {
        $vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';

        // Token support for region classes.
        if (module_exists('token') && isset($vars[$entity_type])) {
          $vars[$region_name . '_classes'] = token_replace($vars[$region_name . '_classes'], array(
            $entity_type => $vars[$entity_type],
          ), array(
            'clear' => TRUE,
            'sanitize' => TRUE,
          ));
        }
      }

      // Add a wrapper to the region.
      if (empty($layout['flexible'])) {
        $vars[$region_name . '_wrapper'] = isset($layout['settings']['wrappers'][$region_name]) ? $layout['settings']['wrappers'][$region_name] : 'div';
      }
    }

    // Let other modules know we have rendered.
    $vars['rendered_by_ds'] = TRUE;

    // Add a layout wrapper.
    $vars['layout_wrapper'] = isset($layout['settings']['layout_wrapper']) ? $layout['settings']['layout_wrapper'] : 'div';

    // Add layout attributes if any.
    $vars['layout_attributes'] = '';
    if (!empty($layout['settings']['layout_attributes'])) {
      if (isset($vars[$entity_type])) {
        $vars['layout_attributes'] .= ' ' . token_replace($layout['settings']['layout_attributes'], array(
          $entity_type => $vars[$entity_type],
        ), array(
          'clear' => TRUE,
          'sanitize' => TRUE,
        ));
      }
      else {
        $vars['layout_attributes'] .= ' ' . $layout['settings']['layout_attributes'];
      }
    }

    // Merge in other attributes which were passed to the template.
    if (!empty($layout['settings']['layout_attributes_merge'])) {

      // Handle classes separately.
      if (isset($vars['attributes_array']['class'])) {
        $vars['classes_array'] = array_unique(array_merge($vars['classes_array'], $vars['attributes_array']['class']));
        unset($vars['attributes_array']['class']);
      }
      $vars['layout_attributes'] .= ' ' . drupal_attributes($vars['attributes_array']);
    }

    // Token support for layout classes.
    if (module_exists('token') && isset($vars[$entity_type])) {
      foreach ($vars['classes_array'] as &$class) {
        $class = token_replace($class, array(
          $entity_type => $vars[$entity_type],
        ), array(
          'clear' => TRUE,
          'sanitize' => TRUE,
        ));
      }
    }

    // Add an onclick attribute on the wrapper.
    if (!empty($layout['settings']['layout_link_attribute'])) {
      $url = '';
      switch ($layout['settings']['layout_link_attribute']) {
        case 'content':
          if ($entity_type == 'user') {
            $uri = entity_uri($vars['elements']['#entity_type'], $vars['elements']['#account']);
          }
          else {
            $uri = entity_uri($vars['elements']['#entity_type'], $vars[$entity_type]);
          }
          if (!empty($uri)) {
            $url = $uri['path'];
          }
          break;
        case 'custom':
          $url = $layout['settings']['layout_link_custom'];
          break;
        case 'tokens':
          $url = token_replace($layout['settings']['layout_link_custom'], array(
            $vars['elements']['#entity_type'] => $vars[$entity_type],
          ), array(
            'clear' => TRUE,
          ));
          break;
      }
      if (!empty($url)) {
        $vars['layout_attributes'] .= ' onclick="location.href=\'' . url($url) . '\'"';
      }
    }

    // Set field weights for all fields, including pre-process.
    foreach ($layout_render_array as $region => &$fields) {
      foreach ($fields as $field_key => &$field) {
        $field['#weight'] = $field_key;
      }
    }

    // Let other modules alter the ds array before creating the region variables.
    $context = array(
      'entity' => isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : ''),
      'entity_type' => $vars['elements']['#entity_type'],
      'bundle' => $vars['elements']['#bundle'],
      'view_mode' => $vars['elements']['#view_mode'],
    );
    drupal_alter('ds_pre_render', $layout_render_array, $context, $vars);
    foreach ($layout_render_array as $region_name => $content) {

      // In case this is a panels layout, add the region to the $content variable.
      if (isset($layout['module']) && $layout['module'] == 'panels') {
        $vars['content'][$region_name] = drupal_render($content);
      }
      else {
        $vars[$region_name] = drupal_render($content);
      }
    }
  }
}