function hook_field_display_alter

Alters the display settings of a field before it is displayed.

Note that instead of hook_field_display_alter(), which is called for all fields on all entity types, hook_field_display_ENTITY_TYPE_alter() may be used to alter display settings for fields on a specific entity type only.

This hook is called once per field per displayed entity. If the result of the hook involves reading from the database, it is highly recommended to statically cache the information.

Parameters

array $display_properties: The display settings that will be used to display the field values, as found in the 'display' key of $instance definitions.

array $context: An associative array containing:

  • entity_type: The entity type, e.g., 'node' or 'user'.
  • bundle: The bundle, e.g., 'page' or 'article'.
  • field: The field being rendered.
  • instance: The instance being rendered.
  • view_mode: The view mode, e.g. 'full', 'teaser'...

See also

hook_field_display_ENTITY_TYPE_alter()

Related topics

File

drupal/core/modules/field/field.api.php, line 1968

Code

function hook_field_display_alter(array &$display_properties, array $context) {

  // Leave field labels out of the search index.
  // Note: The check against $context['entity_type'] == 'node' could be avoided
  // by using hook_field_display_node_alter() instead of
  // hook_field_display_alter(), resulting in less function calls when
  // rendering non-node entities.
  if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') {
    $display_properties['label'] = 'hidden';
  }
}