Skip to content
Snippets Groups Projects
Commit ed83d764 authored by Jonathan Smith's avatar Jonathan Smith
Browse files

Issue #3397181 by jonathan1055, FeyP: Wrong token type used for taxonomy terms

parent 0955a8cb
No related branches found
No related tags found
1 merge request!108Issue #3397181: Wrong token type for taxonomy terms
Pipeline #77006 passed with warnings
......@@ -7,15 +7,32 @@
use Drupal\Core\Render\BubbleableMetadata;
/**
* Internal function to return the token type ids for supported entity types.
*/
function _scheduler_token_types() {
static $token_types;
if (!empty($token_types)) {
return $token_types;
}
$plugin_types = \Drupal::service('scheduler.manager')->getPluginEntityTypes();
// This function should only get executed if the Token module is enabled,
// therefore we can assume the token.entity_mapper service exists without
// having to check for it.
foreach ($plugin_types as $type) {
$token_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($type, TRUE);
$token_types[] = $token_type;
}
return $token_types;
}
/**
* Implements hook_token_info().
*/
function scheduler_token_info() {
$plugin_types = \Drupal::service('scheduler.manager')->getPluginEntityTypes();
// Initialise the array to avoid 'variable is undefined' phpcs error.
$info = [];
foreach ($plugin_types as $type) {
foreach (_scheduler_token_types() as $type) {
$info['tokens'][$type]['scheduler-publish'] = [
'name' => t('Publish on date'),
'description' => t("The date the %type will be published.", ['%type' => $type]),
......@@ -27,7 +44,6 @@ function scheduler_token_info() {
'type' => 'date',
];
}
return $info;
}
......@@ -40,9 +56,7 @@ function scheduler_tokens($type, $tokens, array $data, array $options, Bubbleabl
$language_code = $options['langcode'] ?? NULL;
$replacements = [];
$plugin_types = \Drupal::service('scheduler.manager')->getPluginEntityTypes();
if (in_array($type, $plugin_types) && !empty($data[$type])) {
if (in_array($type, _scheduler_token_types()) && !empty($data[$type])) {
$entity = $data[$type];
foreach ($tokens as $name => $original) {
......
......@@ -20,6 +20,9 @@ class SchedulerTokenReplaceTest extends SchedulerBrowserTestBase {
// Define timestamps for consistent use when repeated throughout this test.
$publish_on_timestamp = $this->requestTime + 3600;
$unpublish_on_timestamp = $this->requestTime + 7200;
// Derive the token type id from the entity type id. Use second parameter
// TRUE to fall back to the input value if the mapping is not found.
$tokenTypeId = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entityTypeId, TRUE);
// Create an unpublished entity with scheduled dates.
$entity = $this->createEntity($entityTypeId, $bundle, [
......@@ -48,7 +51,7 @@ class SchedulerTokenReplaceTest extends SchedulerBrowserTestBase {
foreach ($test_cases as $test_data) {
// Edit the entity and set the body tokens to use the format being tested.
$edit = [
"{$this->bodyField($entityTypeId)}[0][value]" => "Publish on: [{$entityTypeId}:scheduler-publish{$test_data['token_format']}]. Unpublish on: [{$entityTypeId}:scheduler-unpublish{$test_data['token_format']}].",
"{$this->bodyField($entityTypeId)}[0][value]" => "Publish on: [{$tokenTypeId}:scheduler-publish{$test_data['token_format']}]. Unpublish on: [{$tokenTypeId}:scheduler-unpublish{$test_data['token_format']}].",
];
$this->drupalGet($entity->toUrl('edit-form'));
$this->submitForm($edit, 'Save');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment