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.
146 lines
4.6 KiB
146 lines
4.6 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Presentation-only dynamic labels for existing text fields. |
|
*/ |
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
|
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; |
|
use Drupal\Core\Entity\EntityInterface; |
|
use Drupal\Core\Render\Element; |
|
use Drupal\dynamic_field_label\LabelParser; |
|
|
|
/** |
|
* Implements hook_entity_view_alter(). |
|
*/ |
|
function dynamic_field_label_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void { |
|
// Register the dependency before rendering, even when no fields are selected. |
|
// Selecting or deselecting fields must invalidate cached entity output. |
|
\Drupal::service('renderer')->addCacheableDependency( |
|
$build, |
|
\Drupal::config('dynamic_field_label.settings'), |
|
); |
|
} |
|
|
|
/** |
|
* 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 { |
|
if (_dynamic_field_label_parse_items($variables['element'])) { |
|
$suggestions[] = 'field__dynamic_field_label'; |
|
} |
|
} |
|
|
|
/** |
|
* Implements hook_preprocess_field(). |
|
*/ |
|
function dynamic_field_label_preprocess_field(array &$variables): void { |
|
$element = $variables['element']; |
|
$type = _dynamic_field_label_text_type($element); |
|
if ($type === NULL) { |
|
return; |
|
} |
|
|
|
$config = \Drupal::config('dynamic_field_label.settings'); |
|
$renderer = \Drupal::service('renderer'); |
|
|
|
// Also cover fields rendered independently of an entity view, including |
|
// currently unselected fields and fields with malformed input. |
|
foreach ($variables['items'] as &$item) { |
|
$renderer->addCacheableDependency($item['content'], $config); |
|
} |
|
unset($item); |
|
|
|
$parsed = _dynamic_field_label_parse_items($element); |
|
if (!$parsed) { |
|
return; |
|
} |
|
|
|
$items = $element['#items']; |
|
$variables['dynamic_field_label_items'] = []; |
|
foreach ($variables['items'] as $delta => $item) { |
|
if (isset($parsed[$delta])) { |
|
// Save metadata before replacing the original formatter render array. |
|
$cacheability = CacheableMetadata::createFromRenderArray($item['content']); |
|
$field_item = $items->get($delta); |
|
|
|
// Respect the stored text format. Plain text must never become HTML. |
|
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']]; |
|
} |
|
|
|
// Restore original metadata, including the configuration dependency. |
|
// The processed_text element adds its own filter dependencies on render. |
|
$cacheability->applyTo($item['content']); |
|
$item['dynamic_label'] = $parsed[$delta]['label']; |
|
} |
|
else { |
|
// Preserve the original formatter output and label for invalid input. |
|
$item['dynamic_label'] = $variables['label_hidden'] ? NULL : $variables['label']; |
|
} |
|
$variables['dynamic_field_label_items'][$delta] = $item; |
|
} |
|
} |
|
|
|
/** |
|
* Returns the field type if it is supported, or NULL otherwise. |
|
*/ |
|
function _dynamic_field_label_text_type(array $element): ?string { |
|
$items = $element['#items'] ?? NULL; |
|
if ($items === NULL) { |
|
return NULL; |
|
} |
|
|
|
$type = $items->getFieldDefinition()->getType(); |
|
return in_array($type, ['string', 'string_long', 'text', 'text_long', 'text_with_summary'], TRUE) ? $type : NULL; |
|
} |
|
|
|
/** |
|
* Returns parsed values keyed by the deltas exposed by the field formatter. |
|
* |
|
* Both theme suggestion and preprocessing use this method so a hidden or |
|
* omitted value cannot independently trigger the dynamic field template. |
|
*/ |
|
function _dynamic_field_label_parse_items(array $element): array { |
|
if (_dynamic_field_label_text_type($element) === NULL) { |
|
return []; |
|
} |
|
|
|
$fields = \Drupal::config('dynamic_field_label.settings')->get('fields') ?? []; |
|
if (!in_array($element['#field_name'] ?? '', $fields, TRUE)) { |
|
return []; |
|
} |
|
|
|
$parsed = []; |
|
foreach (Element::children($element) as $delta) { |
|
if (!is_int($delta) && !ctype_digit((string) $delta)) { |
|
continue; |
|
} |
|
$raw = $element['#items']->get((int) $delta)?->value; |
|
if (is_string($raw) && ($parts = LabelParser::parse($raw)) !== NULL) { |
|
$parsed[$delta] = $parts; |
|
} |
|
} |
|
return $parsed; |
|
}
|
|
|