Skip to content

Commit

Permalink
Issue backdrop#315: Add the "Entity view mode" module to core
Browse files Browse the repository at this point in the history
  • Loading branch information
docwilmot committed Nov 14, 2016
1 parent 0bda0d6 commit f4c7277
Show file tree
Hide file tree
Showing 5 changed files with 803 additions and 23 deletions.
4 changes: 4 additions & 0 deletions core/modules/entity/config/entity.view_modes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_config_name": "entity.view_modes",
"view_modes": []
}
217 changes: 217 additions & 0 deletions core/modules/entity/entity.admin.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?php

/**
* List all custom view modes.
*/
function entity_view_mode_list() {
$custom_view_modes = config_get('entity.view_modes', 'view_modes');
$entity_info = entity_get_info();

$build = array(
'#prefix' => '<div id="view-modes-accordion">',
'#suffix' => '</div>',
'#attached' => array(
'library' => array(
//array('system', 'ui.accordion'),
),
'js' => array(
//backdrop_get_path('module', 'entity_view_mode') . '/entity_view_mode.admin.js',
),
),
);

foreach (array_keys($entity_info) as $entity_type) {
if (empty($entity_info[$entity_type]['view modes'])) {
continue;
}

$build[$entity_type] = array(
'#type' => 'fieldset',
'#title' => $entity_info[$entity_type]['label'],
'#collapsible' => TRUE,
);

// Sort view modes by machine name.
ksort($entity_info[$entity_type]['view modes']);

$rows = array();
foreach ($entity_info[$entity_type]['view modes'] as $view_mode => $view_mode_info) {
$rows[$view_mode]['label'] = check_plain($view_mode_info['label']);
$rows[$view_mode]['type'] = !empty($custom_view_modes[$entity_type][$view_mode]) ? t('In configuration') : t('In code');
$rows[$view_mode]['custom_settings'] = !empty($view_mode_info['custom settings']) ? t('Yes') : t('No');

$operations = array();
if (isset($custom_view_modes[$entity_type][$view_mode])) {
$operations['edit'] = array(
'title' => t('Edit'),
'href' => "admin/config/system/view-modes/edit/{$entity_type}/{$view_mode}",
);
$operations['delete'] = array(
'title' => t('Delete'),
'href' => "admin/config/system/view-modes/delete/{$entity_type}/{$view_mode}",
);
}
if (!empty($operations)) {
$rows[$view_mode]['operations'] = array(
'data' => array(
'#theme' => 'links',
'#links' => $operations,
'#attributes' => array('class' => array('links', 'inline')),
),
);
}
else {
$rows[$view_mode]['operations'] = t('None (view mode locked)');
}
}

$rows['_add_new'][] = array(
'data' => l(t('Add new view mode'), "admin/config/system/view-modes/add/{$entity_type}"),
'colspan' => 4,
);

$build[$entity_type]['view_modes'] = array(
'#theme' => 'table',
'#header' => array(
t('View mode'),
t('Type'),
t('Custom settings'),
t('Operations'),
),
'#rows' => $rows,
);
}

return $build;
}

/**
* Form builder; edit a custom view mode.
*/
function entity_view_mode_edit_form($form, &$form_state, $entity_type, $machine_name = NULL) {
$form['#entity_type'] = $entity_type;
$form['#entity_info'] = $entity_info = entity_get_info($entity_type);
$view_mode = entity_view_mode_load($entity_type, $machine_name);

if (empty($entity_info['view modes']) || (isset($machine_name) && empty($view_mode))) {
backdrop_not_found();
backdrop_exit();
}

$form['entity_type'] = array(
'#type' => 'item',
'#title' => t('Entity type'),
'#markup' => $entity_info['label'],
);

$form['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => isset($view_mode['label']) ? $view_mode['label'] : '',
'#required' => TRUE,
);

$form['machine_name'] = array(
'#type' => 'machine_name',
'#machine_name' => array(
'source' => array('label'),
'exists' => 'entity_view_mode_exists',
),
'#default_value' => $machine_name,
'#entity_type' => $entity_type,
);

if (isset($machine_name)) {
$form['old_machine_name'] = array(
'#type' => 'value',
'#value' => $machine_name,
);
}

$form['custom_settings'] = array(
'#type' => 'checkbox',
'#title' => t('Use custom display settings'),
'#description' => t('Unchecking this will delete all the display settings for this view mode.'),
'#default_value' => isset($entity_info['view modes'][$machine_name]['custom settings']) ? $entity_info['view modes'][$machine_name]['custom settings'] : TRUE,
);

$bundle_options = entity_view_mode_get_possible_bundles($entity_type);
$bundle_values = !empty($machine_name) ? array_keys(entity_view_mode_get_enabled_bundles($entity_type, $machine_name)) : array_keys($bundle_options);

$form['enabled_bundles'] = array(
'#type' => 'checkboxes',
'#title' => t('Enable this view mode for the following types'),
'#description' => t('Unchecking a type that has already been configured will delete the display settings for this view mode.'),
'#options' => $bundle_options,
'#default_value' => $bundle_values,
'#states' => array(
'visible' => array(
':input[name="custom_settings"]' => array('checked' => TRUE),
),
),
);

$form['actions'] = array('#type' => 'actions');
$form['actions']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);

