You are here

public function EntityPathSubscriber::replacePathPlaceholder in Tome 8

Reacts to a path placeholder event.

Parameters

\Drupal\tome_static\Event\PathPlaceholderEvent $event: The path placeholder event.

File

modules/tome_static/src/EventSubscriber/EntityPathSubscriber.php, line 104

Class

EntityPathSubscriber
Adds entity placeholder paths to the list of paths to export.

Namespace

Drupal\tome_static\EventSubscriber

Code

public function replacePathPlaceholder(PathPlaceholderEvent $event) {
  $path = $event
    ->getPath();
  if (strpos($path, static::PLACEHOLDER_PREFIX . ':') === 0) {
    $event
      ->stopPropagation();
    list(, $entity_type_id, $langcode, $entity_id) = explode(':', $path);
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($entity_id);
    if (!$entity || !$entity instanceof ContentEntityInterface || !$entity
      ->hasTranslation($langcode)) {
      $event
        ->setInvalid();
      return;
    }
    $entity = $entity
      ->getTranslation($langcode);
    $url = $entity
      ->toUrl('canonical');
    if (!$entity
      ->access('view') || $entity
      ->isDefaultTranslation() && !$url
      ->access()) {
      $event
        ->setInvalid();
      return;
    }
    $event
      ->setPath(parse_url($url
      ->toString(), PHP_URL_PATH));
  }
}