diff --git a/token.tokens.inc b/token.tokens.inc index c2da3cb..5511cf5 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -25,6 +25,7 @@ use Drupal\Core\TypedData\PrimitiveInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\image\Entity\ImageStyle; +use Drupal\Core\Language\LanguageInterface; /** * Implements hook_token_info_alter(). @@ -171,6 +172,11 @@ function token_token_info() { // Taxonomy term and vocabulary tokens. if (\Drupal::moduleHandler()->moduleExists('taxonomy')) { + $info['tokens']['term']['source'] = [ + 'name' => t('Translation source term'), + 'description' => t("The source term for this current term's translation set."), + 'type' => 'term', + ]; $info['tokens']['term']['edit-url'] = array( 'name' => t('Edit URL'), 'description' => t("The URL of the taxonomy term's edit page."), @@ -187,6 +193,11 @@ function token_token_info() { 'type' => 'term', ); + $info['tokens']['vocabulary']['source'] = [ + 'name' => t('Translation source vocabulary'), + 'description' => t("The source vocabulary for this current vocabulary's translation set."), + 'type' => 'vocabulary', + ]; $info['tokens']['vocabulary']['machine-name'] = array( 'name' => t('Machine-readable name'), 'description' => t('The unique machine-readable name of the vocabulary.'), @@ -560,6 +571,11 @@ function token_tokens($type, array $tokens, array $data = array(), array $option } // Chained token relationships. + if (($parent_tokens = \Drupal::token()->findWithPrefix($tokens, 'source')) && $source_term = \Drupal::service('entity.repository')->getTranslationFromContext($term, LanguageInterface::LANGCODE_DEFAULT)) { + $replacements += \Drupal::token()->generate('term', $parent_tokens, [ + 'term' => $source_term, + ], $options, $bubbleable_metadata); + } if (($url_tokens = \Drupal::token()->findWithPrefix($tokens, 'url'))) { $replacements += \Drupal::token()->generate('url', $url_tokens, array('url' => $term->toUrl()), $options, $bubbleable_metadata); } @@ -592,6 +608,12 @@ function token_tokens($type, array $tokens, array $data = array(), array $option break; } } + + // Chained token relationships. + if (($parent_tokens = \Drupal::token()->findWithPrefix($tokens, 'source')) && $source_vocabulary = \Drupal::service('entity.repository')->getTranslationFromContext($vocabulary, LanguageInterface::LANGCODE_DEFAULT)) { + $replacements += \Drupal::token()->generate('vocabulary', $parent_tokens, + $options, $bubbleable_metadata); + } } // File tokens.