You are here

public function SiteimproveUtils::getEntityUrls in Siteimprove 8

Return frontend urls for given entity.

Parameters

\Drupal\Core\Entity\EntityInterface|null $entity: Entity to get frontend urls for.

Return value

array Returns an array of frontend urls for entity.

Throws

\Drupal\Core\Entity\EntityMalformedException

1 call to SiteimproveUtils::getEntityUrls()
SiteimproveUtils::setSessionUrl in src/SiteimproveUtils.php
Save URL in session.

File

src/SiteimproveUtils.php, line 225

Class

SiteimproveUtils
Utility functions for Siteimprove.

Namespace

Drupal\siteimprove

Code

public function getEntityUrls(?EntityInterface $entity) {
  if (is_null($entity) || !$entity
    ->hasLinkTemplate('canonical')) {
    return [];
  }
  $domains = $this
    ->getEntityDomains($entity);

  /** @var \Drupal\Core\Entity\Entity $entity */
  $url_relative = $entity
    ->toUrl('canonical', [
    'absolute' => FALSE,
  ])
    ->toString(TRUE);
  $urls = [];

  // Create urls for active frontend urls for the entity.
  foreach ($domains as $domain) {
    $urls[] = $domain . $url_relative
      ->getGeneratedUrl();
  }
  $frontpage = $this->configFactory
    ->get('system.site')
    ->get('page.front');
  $current_route_name = $this->routeMatch
    ->getRouteName();
  $node_route = in_array($current_route_name, [
    'entity.node.edit_form',
    'entity.node.latest_version',
  ]);
  $taxonomy_route = in_array($current_route_name, [
    'entity.taxonomy_term.edit_form',
    'entity.taxonomy_term.latest_version',
  ]);

  // If entity is frontpage, add base url to domains.
  if ($node_route && '/node/' . $entity
    ->id() === $frontpage || $taxonomy_route && '/taxonomy/term/' . $entity
    ->id() === $frontpage || $this->pathMatcher
    ->isFrontPage()) {
    $front = Url::fromRoute('<front>')
      ->toString(TRUE);
    foreach ($domains as $domain) {
      $urls[] = $domain . $front
        ->getGeneratedUrl();
    }
  }
  return $urls;
}