Commit 4264c395 authored by Dave Reid's avatar Dave Reid

Issue #1900668: Added hook_entity_view_mode_info() and hook_entity_view_mode_info_alter().

parent df075dcc
<?php
/**
* @file
* API documentation for the entity_view_mode module.
*/
/**
* Describe the view modes for entity types.
*
* View modes let entities be displayed differently depending on the context.
* For instance, a node can be displayed differently on its own page ('full'
* mode), on the home page or taxonomy listings ('teaser' mode), or in an RSS
* feed ('rss' mode). Modules taking part in the display of the entity (notably
* the Field API) can adjust their behavior depending on the requested view
* mode. An additional 'default' view mode is available for all entity types.
* This view mode is not intended for actual entity display, but holds default
* display settings. For each available view mode, administrators can configure
* whether it should use its own set of field display settings, or just
* replicate the settings of the 'default' view mode, thus reducing the amount
* of display configurations to keep track of.
*
* Note: This hook is invoked inside an implementation of
* hook_entity_info_alter() so care must be taken not to call anything that
* will result in an additional, and hence recurisve call to entity_get_info().
*
* @return array
* An associative array of all entity view modes, keyed by the entity
* type name, and then the view mode name, with the following keys:
* - label: The human-readable name of the view mode.
* - custom_settings: A boolean specifying whether the view mode should by
* default use its own custom field display settings. If FALSE, entities
* displayed in this view mode will reuse the 'default' display settings
* by default (e.g. right after the module exposing the view mode is
* enabled), but administrators can later use the Field UI to apply custom
* display settings specific to the view mode.
*
* @see entity_view_mode_entity_info_alter()
* @see hook_entity_view_mode_info_alter()
*/
function hook_entity_view_mode_info() {
$view_modes['user']['full'] = array(
'label' => t('User account'),
);
$view_modes['user']['compact'] = array(
'label' => t('Compact'),
'custom_settings' => TRUE,
);
return $view_modes;
}
/**
* Alter the view modes for entity types.
*
* Note: This hook is invoked inside an implementation of
* hook_entity_info_alter() so care must be taken not to call anything that
* will result in an additional, and hence recurisve call to entity_get_info().
*
* @param array $view_modes
* An array of view modes, keyed first by entity type, then by view mode name.
*
* @see entity_view_mode_entity_info_alter()
* @see hook_entity_view_mode_info()
*/
function hook_entity_view_mode_info_alter(&$view_modes) {
$view_modes['user']['full']['custom_settings'] = TRUE;
}
<?php
/**
* Implements hook_hook_info().
*/
function entity_view_mode_hook_info() {
$hooks = array(
'entity_view_mode_info',
'entity_view_mode_info_alter',
'entity_view_mode_presave',
'entity_view_mode_insert',
'entity_view_mode_update',
'entity_view_mode_delete',
);
return array_fill_keys($hooks, array('group' => 'entity'));
}
/**
* Implements hook_permission().
*/
......@@ -15,9 +31,42 @@ function entity_view_mode_permission() {
* Implements hook_entity_info_alter().
*/
function entity_view_mode_entity_info_alter(&$info) {
foreach (variable_get('entity_view_modes', array()) as $entity_type => $view_modes) {
if (isset($info[$entity_type]['view modes'])) {
$info[$entity_type]['view modes'] += $view_modes;
static $invoking_hooks = FALSE;
// Protect against recursion just in case any implementations of
// hook_entity_view_mode_info() and hook_entity_view_mode_info_alter() from
// calling entity_get_info().
if ($invoking_hooks) {
return;
}
// First get all the hook-provided view modes.
$invoking_hooks = TRUE;
$view_mode_info = module_invoke_all('entity_view_mode_info');
drupal_alter('entity_view_mode_info', $view_mode_info);
$invoking_hooks = FALSE;
// Add in the variable entity view modes which override hook-provided.
$view_mode_info = drupal_array_merge_deep($view_mode_info, variable_get('entity_view_modes', array()));
// Because entity_get_info() merges in the default 'custom settings' value
// before it invokes hook_entity_info_alter(), we need to make sure that the
// custom view modes also have this value defined.
foreach ($view_mode_info as $entity_type => &$view_modes) {
foreach ($view_modes as &$view_mode) {
// @todo Should this default to TRUE?
$view_mode += array('custom settings' => FALSE);
}
}
// Add in the combined custom entity view modes which override the existing
// view modes in the entity information.
foreach ($view_mode_info as $entity_type => $view_modes) {
if (isset($info[$entity_type])) {
if (!isset($info[$entity_type]['view modes'])) {
$info[$entity_type]['view modes'] = array();
}
$info[$entity_type]['view modes'] = $info[$entity_type]['view modes'] + $view_modes;
}
}
}
......
......@@ -155,6 +155,53 @@ class EntityViewModeFunctionalTest extends EntityViewModeTestHelper {
$this->assertViewModeDefaultDisplay('node', 'article', 'custom_2');
$this->assertViewModeDefaultDisplay('node', 'page', 'custom_2');
}
/**
* Test the new entity view mode hooks.
*/
public function testInfoHooks() {
variable_set('entity_view_modes', array(
'node' => array(
'info_3' => array(
'label' => t('Variable-altered view mode'),
'custom settings' => TRUE,
),
),
'taxonomy_term' => array(),
));
$this->refreshVariables();
$info = entity_get_info();
// An invalid entity type in hook_entity_view_mode_info() does not pass
// into the entity info array.
$this->assertTrue(!isset($info['invalid-type']));
// Test hook-provided view modes.
$this->assertIdentical($info['node']['view modes']['info_1'], array(
'label' => t('Hook-defined view mode #1'),
'custom settings' => FALSE,
));
$this->assertIdentical($info['node']['view modes']['info_2'], array(
'label' => t('Hook-altered view mode'),
'custom settings' => TRUE,
));
$this->assertIdentical($info['node']['view modes']['info_3'], array(
'label' => t('Variable-altered view mode'),
'custom settings' => TRUE,
));
$this->assertIdentical($info['taxonomy_term']['view modes']['info_1'], array(
'label' => t('Hook-defined view mode #1'),
'custom settings' => TRUE,
));
// Test that entity view modes defined in hook_entity_info() are never
// overridden by custom view modes.
$this->assertIdentical($info['node']['view modes']['full'], array(
'label' => 'Full content',
'custom settings' => FALSE,
));
}
}
class EntityViewModeSuggestionsTest extends EntityViewModeTestHelper {
......
<?php
/**
* @file
* Entity view mode integration for the entity_view_mode_test module.
*/
/**
* Implements hook_entity_view_mode_info().
*/
function entity_view_mode_test_entity_view_mode_info() {
$info['node']['info_1'] = array(
'label' => t('Hook-defined view mode #1'),
);
$info['node']['info_2'] = array(
'label' => t('Hook-defined view mode #2'),
'custom settings' => FALSE,
);
$info['node']['info_3'] = array(
'label' => t('Hook-defined view mode #3'),
);
$info['node']['full'] = array(
'label' => t('Full view mode already defined by core.'),
'custom settings' => TRUE,
);
$info['taxonomy_term']['info_1'] = array(
'label' => t('Hook-defined view mode #1'),
'custom settings' => TRUE,
);
// Invalid entity type.
$info['invalid-type']['test'] = array(
'label' => t('Test view mode'),
);
// Call entity_get_info() from inside this hook to test recursion prevention.
foreach (entity_get_info() as $entity_type => $entity_info) {
$info[$entity_type]['recursion'] = array(
'label' => t('Recursion'),
);
}
return $info;
}
/**
* Implements hook_entity_view_mode_info_alter().
*/
function entity_view_mode_test_entity_view_mode_info_alter(&$info) {
$info['node']['info_2']['label'] = t('Hook-altered view mode');
$info['node']['info_2']['custom settings'] = TRUE;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment