123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
- use Drupal\simple_sitemap\EntityHelper;
- use Drupal\simple_sitemap\Logger;
- use Drupal\simple_sitemap\Simplesitemap;
- use Drupal\Core\Language\LanguageManagerInterface;
- use Drupal\Core\Entity\EntityTypeManagerInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Class EntityUrlGenerator
- * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
- *
- * @UrlGenerator(
- * id = "entity",
- * label = @Translation("Entity URL generator"),
- * description = @Translation("Generates URLs for entity bundles and bundle overrides."),
- * )
- */
- class EntityUrlGenerator extends EntityUrlGeneratorBase {
- /**
- * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
- */
- protected $urlGeneratorManager;
- /**
- * 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
- */
- 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
- ) {
- parent::__construct(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $generator,
- $logger,
- $language_manager,
- $entity_type_manager,
- $entityHelper
- );
- $this->urlGeneratorManager = $url_generator_manager;
- }
- 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')
- );
- }
- /**
- * @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);
- }
- foreach ($query->execute() as $entity_id) {
- $data_sets[] = [
- 'entity_type' => $entity_type_name,
- 'id' => $entity_id,
- ];
- }
- }
- }
- }
- }
- return $data_sets;
- }
- /**
- * @inheritdoc
- */
- protected function processDataSet($data_set) {
- if (empty($entity = $this->entityTypeManager->getStorage($data_set['entity_type'])->load($data_set['id']))) {
- return FALSE;
- }
- $entity_id = $entity->id();
- $entity_type_name = $entity->getEntityTypeId();
- $entity_settings = $this->generator
- ->setVariants($this->sitemapVariant)
- ->getEntityInstanceSettings($entity_type_name, $entity_id);
- if (empty($entity_settings['index'])) {
- return FALSE;
- }
- $url_object = $entity->toUrl();
- // Do not include external paths.
- if (!$url_object->isRouted()) {
- return FALSE;
- }
- $path = $url_object->getInternalPath();
- $url_object->setOption('absolute', TRUE);
- return [
- 'url' => $url_object,
- 'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($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->getImages($entity_type_name, $entity_id)
- : [],
- // Additional info useful in hooks.
- 'meta' => [
- 'path' => $path,
- 'entity_info' => [
- 'entity_type' => $entity_type_name,
- 'id' => $entity_id,
- ],
- ]
- ];
- }
- }
|