You are here

public function XmlSitemapLinkStorage::create in XML sitemap 8

Same name and namespace in other branches
  1. 2.x src/XmlSitemapLinkStorage.php \Drupal\xmlsitemap\XmlSitemapLinkStorage::create()

Create a sitemap link from an entity.

The link will be saved as $entity->xmlsitemap.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity whose sitemap link will be created.

Overrides XmlSitemapLinkStorageInterface::create

File

src/XmlSitemapLinkStorage.php, line 70

Class

XmlSitemapLinkStorage
XmlSitemap link storage service class.

Namespace

Drupal\xmlsitemap

Code

public function create(EntityInterface $entity) {
  if (!isset($entity->xmlsitemap) || !is_array($entity->xmlsitemap)) {
    $entity->xmlsitemap = [];
    if ($entity
      ->id() && ($link = $this
      ->load($entity
      ->getEntityTypeId(), $entity
      ->id()))) {
      $entity->xmlsitemap = $link;
    }
  }
  $settings = xmlsitemap_link_bundle_load($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  $entity->xmlsitemap += [
    'type' => $entity
      ->getEntityTypeId(),
    'id' => (string) $entity
      ->id(),
    'subtype' => $entity
      ->bundle(),
    'status' => (int) $settings['status'],
    'status_default' => (int) $settings['status'],
    'status_override' => 0,
    'priority' => $settings['priority'],
    'priority_default' => $settings['priority'],
    'priority_override' => 0,
    'changefreq' => isset($settings['changefreq']) ? $settings['changefreq'] : 0,
  ];
  if ($entity instanceof EntityChangedInterface) {
    $entity->xmlsitemap['lastmod'] = $entity
      ->getChangedTime();
  }

  // The following values must always be checked because they are volatile.
  try {

    // @todo Could we move this logic to some kind of handler on the menu link entity class?
    if ($entity instanceof MenuLinkContentInterface) {
      $url = $entity
        ->getUrlObject();
      if ($url
        ->isRouted()) {
        if ($url
          ->getRouteName() === '<nolink>') {
          $loc = '';
        }
        else {
          $loc = $url
            ->getInternalPath();
        }
      }
      else {

        // Attempt to transform this to a relative URL.
        $loc = file_url_transform_relative($url
          ->toString());

        // If it could not be transformed into a relative path, disregard it
        // since we cannot store external URLs in the sitemap.
        if (UrlHelper::isExternal($loc)) {
          $loc = '';
        }
      }
      $access = $url
        ->access($this->anonymousUser);
    }
    else {
      $loc = $entity
        ->id() && $entity
        ->hasLinkTemplate('canonical') ? $entity
        ->toUrl()
        ->getInternalPath() : '';
      $access = $entity
        ->access('view', $this->anonymousUser);
    }
  } catch (RouteNotFoundException $e) {
    $loc = '';
  }
  $entity->xmlsitemap['loc'] = '/' . ltrim($loc, '/');
  $entity->xmlsitemap['access'] = $loc && $access;
  $language = $entity
    ->language();
  $entity->xmlsitemap['language'] = !empty($language) ? $language
    ->getId() : LanguageInterface::LANGCODE_NOT_SPECIFIED;
  return $entity->xmlsitemap;
}