You are here

class EntityUrlGenerator in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/simple_sitemap/UrlGenerator/EntityUrlGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGenerator
  2. 4.x src/Plugin/simple_sitemap/UrlGenerator/EntityUrlGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGenerator

Class EntityUrlGenerator @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator

Plugin annotation


@UrlGenerator(
  id = "entity",
  label = @Translation("Entity URL generator"),
  description = @Translation("Generates URLs for entity bundles and bundle overrides."),
)

Hierarchy

Expanded class hierarchy of EntityUrlGenerator

File

src/Plugin/simple_sitemap/UrlGenerator/EntityUrlGenerator.php, line 24

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
View source
class EntityUrlGenerator extends EntityUrlGeneratorBase {

  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
   */
  protected $urlGeneratorManager;

  /**
   * @var integer
   */
  protected $entitiesPerDataset;

  /**
   * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface
   */
  protected $entityMemoryCache;

  /**
   * EntityUrlGenerator constructor.
   * @param array $configuration
   * @param $plugin_id
   * @param $plugin_definition
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
   * @param \Drupal\simple_sitemap\Logger $logger
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
   * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Simplesitemap $generator, Logger $logger, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityHelper $entityHelper, UrlGeneratorManager $url_generator_manager, MemoryCacheInterface $memory_cache) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $generator, $logger, $language_manager, $entity_type_manager, $entityHelper);
    $this->urlGeneratorManager = $url_generator_manager;
    $this->entityMemoryCache = $memory_cache;
    $this->entitiesPerDataset = $this->generator
      ->getSetting('entities_per_queue_item', 50);
  }
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('simple_sitemap.generator'), $container
      ->get('simple_sitemap.logger'), $container
      ->get('language_manager'), $container
      ->get('entity_type.manager'), $container
      ->get('simple_sitemap.entity_helper'), $container
      ->get('plugin.manager.simple_sitemap.url_generator'), $container
      ->get('entity.memory_cache'));
  }

  /**
   * @inheritdoc
   */
  public function getDataSets() {
    $data_sets = [];
    $sitemap_entity_types = $this->entityHelper
      ->getSupportedEntityTypes();
    foreach ($this->generator
      ->setVariants($this->sitemapVariant)
      ->getBundleSettings() as $entity_type_name => $bundles) {
      if (isset($sitemap_entity_types[$entity_type_name])) {

        // Skip this entity type if another plugin is written to override its generation.
        foreach ($this->urlGeneratorManager
          ->getDefinitions() as $plugin) {
          if (isset($plugin['settings']['overrides_entity_type']) && $plugin['settings']['overrides_entity_type'] === $entity_type_name) {
            continue 2;
          }
        }
        $entityTypeStorage = $this->entityTypeManager
          ->getStorage($entity_type_name);
        $keys = $sitemap_entity_types[$entity_type_name]
          ->getKeys();
        foreach ($bundles as $bundle_name => $bundle_settings) {
          if (!empty($bundle_settings['index'])) {
            $query = $entityTypeStorage
              ->getQuery();
            if (empty($keys['id'])) {
              $query
                ->sort($keys['id'], 'ASC');
            }
            if (!empty($keys['bundle'])) {
              $query
                ->condition($keys['bundle'], $bundle_name);
            }
            if (!empty($keys['status'])) {
              $query
                ->condition($keys['status'], 1);
            }

            // Shift access check to EntityUrlGeneratorBase for language
            // specific access.
            // See https://www.drupal.org/project/simple_sitemap/issues/3102450.
            $query
              ->accessCheck(FALSE);
            $data_set = [
              'entity_type' => $entity_type_name,
              'id' => [],
            ];
            foreach ($query
              ->execute() as $entity_id) {
              $data_set['id'][] = $entity_id;
              if (count($data_set['id']) >= $this->entitiesPerDataset) {
                $data_sets[] = $data_set;
                $data_set['id'] = [];
              }
            }

            // Add the last data set if there are some IDs gathered.
            if (!empty($data_set['id'])) {
              $data_sets[] = $data_set;
            }
          }
        }
      }
    }
    return $data_sets;
  }

