-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity_summary.tokens.inc
57 lines (43 loc) · 1.56 KB
/
entity_summary.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\entity_summary\GeneratedSummary;
function entity_summary_token_info_alter(&$data) {
foreach ($data['types'] as $type_key => $info) {
if (empty($info['needs-data']) || $info['needs-data'] != $type_key) {
continue;
}
if (empty($data['tokens'][$type_key])) {
continue;
}
$entity_definition = \Drupal::entityTypeManager()->getDefinition($type_key, FALSE);
if (!$entity_definition) {
continue;
}
if (!($entity_definition instanceof ContentEntityType)) {
continue;
}
$data['tokens'][$type_key]['entity_summary'] = [
'name' => t("@entity_type summary", ['@entity_type' => $entity_definition->getLabel()]),
'description' => t("An automatically generated summary of the @entity_type.", ['@entity_type' => $entity_definition->getLabel()]),
];
}
}
function entity_summary_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
foreach ($tokens as $name => $original) {
if ($name != 'entity_summary') {
continue;
}
if (empty($data[$type])) {;
continue;
}
$summary = \Drupal::service('entity_summary.caching_generator')->generate($data[$type]);
$summary_object = GeneratedSummary::createFromObject($summary);
if (!$summary_object->isEmpty()) {
$replacements[$original] = $summary_object->getSummary();
}
$bubbleable_metadata->addCacheableDependency($summary_object);
}
return $replacements;
}