You are here

function render_cache_view_field in Render cache 7

Helper function to view a single entity field.

This can be used to replace field_view_field().

Parameters

string $entity_type: The type of $entity; e.g., 'node' or 'user'.

object $entity: The entity containing the field to display. Must at least contain the id key and the field data to display.

string $field_name: The name of the field to display.

string|array $display: Can be either:

  • The name of a view mode. The field will be displayed according to the display settings specified for this view mode in the $instance definition for the field in the entity's bundle. If no display settings are found for the view mode, the settings for the 'default' view mode will be used.
  • An array of display settings, as found in the 'display' entry of $instance definitions. The following key/value pairs are allowed:

    • label: (string) Position of the label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'.
    • type: (string) The formatter to use. Defaults to the 'default_formatter' for the field type, specified in hook_field_info(). The default formatter will also be used if the requested formatter is not available.
    • settings: (array) Settings specific to the formatter. Defaults to the formatter's default settings, specified in hook_field_formatter_info().
    • weight: (float) The weight to assign to the renderable element. Defaults to 0.

string $langcode: (Optional) The language the field values are to be shown in. The site's current language fallback logic will be applied no values are available for the language. If no language is provided the current language will be used.

Return value

A renderable array for the field value.

1 call to render_cache_view_field()
render_cache_hijack_views_handler_field_field::set_items in modules/render_cache_views/views/plugins/render_cache_hijack_views_handler_field_field.inc
Override views_handler_field_field::render() to add caching.

File

./render_cache.module, line 456
Hook implementations and frequently used functions for render cache module.

Code

function render_cache_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) {

  // If the entity has no field value, just return nothing.
  $items = field_get_items($entity_type, $entity, $field_name);
  if (empty($items)) {
    return array();
  }

  // Prepare context
  $context = array(
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'display' => $display,
    'langcode' => field_language($entity_type, $entity, $field_name, $langcode),
    'deltas' => array_keys($items),
  );

  // Setup drupal_render style cache array.
  $cache_info = render_cache_cache_info_defaults();
  $cache_info['keys'] = array(
    'field',
    $entity_type,
    $field_name,
  );
  drupal_alter('render_cache_field_default_cache_info', $cache_info, $context);
  list($entity_id, $entity_revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  $entity_context = $context + array(
    'entity_id' => $entity_id,
    'entity_revision_id' => $entity_revision_id,
    'bundle' => $bundle,
  );
  $cid = render_cache_get_entity_cid($entity, $cache_info, $entity_context);
  $cache = NULL;
  if (isset($cache_info['granularity']) && $cache_info['granularity'] != DRUPAL_NO_CACHE) {
    $cache = cache_get($cid, 'cache_render');
  }
  if (!$cache) {
    $field_render = field_view_field($entity_type, $entity, $field_name, $display, $langcode);
    _render_cache_pre_render($field_render, $cid, $cache_info);
    $cache = (object) array(
      'data' => $field_render,
    );
  }
  $render = $cache->data;

  // @todo Not sure what should be used for the second parameter here?
  _render_cache_post_render($render, $cid);
  return $render;
}