  /**
   * @inheritdoc
   */
  protected function processDataSet($data_set) {
    $entities = $this->entityTypeManager
      ->getStorage($data_set['entity_type'])
      ->loadMultiple((array) $data_set['id']);
    if (empty($entities)) {
      return FALSE;
    }
    $paths = [];
    foreach ($entities as $entity) {
      $entity_settings = $this->generator
        ->setVariants($this->sitemapVariant)
        ->getEntityInstanceSettings($entity
        ->getEntityTypeId(), $entity
        ->id());
      if (empty($entity_settings['index'])) {
        continue;
      }
      $url_object = $entity
        ->toUrl()
        ->setAbsolute();

      // Do not include external paths.
      if (!$url_object
        ->isRouted()) {
        continue;
      }
      $paths[] = [
        'url' => $url_object,
        'lastmod' => method_exists($entity, 'getChangedTime') ? date('c', $entity
          ->getChangedTime()) : NULL,
        'priority' => isset($entity_settings['priority']) ? $entity_settings['priority'] : NULL,
        'changefreq' => !empty($entity_settings['changefreq']) ? $entity_settings['changefreq'] : NULL,
        'images' => !empty($entity_settings['include_images']) ? $this
          ->getEntityImageData($entity) : [],
        // Additional info useful in hooks.
        'meta' => [
          'path' => $url_object
            ->getInternalPath(),
          'entity_info' => [
            'entity_type' => $entity
              ->getEntityTypeId(),
            'id' => $entity
              ->id(),
          ],
        ],
      ];
    }
    return $paths;
  }

  /**
   * @inheritdoc
   */
  public function generate($data_set) {
    $path_data_sets = $this
      ->processDataSet($data_set);
    $url_variant_sets = [];
    foreach ($path_data_sets as $key => $path_data) {
      if (isset($path_data['url']) && $path_data['url'] instanceof Url) {
        $url_object = $path_data['url'];
        unset($path_data['url']);
        $url_variant_sets[] = $this
          ->getUrlVariants($path_data, $url_object);
      }
    }

    // Make sure to clear entity memory cache so it does not build up resulting
    // in a constant increase of memory.
    // See https://www.drupal.org/project/simple_sitemap/issues/3170261 and
    // https://www.drupal.org/project/simple_sitemap/issues/3202233
    if ($this->entityTypeManager
      ->getDefinition($data_set['entity_type'])
      ->isStaticallyCacheable()) {
      $this->entityMemoryCache
        ->deleteAll();
    }
    return array_merge([], ...$url_variant_sets);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityUrlGenerator::$entitiesPerDataset protected property
EntityUrlGenerator::$entityMemoryCache protected property
EntityUrlGenerator::$urlGeneratorManager protected property
EntityUrlGenerator::create public static function Creates an instance of the plugin. Overrides EntityUrlGeneratorBase::create
EntityUrlGenerator::generate public function @inheritdoc Overrides EntityUrlGeneratorBase::generate
EntityUrlGenerator::getDataSets public function @inheritdoc Overrides UrlGeneratorBase::getDataSets
EntityUrlGenerator::processDataSet protected function @inheritdoc Overrides UrlGeneratorBase::processDataSet
EntityUrlGenerator::__construct public function EntityUrlGenerator constructor. Overrides EntityUrlGeneratorBase::__construct
EntityUrlGeneratorBase::$anonUser protected property
EntityUrlGeneratorBase::$defaultLanguageId protected property
EntityUrlGeneratorBase::$entityHelper protected property
EntityUrlGeneratorBase::$entityTypeManager protected property
EntityUrlGeneratorBase::$isMultilingualSitemap protected property
EntityUrlGeneratorBase::$languages protected property
EntityUrlGeneratorBase::getAlternateUrlsForAllLanguages protected function
EntityUrlGeneratorBase::getAlternateUrlsForDefaultLanguage protected function
EntityUrlGeneratorBase::getAlternateUrlsForTranslatedLanguages protected function
EntityUrlGeneratorBase::getEntityImageData protected function
EntityUrlGeneratorBase::getUrlVariants protected function
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorBase::$generator protected property
UrlGeneratorBase::$logger protected property
UrlGeneratorBase::$settings protected property
UrlGeneratorBase::$sitemapVariant protected property
UrlGeneratorBase::replaceBaseUrlWithCustom protected function
UrlGeneratorBase::setSettings public function Overrides UrlGeneratorInterface::setSettings
UrlGeneratorBase::setSitemapVariant public function Overrides UrlGeneratorInterface::setSitemapVariant