Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / media_entity / media_entity.tokens.inc
1 <?php
2
3 /**
4  * @file
5  * Builds placeholder replacement tokens for media_entity-related data.
6  */
7
8 use Drupal\Component\Utility\Html;
9 use Drupal\Core\Datetime\Entity\DateFormat;
10 use Drupal\Core\Language\LanguageInterface;
11 use Drupal\Core\Render\BubbleableMetadata;
12 use Drupal\media_entity\Entity\MediaBundle;
13
14 /**
15  * Implements hook_token_info().
16  */
17 function media_entity_token_info() {
18   $type = [
19     'name' => t('Media'),
20     'description' => t('Tokens related to individual media items.'),
21     'needs-data' => 'media',
22   ];
23
24   // Core tokens for media.
25   $media['mid'] = [
26     'name' => t("Media ID"),
27     'description' => t('The unique ID of the media item.'),
28   ];
29   $media['uuid'] = [
30     'name' => t("Media UUID"),
31     'description' => t('The unique UUID of the media item.'),
32   ];
33   $media['vid'] = [
34     'name' => t("Revision ID"),
35     'description' => t("The unique ID of the media's latest revision."),
36   ];
37   $media['bundle'] = [
38     'name' => t("Media bundle"),
39   ];
40   $media['bundle-name'] = [
41     'name' => t("Media bundle name"),
42     'description' => t("The human-readable name of the media bundle."),
43   ];
44   $media['langcode'] = [
45     'name' => t('Language code'),
46     'description' => t('The language code of the language the media is written in.'),
47   ];
48   $media['name'] = [
49     'name' => t('Name'),
50     'description' => t('The name of this media.'),
51   ];
52   $media['type'] = [
53     'name' => t("Type"),
54     'description' => t("The type of this media."),
55   ];
56   $node['author'] = [
57     'name' => t("Author"),
58     'type' => 'user',
59   ];
60   $media['url'] = [
61     'name' => t("URL"),
62     'description' => t("The URL of the media."),
63   ];
64   $media['edit-url'] = [
65     'name' => t("Edit URL"),
66     'description' => t("The URL of the media's edit page."),
67   ];
68
69   // Chained tokens for media.
70   $media['created'] = [
71     'name' => t("Date created"),
72     'type' => 'date',
73   ];
74   $media['changed'] = [
75     'name' => t("Date changed"),
76     'description' => t("The date the media was most recently updated."),
77     'type' => 'date',
78   ];
79
80   return [
81     'types' => ['media' => $type],
82     'tokens' => ['media' => $media],
83   ];
84 }
85
86 /**
87  * Implements hook_tokens().
88  */
89 function media_entity_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
90   $token_service = \Drupal::token();
91
92   $url_options = ['absolute' => TRUE];
93   if (isset($options['langcode'])) {
94     $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
95     $langcode = $options['langcode'];
96   }
97   else {
98     $langcode = LanguageInterface::LANGCODE_DEFAULT;
99   }
100   $sanitize = !empty($options['sanitize']);
101
102   $replacements = [];
103
104   if ($type == 'media' && !empty($data['media'])) {
105     /** @var \Drupal\media_entity\MediaInterface $media */
106     $media = \Drupal::service('entity.repository')->getTranslationFromContext($data['media'], $langcode, ['operation' => 'media_entity_tokens']);
107
108     foreach ($tokens as $name => $original) {
109       switch ($name) {
110         // Simple key values on the media_entity.
111         case 'mid':
112           $replacements[$original] = $media->id();
113           break;
114
115         case 'uuid':
116           $replacements[$original] = $media->uuid();
117           break;
118
119         case 'vid':
120           $replacements[$original] = $media->getRevisionId();
121           break;
122
123         case 'bundle':
124           $replacements[$original] = $sanitize ? Html::escape($media->bundle()) : $media->bundle();
125           break;
126
127         case 'bundle-name':
128           $bundle_name = MediaBundle::load($media->bundle())->label();
129           $replacements[$original] = $sanitize ? Html::escape($bundle_name) : $bundle_name;
130           break;
131
132         case 'langcode':
133           $replacements[$original] = $sanitize ? Html::escape($media->language()->getId()) : $media->language()->getId();
134           break;
135
136         case 'name':
137           $media_name = $media->get('name')->value;
138           $replacements[$original] = $sanitize ? Html::escape($media_name) : $media_name;
139           break;
140
141         case 'type':
142           $media_type = $media->get('name')->value;
143           $replacements[$original] = $sanitize ? Html::escape($media_type) : $media_type;
144           break;
145
146         case 'url':
147           $replacements[$original] = $media->toUrl('canonical', $url_options);
148           break;
149
150         case 'edit-url':
151           $replacements[$original] = $media->toUrl('edit-form', $url_options);
152           break;
153
154         // Default values for the chained tokens handled below.
155         case 'author':
156           /** @var \Drupal\user\UserInterface $account */
157           $account = $media->get('uid')->entity;
158           $bubbleable_metadata->addCacheableDependency($account);
159           $replacements[$original] = $sanitize ? Html::escape($account->label()) : $account->label();
160           break;
161
162         case 'created':
163           $date_format = DateFormat::load('medium');
164           $bubbleable_metadata->addCacheableDependency($date_format);
165           $replacements[$original] = \Drupal::service('date.formatter')
166             ->format($media->getCreatedTime(), 'medium', '', NULL, $langcode);
167           break;
168
169         case 'changed':
170           $date_format = DateFormat::load('medium');
171           $bubbleable_metadata->addCacheableDependency($date_format);
172           $replacements[$original] = \Drupal::service('date.formatter')
173             ->format($media->getChangedTime(), 'medium', '', NULL, $langcode);
174           break;
175       }
176     }
177
178     if ($author_tokens = $token_service->findWithPrefix($tokens, 'author')) {
179       $account = $media->get('uid')->entity;
180       $replacements += $token_service->generate('user', $author_tokens, ['user' => $account], $options, $bubbleable_metadata);
181     }
182
183     if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) {
184       $replacements += $token_service->generate('date', $created_tokens, ['date' => $media->getCreatedTime()], $options, $bubbleable_metadata);
185     }
186
187     if ($changed_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
188       $replacements += $token_service->generate('date', $changed_tokens, ['date' => $media->getChangedTime()], $options, $bubbleable_metadata);
189     }
190   }
191
192   return $replacements;
193 }