Jose Luis Bellido (jlbellido)
@jose_lakatos
https://github.com/jlbellido
https://www.drupal.org/u/jlbellido
View modes in Drupal 7
It's a concrete visualization mode for a node or other entities.
Where can we use view modes?
For nodes :
More info : https://www.drupal.org/node/1577752
Related concepts
Manage display
How to create new view modes
/**
* Implements hook_entity_info_alter().
*/
function MYMODULE_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['related'] = array(
'label' => t('Related'),
'custom settings' => TRUE,
);
}
Theme suggestions.
/**
* Implements template_preprocess_node()
*/
function THEME_preprocess_node(&$variables) {
$node = $variables['node'];
$view_mode = $variables['view_mode'];
// Set up template suggestions for non-standard view modes
if ($view_mode !== 'full') {
$variables['theme_hook_suggestions'][] = 'node__' . $node->type . '__' . $view_mode;
}
}
Limitations
Useful contrib modules
Form Modes + View Modes
Form Modes
No more : #access => FALSE !!!!
New administration page
Exportable configuration.
Other features
More info : https://www.drupal.org/node/2511722
Tapas section!
https://drupalconsole.com/ (v 1.0.0-RC16)
Only 5 minutes with Drupal Console!!!
Available on Github :
Requirements that we want to achieve:
Define a new form mode (I)
Define a new form mode (II)
Configure our fields for our field mode
Declare as "Form operation"
* "form" = {
* "default" = "Drupal\display_modes_example\Form\TapaForm",
* "add" = "Drupal\display_modes_example\Form\TapaForm",
* "suggested_tapa_form" = "Drupal\display_modes_example\Form\TapaForm",
* "edit" = "Drupal\display_modes_example\Form\TapaForm",
* "delete" = "Drupal\display_modes_example\Form\TapaDeleteForm",
* },
For other entities:
/**
* Implements hook_entity_type_alter().
*/
function display_modes_example_entity_type_alter(array &$entity_types) {
if (isset($entity_types['entity'])) {
$entity_types['entity']->setFormClass(....);
}
}
Adding a new route to routing.yml
entity.tapa.suggested_tapa_form:
path: '/admin/structure/tapa/suggestion'
defaults:
_entity_form: tapa.suggested_tapa_form
_title: 'Suggested tapa from'
requirements:
_permission: 'edit tapa entities'
entity.tapa.suggested_tapa_form:
route_name: entity.tapa.suggested_tapa_form
title: 'Add Tapa suggestion'
appears_on:
- entity.tapa.collection
New link action
display_modes_example.links.action.yml
Final result: Action links
Final result: Suggested tapa form
Contrib modules
Documentation page
Requirements that we want to achieve:
Define a new view mode (I)
Define a new view mode (II)
Configure our view modes.
Using our view modes - Views
Final result
Contextual links
Inline editing
Available in several hooks and methods
/**
* Implements hook_entity_view().
*/
function hook_entity_view(&$build, $entity, EntityViewDisplayInterface $display, $view_mode) {
......
}
Template suggestions
/**
* Implements hook_theme_suggestions_HOOK().
*/
function node_theme_suggestions_node(array $variables) {
$suggestions = array();
$node = $variables['elements']['#node'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'node__' . $sanitized_view_mode;
$suggestions[] = 'node__' . $node->bundle();
$suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'node__' . $node->id();
$suggestions[] = 'node__' . $node->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
Extra fields
Requirements that we want to achieve:
Not field components that we handle as fields at Manage fields and Manage form display sections
Exposing pseudo-fields
/**
* Implements hook_entity_extra_field_info().
*/
function display_modes_example_entity_extra_field_info() {
$extra = [];
$extra['tapa']['tapa']['display']['common_text'] = [
'label' => t('Common text'),
'description' => t('Common text for Tapas'),
'weight' => 0,
'visible' => TRUE,
];
return $extra;
}
Exposing pseudo-fields
Retrieving content for the pseudo field
/**
* Class TapaViewBuilder
*
* Render controller for Tapas.
*/
class TapaViewBuilder extends EntityViewBuilder {
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
parent::buildComponents($build, $entities, $displays, $view_mode);
foreach ($entities as $id => $entity) {
$bundle = $entity->bundle();
$display = $displays[$bundle];
if ($display->getComponent('common_text')) {
$build[$id]['common_text'] = [
'#theme' => 'common_text',
'#text' => t('Taste this and other awesome Tapas at this DrupalCamp Es!!! '),
];
}
}
}
}
Final Result
Pseudo-fields in forms
/**
* Implements hook_entity_extra_field_info().
*/
function display_modes_example_entity_extra_field_info() {
$extra = [];
$extra['tapa']['tapa']['display']['common_text'] = [
'label' => t('Common text'),
'description' => t('Common text for Tapas'),
'weight' => 0,
'visible' => TRUE,
];
$extra['tapa']['tapa']['form']['common_text_form'] = [
'label' => t('Common text Form'),
'description' => t('Common text for form Tapas'),
'weight' => 0,
'visible' => TRUE,
];
return $extra;
}
Pseudo-fields in forms
/**
* Form controller for Tapa edit forms.
*
* @ingroup display_modes_example
*/
class TapaForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\display_modes_example\Entity\Tapa */
$form = parent::buildForm($form, $form_state);
$display = $this->getFormDisplay($form_state);
if ($display->getComponent('common_text_form')) {
$form['common_text_form']['#markup'] = t('Common text form');
}
return $form;
}
....
....
@jose_lakatos
https://github.com/jlbellido
https://www.drupal.org/u/jlbellido