function entity_view_mode_preprocess in Entity view modes 7
Implements hook_preprocess().
Add template suggestions for an entity based on view modes if they do not already exist.
The eventual order of the entity's theme suggestions are:
- entity-type__bundle (lowest priority = first in the array)
- entity-type__bundle__view-mode
- entity-type__id
- entity-type__id__view-mode (highest priority = last in the array)
See also
entity_view_mode_entity_view()
File
- ./
entity_view_mode.module, line 360
Code
function entity_view_mode_preprocess(&$variables, $hook) {
// Check for the context provided in entity_view_mode_entity_view().
if (!empty($variables['elements']['#entity_view_mode'])) {
// Sometimes a rendered entity is passed into another theme function, in
// which case we should not process. But also account that #theme may be a
// hook suggestion itself. For example. #theme = 'comment__node_type'
// and $hook = 'comment'.
if ($variables['elements']['#theme'] != $hook && strpos($variables['elements']['#theme'], $hook . '__') !== 0) {
return;
}
extract($variables['elements']['#entity_view_mode']);
$suggestions =& $variables['theme_hook_suggestions'];
// Ensure the base suggestions exist and if not, add them.
if ($has_bundles && !in_array("{$entity_type}__{$bundle}", $suggestions)) {
// The entity-type__bundle suggestion is typically "first".
array_unshift($suggestions, "{$entity_type}__{$bundle}");
}
if (!in_array("{$entity_type}__{$id}", $suggestions)) {
// The entity-type__id suggestion is always "last".
array_push($suggestions, "{$entity_type}__{$id}");
}
// Add view mode suggestions based on the location of the base suggestions.
if ($has_bundles && !in_array("{$entity_type}__{$bundle}__{$view_mode}", $suggestions)) {
$index = array_search("{$entity_type}__{$bundle}", $suggestions);
array_splice($suggestions, $index + 1, 0, "{$entity_type}__{$bundle}__{$view_mode}");
}
if (!in_array("{$entity_type}__{$id}__{$view_mode}", $suggestions)) {
$index = array_search("{$entity_type}__{$id}", $suggestions);
array_splice($suggestions, $index + 1, 0, "{$entity_type}__{$id}__{$view_mode}");
}
}
}