Repository URL to install this package:
|
Version:
3.1.2 ▾
|
novicell/dds_content
/
dds_content.tokens.inc
|
|---|
<?php
/**
* Implements hook_token_info().
*/
function dds_content_token_info()
{
return [
'types' => [
'dds_content' => [
'name' => 'DDS tokens',
'description' => '',
],
],
'tokens' => [
'dds_content' => [
'dds_content_primary_image' =>[
'name' => 'DDS Content primary image',
'description' => 'Calculates the primary image for nodes',
],
],
],
];
}
/**
* Implements hook_tokens().
*/
function dds_content_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata)
{
$replacements = [];
if ($type == 'dds_content') {
if (!empty($data['node'])) {
/** @var \Drupal\node\NodeInterface $node */
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'dds_content_primary_image':
$url = '';
if ($node->hasField('field_header_background') && !$node->field_header_background->isEmpty()) {
if($media_entity = $node->field_header_background->entity) {
if($thumbnail = $media_entity->thumbnail->entity) {
if($uri = $thumbnail->getFileUri()) {
$url = \Drupal::service('dds_media.media_helper')->get_styled_image_url($uri, 'large');
}
}
}
}
if ($url == '') {
//todo: use fallback image
}
$replacements[$original] = $url;
break;
}
}
}
}
return $replacements;
}