You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.7 KiB
108 lines
3.7 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Presentation-only dynamic labels for existing text fields. |
|
*/ |
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
|
use Drupal\dynamic_field_label\LabelParser; |
|
|
|
/** |
|
* Implements hook_entity_view_alter(). |
|
*/ |
|
function dynamic_field_label_entity_view_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display): void { |
|
// Attach even when no fields are selected, so enabling a field invalidates |
|
// previously cached entity output. Preserve all existing cache metadata. |
|
CacheableMetadata::createFromRenderArray($build) |
|
->addCacheableDependency(\Drupal::config('dynamic_field_label.settings')) |
|
->applyTo($build); |
|
} |
|
|
|
/** |
|
* Implements hook_preprocess_field(). |
|
*/ |
|
function dynamic_field_label_preprocess_field(array &$variables): void { |
|
$element = $variables['element']; |
|
$config = \Drupal::config('dynamic_field_label.settings'); |
|
if (!in_array($element['#field_name'] ?? '', $config->get('fields') ?? [], TRUE)) { |
|
return; |
|
} |
|
$items = $element['#items'] ?? NULL; |
|
if (!$items || !in_array($items->getFieldDefinition()->getType(), ['string', 'string_long', 'text', 'text_long', 'text_with_summary'], TRUE)) { |
|
return; |
|
} |
|
// Only change items actually exposed by the formatter. Preserve fallback |
|
// content and item attributes for invalid values. |
|
$parsed = []; |
|
foreach ($variables['items'] as $delta => $item) { |
|
$raw = $items->get($delta)?->value; |
|
if (is_string($raw) && ($parts = LabelParser::parse($raw)) !== NULL) { |
|
$parsed[$delta] = $parts; |
|
} |
|
} |
|
if (!$parsed) { |
|
return; |
|
} |
|
$variables['dynamic_field_label_items'] = []; |
|
foreach ($variables['items'] as $delta => $item) { |
|
if (isset($parsed[$delta])) { |
|
$cacheability = CacheableMetadata::createFromRenderArray($item['content']); |
|
$cacheability->addCacheableDependency($config); |
|
$field_item = $items->get($delta); |
|
$type = $items->getFieldDefinition()->getType(); |
|
// Let Drupal apply the stored text format, including its cacheability. |
|
// Never interpret markup in an unformatted string field. |
|
if (in_array($type, ['text', 'text_long', 'text_with_summary'], TRUE) && !empty($field_item->format)) { |
|
$item['content'] = [ |
|
'#type' => 'processed_text', |
|
'#text' => $parsed[$delta]['value'], |
|
'#format' => $field_item->format, |
|
'#langcode' => $items->getLangcode(), |
|
]; |
|
} |
|
else { |
|
$item['content'] = ['#plain_text' => $parsed[$delta]['value']]; |
|
} |
|
$cacheability->applyTo($item['content']); |
|
$item['dynamic_label'] = $parsed[$delta]['label']; |
|
} |
|
else { |
|
$item['dynamic_label'] = $variables['label_hidden'] ? NULL : $variables['label']; |
|
} |
|
$variables['dynamic_field_label_items'][] = $item; |
|
} |
|
|
|
} |
|
|
|
/** |
|
* Implements hook_theme(). |
|
*/ |
|
function dynamic_field_label_theme(): array { |
|
return [ |
|
'field__dynamic_field_label' => [ |
|
'base hook' => 'field', |
|
'template' => 'field--dynamic-field-label', |
|
], |
|
]; |
|
} |
|
|
|
/** |
|
* Implements hook_theme_suggestions_field_alter(). |
|
*/ |
|
function dynamic_field_label_theme_suggestions_field_alter(array &$suggestions, array $variables): void { |
|
$element = $variables['element']; |
|
$items = $element['#items'] ?? NULL; |
|
if (!$items || !in_array($element['#field_name'] ?? '', \Drupal::config('dynamic_field_label.settings')->get('fields') ?? [], TRUE)) { |
|
return; |
|
} |
|
if (!in_array($items->getFieldDefinition()->getType(), ['string', 'string_long', 'text', 'text_long', 'text_with_summary'], TRUE)) { |
|
return; |
|
} |
|
foreach ($items as $item) { |
|
if (is_string($item->value) && LabelParser::parse($item->value) !== NULL) { |
|
$suggestions[] = 'field__dynamic_field_label'; |
|
return; |
|
} |
|
} |
|
}
|
|
|