You are here

function ds_field_attach_view_alter in Display Suite 7.2

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

Implements hook_field_attach_view_alter().

1 call to ds_field_attach_view_alter()
ds_extras_preprocess_view_layout in modules/ds_extras/ds_extras.module
Implements hook_preprocess_views_view().

File

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

Code

function ds_field_attach_view_alter(&$build, $context) {
  static $loaded_css = array();

  // Global kill switch. In some edge cases, a view might
  // be inserted into the view of an entity, in which the
  // same entity is available as well. This is simply not
  // possible, so you can temporarily disable DS completely
  // by setting this variable, either from code or via
  // the UI through admin/structure/ds/
  if (ds_kill_switch()) {
    return;
  }

  // If views and core doesn't send information along on the entity,
  // Display Suite doesn't have a context to render the fields.
  if (!isset($build['#entity_type']) && !isset($build['#bundle'])) {
    return;
  }

  // If no layout is configured, stop as well.
  if (!ds_get_layout($build['#entity_type'], $build['#bundle'], $context['view_mode'])) {
    return;
  }
  $entity_type = $build['#entity_type'];
  $bundle = $build['#bundle'];
  $view_mode = $context['view_mode'];
  $entity = $context['entity'];
  $layout = ds_get_layout($entity_type, $bundle, $view_mode);

  // Check on field/delta limit.
  if (isset($layout['settings']['limit'])) {
    foreach ($layout['settings']['limit'] as $field => $limit) {
      if (isset($build[$field])) {
        if ($limit === 'delta' && isset($entity->ds_delta) && isset($entity->ds_delta[$field])) {
          $delta = $entity->ds_delta[$field];
          foreach ($build[$field]['#items'] as $key => $item) {
            if ($key != $delta) {
              unset($build[$field][$key]);
            }
          }
        }
        else {
          $count = count($build[$field]['#items']);
          if ($count > $limit) {
            $build[$field]['#items'] = array_slice($build[$field]['#items'], 0, $limit);
          }
        }
      }
    }
  }

  // Add Display Suite display fields.
  $fields = ds_get_fields($entity_type);
  $field_values = ds_get_field_settings($entity_type, $bundle, $layout['view_mode']);
  foreach ($field_values as $key => $field) {

    // Ignore if this field is not a DS field.
    if (!isset($fields[$key])) {
      continue;
    }
    $field = $fields[$key];
    if (isset($field_values[$key]['format'])) {
      $field['formatter'] = $field_values[$key]['format'];
    }
    if (isset($field_values[$key]['formatter_settings'])) {
      $field['formatter_settings'] = $field_values[$key]['formatter_settings'];
    }
    $field_value = ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build);

    // Title label.
    if ($key == 'title' && $entity_type == 'node') {
      $node_type = node_type_get_type($entity);
      $field['title'] = function_exists('i18n_node_translate_type') ? i18n_node_translate_type($node_type->type, 'title_label', $node_type->title_label) : $node_type->title_label;
    }
    if (!empty($field_value) || (string) $field_value === '0') {

      // Special case for views.
      if (!empty($build['render_code_fields'])) {
        $build[$key] = $field_value;
      }
      else {
        $build[$key] = array(
          '#theme' => 'field',
          '#field_type' => 'ds',
          '#skip_edit' => TRUE,
          '#title' => $field['title'],
          '#weight' => isset($field_values[$key]['weight']) ? $field_values[$key]['weight'] : 0,
          '#label_display' => isset($field_values[$key]['label']) ? $field_values[$key]['label'] : 'inline',
          '#field_name' => $key,
          '#bundle' => $bundle,
          '#object' => $entity,
          '#entity_type' => $entity_type,
          '#view_mode' => $view_mode,
          '#access' => variable_get('ds_extras_field_permissions', FALSE) && function_exists('ds_extras_ds_field_access') ? ds_extras_ds_field_access($key, $entity_type) : TRUE,
          '#items' => array(
            0 => array(
              'value' => $field_value,
            ),
          ),
          0 => array(
            '#markup' => $field_value,
          ),
        );
      }
    }
  }
  $disable_css = FALSE;
  if (!empty($layout['settings']['layout_disable_css'])) {
    $disable_css = TRUE;
  }

  // Add path to css file for this layout and disable block regions if necessary.
  if (!$disable_css && isset($layout['css']) && !isset($loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'])) {

    // Register css file.
    $loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'] = TRUE;

    // Add css file.
    if (isset($layout['module']) && $layout['module'] == 'panels') {
      $build['#attached']['css'][] = $layout['path'] . '/' . $layout['panels']['css'];
    }
    else {
      $build['#attached']['css'][] = $layout['path'] . '/' . $layout['layout'] . '.css';
    }
  }
}