return $form;
}

function entity_view_mode_exists($machine_name, $element, $form_state) {
$entity_info = entity_get_info($element['#entity_type']);
return !empty($entity_info['view modes'][$machine_name]);
}

function entity_view_mode_edit_form_submit($form, &$form_state) {
// Save the view mode.
form_state_values_clean($form_state);
$view_mode = $form_state['values'];
entity_view_mode_save($form['#entity_type'], $view_mode);

backdrop_set_message(t('Saved the %view-mode @entity-type view mode.', array(
'@entity-type' => backdrop_strtolower($form['#entity_info']['label']),
'%view-mode' => $view_mode['label'],
)));
$form_state['redirect'] = 'admin/config/system/view-modes';
}

function entity_view_mode_delete_form(array $form, $form_state, $entity_type, $machine_name) {
$form['#entity_type'] = $entity_type;
$form['#entity_info'] = $entity_info = entity_get_info($entity_type);
$form['#view_mode'] = $view_mode = entity_view_mode_load($entity_type, $machine_name);

if (empty($form['#view_mode'])) {
backdrop_not_found();
backdrop_exit();
}

$form['machine_name'] = array(
'#type' => 'value',
'#value' => $machine_name,
);

return confirm_form(
$form,
t('Are you sure you want to delete the %view-mode @entity-type view mode?', array(
'@entity-type' => backdrop_strtolower($entity_info['label']),
'%view-mode' => $view_mode['label'],
)),
'admin/config/system/view-modes',
t('Deleting a view mode will cause any output still requesting to use that view mode to use the default display settings.'),
t('Delete'),
t('Cancel')
);
}

function entity_view_mode_delete_form_submit($form, &$form_state) {
entity_view_mode_delete($form['#entity_type'], $form_state['values']['machine_name']);

backdrop_set_message(t('Deleted the %view-mode @entity-type view mode.', array(
'@entity-type' => backdrop_strtolower($form['#entity_info']['label']),
'%view-mode' => $form['#view_mode']['label'],
)));
$form_state['redirect'] = 'admin/config/system/view-modes';
}
154 changes: 154 additions & 0 deletions core/modules/entity/entity.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,157 @@ function hook_entity_prepare_view($entities, $type) {
}
}
}

/**
* 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;
}

/**
* Change the view mode of an entity that is being displayed.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param array $context
* Array with contextual information, including:
* - entity_type: The type of the entity that is being viewed.
* - entity: The entity object.
* - langcode: The langcode the entity is being viewed in.
*/
function hook_entity_view_mode_alter(&$view_mode, $context) {
// For nodes, change the view mode when it is teaser.
if ($context['entity_type'] == 'node' && $view_mode == 'teaser') {
$view_mode = 'my_custom_view_mode';
}
}

/**
* Act on a view mode before it is created or updated.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param $entity_type
* The type of entity being saved (i.e. node, user, comment).
*/
function hook_entity_view_mode_presave($view_mode, $entity_type) {
// Force all view modes to be saved with custom settings.
$view_mode['custom settings'] = TRUE;
return $view_mode;
}

/**
* Respond to creation of a new view mode.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param $entity_type
* The type of entity being saved (i.e. node, user, comment).
*/
function hook_entity_view_mode_insert($view_mode, $entity_type) {
config_set('my_module.view_modes', 'view_mode_list', array($entity_type => $view_mode);
}

/**
* Respond to update of a view mode.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param $entity_type
* The type of entity being saved (i.e. node, user, comment).
*/
function hook_entity_view_mode_update($view_mode, $entity_type) {
config_set('my_module.view_modes', 'view_mode_list', array($entity_type => $view_mode);
}

/**
* Respond to deletion of a view mode.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param $entity_type
* The type of entity being saved (i.e. node, user, comment).
*/
function hook_entity_view_mode_delete($view_mode, $entity_type) {
$config = config('my_module.view_modes');
$view_mode_list = $config->get('view_mode_list');
unset($view_mode_list[$view_mode['machine_name']]);
$config->set('view_mode_list', $view_mode_list);
$config->save();
}

/**
* Act on a view mode which is being renamed.
*
* @param $entity_type
* The type of entity being saved (i.e. node, user, comment).
* @param string $old_view_mode_name
* The name of the view_mode before it is renamed.
* @param string $new_view_mode_name
* The name of the view_mode after it is renamed.
*/
function hook_entity_view_mode_rename($entity_type, $old_view_mode_name, $new_view_mode_name) {
$config = config('my_module.view_modes');
$view_mode_list = $config->get('view_mode_list');
if (isset($view_mode_list[$old_view_mode_name]) {
$view_mode_list[$new_view_mode_name] = $view_mode_list[$old_view_mode_name];
unset($view_mode_list[$old_view_mode_name]);
}
$config->set('view_mode_list', $view_mode_list);
$config->save();
}
Loading

0 comments on commit f4c7277

Please sign in to comment.