You are here

function empty_fields_field_attach_view_alter in Empty fields 7

Same name and namespace in other branches
  1. 7.2 empty_fields.module \empty_fields_field_attach_view_alter()

Implements hook_field_attach_view_alter().

File

./empty_fields.module, line 124
Contains the implementation for the empty_fields module.

Code

function empty_fields_field_attach_view_alter(&$result, $context) {
  if (empty($context['view_mode']) || empty($context['display']) || $context['view_mode'] != $context['display']) {
    return;
  }
  $view_mode = $context['view_mode'];
  $entity_type = $context['entity_type'];
  $entity = $context['entity'];
  $language = $context['language'];
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $rendered_elements = element_children($result);
  foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {

    // Do not add field that is hidden in current display.
    $display = field_get_display($instance, $view_mode, $entity);
    if ($display['type'] == 'hidden' || empty($display['settings']['display_empty'])) {
      continue;
    }

    // Some field types still render, so double check that these have content.
    // To date, this is limited to the text fields that always save even if
    // empty.
    $field = field_info_field($field_name);

    // But firstly, check if the user can access the field.
    if (!field_access('view', $field, $entity_type, $entity)) {
      continue;
    }
    if (in_array($field_name, $rendered_elements)) {
      switch ($field['type']) {
        case 'text':
        case 'text_with_summary':
        case 'text_summary_or_trimmed':
          foreach (element_children($result[$field_name]) as $delta) {
            if (!empty($result[$field_name][$delta]['#markup']) || strlen($result[$field_name][$delta]['#markup'])) {
              continue 3;
            }
          }
          break;
        default:
          continue 2;
      }
    }
    $markup = '';
    switch ($display['settings']['display_empty']) {
      case 'text':
        if (!empty($display['settings']['custom_text'])) {
          global $user;
          $args = array(
            $entity_type => $entity,
            'user' => $user,
          );
          $markup = t($display['settings']['custom_text']);
          $markup = token_replace($markup, $args, array(
            'clear' => TRUE,
          ));
          $markup = filter_xss_admin($markup);
        }
        break;
      case 'callback':
        $markup = FALSE;
        if ($callback = empty_fields_get_empty_field_callbacks($display['settings']['empty_callback'])) {
          $func = $callback['callback'];
          if (function_exists($func)) {
            $field_context = $context;
            $field_context['display'] = $display;
            $field_context['instance'] = $instance;
            $field_context['field'] = $field;
            $markup = $func($field_name, $field_context);
          }
        }
        if ($markup === FALSE || is_null($markup)) {
          continue 2;
        }
        break;
      case 'default':
        $items = field_get_default_value($entity_type, $entity, $field, $instance, $language);
        $items = _field_filter_items($field, $items);
        if (!empty($items)) {

          // Too late to allow other modules to alter the results, so only
          // display the first item.
          $item = array_shift($items);
          $element = field_view_value($entity_type, $entity, $field_name, $item, $display, $language);
          if ($element) {
            $result[$field_name] = array(
              '#theme' => 'field',
              '#title' => $instance['label'],
              '#label_display' => $display['label'],
              '#field_type' => $field['type'],
              '#field_name' => $field_name,
              '#bundle' => $bundle,
              '#object' => $entity,
              '#items' => array(
                0 => $item,
              ),
              '#entity_type' => $entity_type,
              '#weight' => $display['weight'],
              '0' => $element,
            );
          }
        }

      // Fall through.
      default:
        continue 2;
    }
    $result[$field_name] = array(
      '#theme' => 'field',
      '#title' => $instance['label'],
      '#label_display' => $display['label'],
      '#field_type' => $field['type'],
      '#field_name' => $field_name,
      '#bundle' => $bundle,
      '#object' => $entity,
      '#items' => array(
        0 => array(
          'value' => $markup,
        ),
      ),
      '#entity_type' => $entity_type,
      '#weight' => $display['weight'],
      0 => array(
        '#markup' => $markup,
      ),
    );
  }
}