You are here

function ds_field_attach_view_alter in Display Suite 7

Same name and namespace in other branches
  1. 7.2 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 680
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);

  // Do not render panel layouts.
  if (!empty($layout['settings']['ds_panels'])) {
    return;
  }

  // Check on field limit.
  if (isset($layout['settings']['limit'])) {
    foreach ($layout['settings']['limit'] as $field => $limit) {
      if (isset($build[$field])) {
        $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 ($field_value) {
      $build[$key] = array(
        '#theme' => 'field',
        '#field_type' => 'ds',
        '#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,
        '#entity_type' => $entity_type,
        '#view_mode' => $view_mode,
        '#access' => TRUE,
        '#items' => array(
          0 => array(
            'value' => $field_value,
          ),
        ),
        0 => array(
          '#markup' => $field_value,
        ),
      );
    }
  }

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

    // Disable blocks.
    if (isset($layout['settings']['hide_sidebars']) && $layout['settings']['hide_sidebars']) {
      ctools_set_no_blocks();
    }

    // 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';
    }
  }
}