You are here

function xmlsitemap_xmlsitemap_process_entity_link in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap.module \xmlsitemap_xmlsitemap_process_entity_link()

Process sitemap link for an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

3 calls to xmlsitemap_xmlsitemap_process_entity_link()
xmlsitemap_entity_insert in ./xmlsitemap.module
Implements hook_entity_insert().
xmlsitemap_entity_update in ./xmlsitemap.module
Implements hook_entity_update().
xmlsitemap_xmlsitemap_process_entity_links in ./xmlsitemap.module
Process sitemap links.

File

./xmlsitemap.module, line 1635
xmlsitemap XML sitemap

Code

function xmlsitemap_xmlsitemap_process_entity_link(EntityInterface $entity) {

  /** @var \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage */
  $link_storage = \Drupal::service('xmlsitemap.link_storage');
  if ($entity instanceof ContentEntityInterface) {

    // Generate an entry for each translation.
    $translation_languages = $entity
      ->getTranslationLanguages();
    foreach ($translation_languages as $langcode => $language) {
      $translation = $entity
        ->getTranslation($langcode);
      $link = $link_storage
        ->create($translation);
      $context = [
        $link['type'] => $entity,
        'entity' => $entity,
      ];
      $link_storage
        ->save($link, $context);
    }

    // Check if there are any removed language translations.
    if (isset($entity->original)) {
      $original_translation_languages = $entity->original
        ->getTranslationLanguages();
      $removed_translation_languages = array_diff(array_keys($original_translation_languages), array_keys($translation_languages));
      if (!empty($removed_translation_languages)) {
        $link_storage
          ->deleteMultiple([
          'type' => $entity
            ->getEntityTypeId(),
          'id' => $entity
            ->id(),
          'language' => $removed_translation_languages,
        ]);
      }
    }
  }
  else {
    $link = $link_storage
      ->create($entity);
    $context = [
      $link['type'] => $entity,
      'entity' => $entity,
    ];
    $link_storage
      ->save($link, $context);
  }
}