function social_follow_taxonomy_tokens in Open Social 10.3.x
Same name and namespace in other branches
- 10.0.x modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module \social_follow_taxonomy_tokens()
- 10.1.x modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module \social_follow_taxonomy_tokens()
- 10.2.x modules/social_features/social_follow_taxonomy/social_follow_taxonomy.module \social_follow_taxonomy_tokens()
Implements hook_tokens().
{ @inheritdoc }
File
- modules/
social_features/ social_follow_taxonomy/ social_follow_taxonomy.module, line 228 - Contains social_follow_taxonomy.module.
Code
function social_follow_taxonomy_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$display_name = '';
if (empty($data['message'])) {
return $replacements;
}
if ($type === 'social_taxonomy') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'content_type':
case 'indefinite_article':
case 'taxonomy_i_follow':
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
// Get the related entity.
if (isset($message->field_message_related_object)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
$entity = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
if (is_object($entity)) {
switch ($target_type) {
case 'node':
/** @var \Drupal\node\Entity\Node $entity */
if ($entity instanceof NodeInterface) {
// Get the name of the content.
$display_name = strtolower($entity->type->entity
->label());
// Get the names of terms related to the content.
$term_ids = social_follow_taxonomy_terms_list($entity);
$term_names = [];
foreach ($term_ids as $term_id) {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($term_id);
if ($term instanceof TermInterface) {
if (social_follow_taxonomy_term_followed($term)) {
$term_names[] = $term
->getName();
}
}
}
}
break;
case 'post':
// Get the name of the entity type.
$display_name = strtolower($entity
->getEntityType()
->getLabel());
break;
}
}
}
if ($name === 'content_type') {
$replacements[$original] = $display_name;
}
if ($name === 'indefinite_article') {
if (!empty($display_name)) {
// Prepares a replacement token: content name.
// When a name of content name starts from a vowel letter then
// will be added "an" before this name. For example "an event".
if (preg_match('/^[aeiou]/', $display_name)) {
$indefinite_article = t('an');
}
else {
$indefinite_article = t('a');
}
$replacements[$original] = $indefinite_article;
}
else {
$replacements[$original] = '';
}
}
if ($name === 'taxonomy_i_follow' && !empty($term_names)) {
// Prepares a replacement token: a string with term names.
// Wrap the names in quotation marks and separate it with commas.
$replacement_string = "'" . implode("', '", $term_names) . "'";
$replacements[$original] = $replacement_string;
}
break;
}
}
}
if ($type === 'message') {
/** @var \Drupal\message\MessageInterface $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
if ($name === 'post_url' && isset($message->field_message_related_object)) {
$replacements[$original] = '';
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
$entity = Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
if ($entity) {
if ($target_type === 'post') {
$post_link = Url::fromRoute('entity.post.canonical', [
'post' => $entity
->id(),
], [
'absolute' => TRUE,
])
->toString();
$replacements[$original] = $post_link;
}
}
}
}
}
return $replacements;
}