You are here

function empty_fields_field_attach_view_alter in Empty fields 7.2

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

Implements hook_field_attach_view_alter().

Overwrites the output of field.

File

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

Code

function empty_fields_field_attach_view_alter(&$output, $context) {
  if (empty($context['view_mode']) || empty($context['display'])) {
    return;
  }
  $entity_type = $context['entity_type'];
  $entity = $context['entity'];
  $view_mode = $context['view_mode'];

  // This is called per field without field context, so this flag prevents
  // excessive processing on the entity. Processed once per view mode.
  if (isset($entity->empty_fields_processed) && !empty($entity->empty_fields_processed[$view_mode])) {
    return;
  }
  $entity->empty_fields_processed[$view_mode] = TRUE;
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $rendered_elements = element_children($output);
  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']['empty_fields_handler'])) {
      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);

    // Provide additional context for the handlers.
    $context['field_name'] = $field_name;
    $context['field'] = $field;
    $context['instance'] = $instance;

    // But firstly, check if the user can access the field.
    if (!field_access('view', $field, $entity_type, $entity)) {
      continue;
    }
    $plugin_type = $display['settings']['empty_fields_handler'];
    if (in_array($field_name, $rendered_elements)) {
      switch ($field['type']) {
        case 'text':
        case 'text_long':
        case 'text_with_summary':
        case 'text_summary_or_trimmed':
          foreach (element_children($output[$field_name]) as $delta) {
            if (!empty($output[$field_name][$delta]['#markup']) || drupal_strlen($output[$field_name][$delta]['#markup'])) {
              continue 3;
            }
          }
          break;
        default:
          continue 2;
      }
    }

    // Load the configured plugin.
    $plugin = empty_fields_load_plugin($instance, $display['settings'], $view_mode);
    if ($plugin) {
      $markup = $plugin
        ->react($context);

      // Do not render empty values.
      if (!empty($markup) || drupal_strlen($markup)) {
        if (empty($context['language'])) {
          $language = field_language($entity_type, $entity, $field_name);
        }
        else {
          $language = $context['language'];
        }
        $output[$field_name] = array(
          '#theme' => 'field',
          '#title' => $instance['label'],
          '#label_display' => $display['label'],
          '#weight' => $display['weight'],
          '#field_type' => $field['type'],
          '#field_name' => $field_name,
          '#bundle' => $bundle,
          '#object' => $entity,
          '#entity_type' => $entity_type,
          '#view_mode' => $view_mode,
          '#language' => $language,
          '#formatter' => $display['type'],
          '#items' => array(
            0 => array(
              'value' => $markup,
            ),
          ),
          0 => array(
            '#markup' => $markup,
          ),
        );
        if (!empty($display['settings']['empty_fields_empty_class'])) {
          $output[$field_name]['#empty_fields_empty_class'] = $display['settings']['empty_fields_empty_class'];
        }

        // Allow plugins to alter output.
        $plugin
          ->alterOutput($output[$field_name], $context);
      }
    }
  }
}