EntityUrlGenerator.php 7.17 KB
Newer Older
1 2
<?php

3
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
4

5 6 7
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
8
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager;
9 10 11
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Drupal\Core\Extension\ModuleHandler;
13

14
/**
gbyte.co's avatar
gbyte.co committed
15
 * Class EntityUrlGenerator
16 17 18
 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
 *
 * @UrlGenerator(
19
 *   id = "entity",
20 21
 *   title = @Translation("Entity URL generator"),
 *   description = @Translation("Generates URLs for entity bundles and bundle overrides."),
22
 * )
23
 */
24
class EntityUrlGenerator extends UrlGeneratorBase {
25

26 27 28 29 30
  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
   */
  protected $urlGeneratorManager;

31 32 33 34 35
  /**
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

36
  /**
37
   * EntityUrlGenerator constructor.
38
   * @param array $configuration
39 40
   * @param $plugin_id
   * @param $plugin_definition
41
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
42
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager $sitemap_generator_manager
43 44 45 46 47
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\simple_sitemap\Logger $logger
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
48
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
49 50 51 52 53 54
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    Simplesitemap $generator,
55
    SitemapGeneratorManager $sitemap_generator_manager,
56 57 58 59
    LanguageManagerInterface $language_manager,
    EntityTypeManagerInterface $entity_type_manager,
    Logger $logger,
    EntityHelper $entityHelper,
60 61
    UrlGeneratorManager $url_generator_manager,
    ModuleHandler $module_handler
62 63 64 65 66 67
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $generator,
68
      $sitemap_generator_manager,
69 70 71 72 73 74
      $language_manager,
      $entity_type_manager,
      $logger,
      $entityHelper
    );
    $this->urlGeneratorManager = $url_generator_manager;
75
    $this->moduleHandler = $module_handler;
76 77 78 79 80 81 82 83 84 85 86 87
  }

  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'),
88
      $container->get('plugin.manager.simple_sitemap.sitemap_generator'),
89 90 91 92
      $container->get('language_manager'),
      $container->get('entity_type.manager'),
      $container->get('simple_sitemap.logger'),
      $container->get('simple_sitemap.entity_helper'),
93 94
      $container->get('plugin.manager.simple_sitemap.url_generator'),
      $container->get('module_handler')
95 96 97
    );
  }

98
  /**
gbyte.co's avatar
gbyte.co committed
99
   * @inheritdoc
100
   */
101
  public function getDataSets() {
102 103
    $data_sets = [];
    $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
104

105 106 107
    foreach ($this->generator->getBundleSettings() as $entity_type_name => $bundles) {
      if (isset($sitemap_entity_types[$entity_type_name])) {

108 109
        // Skip this entity type if another plugin is written to override its generation.
        foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
110
          if (!empty($plugin['settings']['overrides_entity_type'])
111
            && $plugin['settings']['overrides_entity_type'] === $entity_type_name) {
112 113 114
            continue 2;
          }
        }
115

116
        $entity_type_name_alterable = $entity_type_name;
117
        foreach ($bundles as $bundle_name => $bundle_settings) {
118 119 120 121 122 123 124 125 126 127
          $bundle_name_alterable = $bundle_name;

          $this->moduleHandler->alter('simple_sitemap_bundle_settings', $bundle_settings, $entity_type_name_alterable, $bundle_name_alterable);

          // Skip this bundle if it is to be generated in a different sitemap variant.
          if (NULL !== $this->sitemapVariant && isset($bundle_settings['variant'])
            && $bundle_settings['variant'] !== $this->sitemapVariant) {
            continue;
          }

128
          if ($bundle_settings['index']) {
129
            $data_sets[] = [
130 131 132
              'bundle_settings' => $bundle_settings,
              'bundle_name' => $bundle_name,
              'entity_type_name' => $entity_type_name,
133
              'keys' => $sitemap_entity_types[$entity_type_name]->getKeys(),
134
            ];
135
          }
136
        }
137
      }
138
    }
139

140 141
    return $data_sets;
  }
142

gbyte.co's avatar
gbyte.co committed
143 144 145
  /**
   * @inheritdoc
   */
gbyte.co's avatar
gbyte.co committed
146
  protected function processDataSet($entity) {
147

gbyte.co's avatar
gbyte.co committed
148 149
    $entity_id = $entity->id();
    $entity_type_name = $entity->getEntityTypeId();
150

gbyte.co's avatar
gbyte.co committed
151
    $entity_settings = $this->generator->getEntityInstanceSettings($entity_type_name, $entity_id);
152

gbyte.co's avatar
gbyte.co committed
153 154 155 156
    if (empty($entity_settings['index'])) {
      return FALSE;
    }

157
    $url_object = $entity->toUrl();
gbyte.co's avatar
gbyte.co committed
158 159 160 161 162 163 164 165 166

    // Do not include external paths.
    if (!$url_object->isRouted()) {
      return FALSE;
    }

    $path = $url_object->getInternalPath();

    // Do not include paths that have been already indexed.
167
    if ($this->settings['remove_duplicates'] && $this->pathProcessed($path)) {
gbyte.co's avatar
gbyte.co committed
168 169 170 171 172 173 174
      return FALSE;
    }

    $url_object->setOption('absolute', TRUE);

    return [
      'url' => $url_object,
175
      'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
gbyte.co's avatar
gbyte.co committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
      '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,
        ],
      ]
    ];
191 192 193
  }

  /**
gbyte.co's avatar
gbyte.co committed
194
   * @inheritdoc
195
   */
196
  protected function getBatchIterationElements($entity_info) {
197
    $query = $this->entityTypeManager->getStorage($entity_info['entity_type_name'])->getQuery();
198 199 200 201 202 203 204 205 206 207 208

    if (!empty($entity_info['keys']['id'])) {
      $query->sort($entity_info['keys']['id'], 'ASC');
    }
    if (!empty($entity_info['keys']['bundle'])) {
      $query->condition($entity_info['keys']['bundle'], $entity_info['bundle_name']);
    }
    if (!empty($entity_info['keys']['status'])) {
      $query->condition($entity_info['keys']['status'], 1);
    }

209
    if ($this->batchOperationNeedsInitialization()) {
210
      $count_query = clone $query;
211
      $this->initializeBatchOperation($count_query->count()->execute());
212 213
    }

214
    if ($this->isDrupalBatch()) {
215
      $query->range($this->context['sandbox']['progress'], $this->settings['batch_process_limit']);
216 217 218 219 220 221 222
    }

    return $this->entityTypeManager
      ->getStorage($entity_info['entity_type_name'])
      ->loadMultiple($query->execute());
  }
}