function empty_fields_entity_display_build_alter in Empty fields 8
Implements hook_entity_display_build_alter().
File
- ./
empty_fields.module, line 103 - Contains the implementation for the empty_fields module.
Code
function empty_fields_entity_display_build_alter(&$build, $context) {
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $context['entity'];
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
$display = $context['display'];
/** @var \Drupal\empty_fields\EmptyFieldsPluginManager $manager */
$manager = \Drupal::service('plugin.manager.empty_fields');
foreach (Element::children($build) as $field_name) {
if ($entity
->get($field_name)
->isEmpty() && $entity->{$field_name}
->access('view') && ($formatter = $display
->getRenderer($field_name)) && ($handler = $formatter
->getThirdPartySetting('empty_fields', 'handler', ''))) {
$configuration = $formatter
->getThirdPartySetting('empty_fields', 'settings', []);
/** @var \Drupal\empty_fields\EmptyFieldPluginInterface $plugin */
$plugin = $manager
->createInstance($handler, $configuration);
$definition = $entity
->get($field_name)
->getFieldDefinition();
$component = $display
->getComponent($field_name);
$build[$field_name] = [
'#theme' => 'field',
'#title' => $definition
->getLabel(),
'#label_display' => $component['label'],
'#view_mode' => $context['view_mode'],
'#language' => $entity
->get($field_name)
->getLangcode(),
'#field_name' => $definition
->getName(),
'#field_type' => 'string',
'#field_translatable' => $definition
->isTranslatable(),
'#entity_type' => $entity
->getEntityTypeId(),
'#bundle' => $entity
->bundle(),
'#object' => $entity,
'#items' => [
(object) [
'_attributes' => [],
],
],
'#is_multiple' => FALSE,
'#attributes' => [
'class' => [
'empty-fields',
"empty-fields__{$handler}",
],
],
// Use simple formatter.
'#formatter' => 'string',
'0' => $plugin
->react($context),
] + $build[$field_name];
}
}
}