From 444c4155d4dd7ace480ce40aedc23a4e28150acf Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Wed, 20 Nov 2024 10:11:03 -0800 Subject: [PATCH] Added token support in Next Entity Type additional urls --- .../src/Plugin/Next/Revalidator/Path.php | 40 +++++++++++++++ .../stanford_decoupled.module | 49 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 modules/stanford_decoupled/src/Plugin/Next/Revalidator/Path.php diff --git a/modules/stanford_decoupled/src/Plugin/Next/Revalidator/Path.php b/modules/stanford_decoupled/src/Plugin/Next/Revalidator/Path.php new file mode 100644 index 00000000..1ef808ce --- /dev/null +++ b/modules/stanford_decoupled/src/Plugin/Next/Revalidator/Path.php @@ -0,0 +1,40 @@ +configuration['additional_paths'] = self::adjustAdditionalPaths($this->configuration['additional_paths'], $event->getEntity()); + return parent::revalidate($event); + } + + /** + * Convert tokens in the additional paths. + * + * @param string $paths + * Additional paths config. + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * Entity being revalidated. + * + * @return string + * Adjusted paths. + */ + protected static function adjustAdditionalPaths(string $paths, ContentEntityInterface $entity) { + return \Drupal::token() + ->replace($paths, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]); + } + +} diff --git a/modules/stanford_decoupled/stanford_decoupled.module b/modules/stanford_decoupled/stanford_decoupled.module index f90d21c5..673a0e7b 100644 --- a/modules/stanford_decoupled/stanford_decoupled.module +++ b/modules/stanford_decoupled/stanford_decoupled.module @@ -16,6 +16,13 @@ use Drupal\paragraphs\ParagraphInterface; use Drupal\stanford_decoupled\Config\DecoupledConfigOverrides; use Drupal\views\ViewExecutable; +/** + * Implements hook_next_revalidator_info_alter(). + */ +function stanford_decoupled_next_revalidator_info_alter(&$plugins) { + $plugins['path']['class'] = 'Drupal\stanford_decoupled\Plugin\Next\Revalidator\Path'; +} + /** * Implements hook_graphql_compose_field_type_alter(). */ @@ -48,6 +55,48 @@ function stanford_decoupled_layout_access(EntityInterface $entity, $operation, A return AccessResult::allowedIf($operation == 'view' && DecoupledConfigOverrides::isDecoupled()); } +/** + * Implements hook_token_info(). + */ +function stanford_decoupled_token_info() { + // Drupal 11.1.0 already has the UUID token. This can be removed at that time. + if (version_compare(\Drupal::VERSION, '11.1', '>=')) { + return []; + } + $entity_types = \Drupal::entityTypeManager()->getDefinitions(); + $info['tokens'] = []; + + foreach ($entity_types as $entity_id => $entity_type) { + if ($entity_type->getGroup() == 'content') { + $info['tokens'][$entity_id]['uuid'] = [ + 'name' => t('@entity_id UUID', ['@entity_id' => $entity_type->getLabel()]), + 'description' => t('The Universal Unique Identifier of @entity_id', ['@entity_id' => $entity_type->getLabel()]), + ]; + } + } + return $info; +} + +/** + * Implements hook_tokens(). + */ +function stanford_decoupled_tokens($type, $tokens, array $data = [], array $options = []) { + $replacements = []; + if (!empty($data[$type]) && method_exists($data[$type], 'uuid')) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $data[$type]; + + foreach ($tokens as $name => $original) { + switch ($name) { + case 'uuid': + $replacements[$original] = $entity->uuid(); + break; + } + } + } + return $replacements; +} + /** * Implements hook_cron(). */