function media_entity_tokens in Media entity 8
Implements hook_tokens().
File
- ./
media_entity.tokens.inc, line 89 - Builds placeholder replacement tokens for media_entity-related data.
Code
function media_entity_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
$url_options = [
'absolute' => TRUE,
];
if (isset($options['langcode'])) {
$url_options['language'] = \Drupal::languageManager()
->getLanguage($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = LanguageInterface::LANGCODE_DEFAULT;
}
$sanitize = !empty($options['sanitize']);
$replacements = [];
if ($type == 'media' && !empty($data['media'])) {
/** @var \Drupal\media_entity\MediaInterface $media */
$media = \Drupal::service('entity.repository')
->getTranslationFromContext($data['media'], $langcode, [
'operation' => 'media_entity_tokens',
]);
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the media_entity.
case 'mid':
$replacements[$original] = $media
->id();
break;
case 'uuid':
$replacements[$original] = $media
->uuid();
break;
case 'vid':
$replacements[$original] = $media
->getRevisionId();
break;
case 'bundle':
$replacements[$original] = $sanitize ? Html::escape($media
->bundle()) : $media
->bundle();
break;
case 'bundle-name':
$bundle_name = MediaBundle::load($media
->bundle())
->label();
$replacements[$original] = $sanitize ? Html::escape($bundle_name) : $bundle_name;
break;
case 'langcode':
$replacements[$original] = $sanitize ? Html::escape($media
->language()
->getId()) : $media
->language()
->getId();
break;
case 'name':
$media_name = $media
->get('name')->value;
$replacements[$original] = $sanitize ? Html::escape($media_name) : $media_name;
break;
case 'type':
$media_type = $media
->get('name')->value;
$replacements[$original] = $sanitize ? Html::escape($media_type) : $media_type;
break;
case 'url':
$replacements[$original] = $media
->toUrl('canonical', $url_options);
break;
case 'edit-url':
$replacements[$original] = $media
->toUrl('edit-form', $url_options);
break;
// Default values for the chained tokens handled below.
case 'author':
/** @var \Drupal\user\UserInterface $account */
$account = $media
->get('uid')->entity;
$bubbleable_metadata
->addCacheableDependency($account);
$replacements[$original] = $sanitize ? Html::escape($account
->label()) : $account
->label();
break;
case 'created':
$date_format = DateFormat::load('medium');
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($media
->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$date_format = DateFormat::load('medium');
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($media
->getChangedTime(), 'medium', '', NULL, $langcode);
break;
}
}
if ($author_tokens = $token_service
->findWithPrefix($tokens, 'author')) {
$account = $media
->get('uid')->entity;
$replacements += $token_service
->generate('user', $author_tokens, [
'user' => $account,
], $options, $bubbleable_metadata);
}
if ($created_tokens = $token_service
->findWithPrefix($tokens, 'created')) {
$replacements += $token_service
->generate('date', $created_tokens, [
'date' => $media
->getCreatedTime(),
], $options, $bubbleable_metadata);
}
if ($changed_tokens = $token_service
->findWithPrefix($tokens, 'changed')) {
$replacements += $token_service
->generate('date', $changed_tokens, [
'date' => $media
->getChangedTime(),
], $options, $bubbleable_metadata);
}
}
return $replacements;
}