collection-8.x-1.x-dev/modules/collection_pathauto/collection_pathauto.module
modules/collection_pathauto/collection_pathauto.module
<?php
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Implements hook_pathauto_alias_alter().
*
* Checks for a canonical collection item and prepends the collection url to the
* path. It also implements an alter hook so that other modules can further
* modify the alias.
*/
function collection_pathauto_pathauto_alias_alter(&$alias, array &$context) {
$collection_content_manager = \Drupal::service('collection.content_manager');
$path_alias_manager = \Drupal::service('path_alias.manager');
$entity = reset($context['data']);
if (empty($entity) || !$entity instanceof ContentEntityInterface) {
return;
}
foreach ($collection_content_manager->getCollectionItemsForEntity($entity) as $collection_item) {
if ($collection_item->isCanonical()) {
$collection_url = $path_alias_manager->getAliasByPath('/collection/' . $collection_item->collection->target_id);
$context['original_alias'] = $alias;
$alias = $collection_url . $alias;
// Add relevant collection details to the context and allow other modules to
// alter the alias.
$context['collection_item'] = $collection_item;
$context['canonical_collection'] = $collection_item->collection->entity;
\Drupal::moduleHandler()->alter('collection_pathauto_alias', $alias, $context);
break;
}
}
}
