Skip to content

Commit

Permalink
Added token support in Next Entity Type additional urls
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 20, 2024
1 parent 059a432 commit 444c415
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
40 changes: 40 additions & 0 deletions modules/stanford_decoupled/src/Plugin/Next/Revalidator/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Drupal\stanford_decoupled\Plugin\Next\Revalidator;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\next\Event\EntityActionEvent;
use Drupal\next\Plugin\Next\Revalidator\Path as NextPath;

/**
* Overridden next module path plugin to support tokens in the additional paths.
*
* @codeCoverageIgnore
*/
class Path extends NextPath {

/**
* {@inheritDoc}
*/
public function revalidate(EntityActionEvent $event): bool {
$this->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]);
}

}
49 changes: 49 additions & 0 deletions modules/stanford_decoupled/stanford_decoupled.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down Expand Up @@ -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().
*/
Expand Down

0 comments on commit 444c415

Please sign in to comment.