title.module in Title 8.2
Same filename and directory in other branches
Convert titles into fields which can be display manipulated.
File
title.moduleView source
<?php
/**
* @file
* Convert titles into fields which can be display manipulated.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
// The display region for a visible item.
define('TITLE_DISPLAY_REGION_ENABLED', 1);
// The display region for a disabled item.
define('TITLE_DISPLAY_REGION_DISABLED', 0);
/**
* Implements hook_form__FORM_ID_alter().
*/
function title_form_node_type_edit_form_alter(&$form, FormStateInterface $form_state) {
// Add additional settings to the node form using the 3rd party settings
// interface. See title.schema.yml.
$type = $form_state
->getFormObject()
->getEntity();
$config_manager = \Drupal::service('title.config_manager');
$form['title_settings'] = [
'#title' => t('Title Settings'),
'#group' => 'additional_settings',
'#type' => 'details',
'title_settings' => [
'display_configurable_title' => [
'#type' => 'checkbox',
'#title' => t('Display Configurable Title'),
'#description' => t('Allow the display of the node title to be configured on the "Manage Display" interface.'),
'#default_value' => $config_manager
->getEnabled($type
->id()),
],
],
];
$form['#entity_builders'][] = 'title_form_node_type_form_builder';
}
/**
* Entity builder for the node type form with title options.
*
* @see title_form_node_type_edit_form_alter().
*/
function title_form_node_type_form_builder($entity_type, $type, &$form, FormStateInterface $form_state) {
$config_manager = \Drupal::service('title.config_manager');
$state = $form_state
->getValue('display_configurable_title');
$config_manager
->setEnabled($type, $state);
// Setup some basic state such as placing the title into the correct regions
// on the view modes.
if ($state) {
$config_manager
->setupBundleState($type
->id());
}
else {
$config_manager
->tearDownBundleState($type
->id());
}
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function title_entity_base_field_info_alter(&$base_field_definitions, EntityTypeInterface $entity_type) {
// For all nodes, make the title display configurable. This is not possible
// on a per bundle basis, so it must be done for all nodes and then controlled
// individually on a bundle level.
if ($entity_type
->id() == 'node') {
$base_field_definitions['title']
->setDisplayConfigurable('view', TRUE);
}
}
/**
* Implements hook_form_alter().
*/
function title_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form['#entity_type'] !== 'node') {
return;
}
$config_manager = \Drupal::service('title.config_manager');
// Since a renderable title needs to be enabled at the entity level, hide the
// title field manually for bundles which aren't using a display configurable
// title.
if (!$config_manager
->getEnabled($form['#bundle'])) {
unset($form['fields']['title']);
}
}
/**
* Implements hook_entity_view_alter().
*/
function title_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
if ($entity
->getEntityTypeId() != 'node') {
return;
}
$config_manager = \Drupal::service('title.config_manager');
if (!$config_manager
->getEnabled($entity
->bundle())) {
return;
}
if (isset($display
->getComponent('title')['hidden'])) {
$build['title']['#title_hidden'] = TRUE;
}
}
/**
* Implements hook_preprocess_node().
*/
function title_preprocess_node(&$variables) {
// Restore the title field in the content array if display is being managed
// and is not hidden.
$config = \Drupal::service('title.config_manager');
if (!empty($variables['label']) && $config
->getEnabled($variables['node']
->bundle())) {
if (!isset($variables['label']['#title_hidden'])) {
$variables['content']['title'] = $variables['label'];
}
// The only way to hide the title in the core node template.
$variables['page'] = TRUE;
// Since titles are rendered in a block, they are pulled out of the page
// and rendered, setting #printed to TRUE.
// @see \Drupal\Core\Entity\Controller\EntityViewController::buildTitle.
if ($variables['view_mode'] == 'full' && isset($variables['content']['title'])) {
// @todo, find a way to deal with titles on full page views now that
// the title is in a block.
// $variables['content']['title']['#printed'] = FALSE;
}
}
}
/**
* Implements hook_entity_prepare_view().
*/
function title_entity_prepare_view($entity_type_id, array $entities, array $displays, $view_mode) {
if ($entity_type_id !== 'node') {
return;
}
// This is only required because template_preprocess_node assumes there
// is a key inside the renderable array inside the elements array:
// $variables['label'] = $variables['elements']['title'];
// Since it can be display configured to be removed, it disappaers from the
// elements list. We must reinstate if it has been hidden from display,
// but without content.
$config = \Drupal::service('title.config_manager');
foreach ($entities as $entity) {
if (!$config
->getEnabled($entity
->bundle())) {
continue;
}
$display = $displays[$entity
->bundle()];
if (!$display
->getComponent('title')) {
$display
->setComponent('title', [
'hidden' => TRUE,
]);
}
}
}
Functions
Name![]() |
Description |
---|---|
title_entity_base_field_info_alter | Implements hook_entity_base_field_info_alter(). |
title_entity_prepare_view | Implements hook_entity_prepare_view(). |
title_entity_view_alter | Implements hook_entity_view_alter(). |
title_form_entity_view_display_edit_form_alter | Implements hook_form_alter(). |
title_form_node_type_edit_form_alter | Implements hook_form__FORM_ID_alter(). |
title_form_node_type_form_builder | Entity builder for the node type form with title options. |
title_preprocess_node | Implements hook_preprocess_node(). |