display_fields-8.x-1.x-dev/display_fields.module
display_fields.module
<?php
/**
* @file
* Hooks implementation for the Display Fields module.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\display_fields\DisplayFields;
/**
* Implements hook_entity_view_alter().
*/
function display_fields_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$view_mode = $display->get('mode');
$language = $entity->language()->getId();
// Put #entity_type, #bundle and #layout on the build
$build['#entity_type'] = $entity_type;
$build['#bundle'] = $bundle;
// Add Display Fields fields.
$display_fields = DisplayFields::getDisplayFields($entity_type, $bundle);
$display_fields_display = display_fields_get_entity_view_settings($entity_type, $bundle, $view_mode);
$display_fields_fields = $display_fields->get('display_fields');
if (empty($display_fields_fields)) {
return;
}
foreach ($display_fields_fields as $key => $display_field) {
$display_settings = $display_fields_display->getComponent($key);
if (empty($display_settings)) {
// The field is hidden.
continue;
}
$plugin = DisplayFields::getDisplayFieldsField($display_field['plugin_id'], $entity_type, $bundle);
if (!$plugin) {
continue;
}
$build["display_fields_$key"] = $plugin->getFieldBuild([$entity], $display_field, $display_settings, $entity, $view_mode, $language);
$build["display_fields_$key"]['#weight'] = $display_settings['weight'];
}
}
/**
* Gets the display fields view settings associated to a bundle and view mode.
*
* Use this function when assigning suggested display options for a component
* in a given view mode. Note that they will only be actually used at render
* time if the view mode itself is configured to use dedicated display settings
* for the bundle; if not, the 'default' display is used instead.
*
* The function reads the entity view display from the current configuration, or
* returns a ready-to-use empty one if configuration entry exists yet for this
* bundle and view mode. This streamlines manipulation of display objects by
* always returning a consistent object that reflects the current state of the
* configuration.
*
* Example usage:
* - Set the 'body' field to be displayed and the 'field_image' field to be
* hidden on article nodes in the 'default' display.
*
* @code
* display_fields_get_entity_view_settings('node', 'article', 'default')
* ->setComponent('body', array(
* 'type' => 'text_summary_or_trimmed',
* 'settings' => array('trim_length' => '200')
* 'weight' => 1,
* ))
* ->removeComponent('field_image')
* ->save();
* @endcode
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle.
* @param string $view_mode
* The view mode, or 'default' to retrieve the 'default' display object for
* this bundle.
*
* @return \Drupal\Core\Entity\Display\EntityDisplayInterface
* The entity view display associated to the view mode.
*/
function display_fields_get_entity_view_settings(string $entity_type, string $bundle, string $view_mode) {
// Try loading the display from configuration.
$display = \Drupal::entityTypeManager()
->getStorage('display_fields_view')
->load($entity_type . '.' . $bundle . '.' . $view_mode);
// If not found, create a fresh display object. We do not preemptively create
// new entity_view_display configuration entries for each existing entity
// type and bundle whenever a new view mode becomes available. Instead,
// configuration entries are only created when a display object is explicitly
// configured and saved.
if (!$display) {
$display = \Drupal::entityTypeManager()->getStorage('display_fields_view')->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $display;
}
/**
* Gets the option list array.
*
* @param string $entity_type
* The entity type for which the option list is retrieved.
*
* @return array
* The option list.
*/
function display_fields_get_field_types($entity_type) {
$options = [];
$displayFieldsFieldDefinitions = DisplayFields::getDisplayFieldsFieldDefinitions($entity_type);
foreach ($displayFieldsFieldDefinitions as $plugin_id => $plugin) {
$options[$plugin_id] = $plugin['title'];
}
return $options;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function display_fields_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {
$form_state->loadInclude('display_fields', 'inc', 'includes/display_fields.field_ui');
_display_fields_form_field_ui_display($form, $form_state);
}
