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

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

5 6 7 8 9 10 11
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;

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

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

  /**
30
   * EntityUrlGenerator constructor.
31
   * @param array $configuration
32 33
   * @param $plugin_id
   * @param $plugin_definition
34
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
gbyte.co's avatar
gbyte.co committed
35
   * @param \Drupal\simple_sitemap\Logger $logger
36 37 38 39 40 41 42 43 44 45
   * @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,
gbyte.co's avatar
gbyte.co committed
46
    Logger $logger,
47 48 49
    LanguageManagerInterface $language_manager,
    EntityTypeManagerInterface $entity_type_manager,
    EntityHelper $entityHelper,
50
    UrlGeneratorManager $url_generator_manager
51 52 53 54 55 56
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $generator,
gbyte.co's avatar
gbyte.co committed
57
      $logger,
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      $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'),
gbyte.co's avatar
gbyte.co committed
75
      $container->get('simple_sitemap.logger'),
76 77 78
      $container->get('language_manager'),
      $container->get('entity_type.manager'),
      $container->get('simple_sitemap.entity_helper'),
79
      $container->get('plugin.manager.simple_sitemap.url_generator')
80 81 82
    );
  }

83
  /**
gbyte.co's avatar
gbyte.co committed
84
   * @inheritdoc
85
   */
86
  public function getDataSets() {
87 88
    $data_sets = [];
    $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
89

90
    foreach ($this->generator->setVariants($this->sitemapVariant)->getBundleSettings() as $entity_type_name => $bundles) {
91 92
      if (isset($sitemap_entity_types[$entity_type_name])) {

93 94
        // Skip this entity type if another plugin is written to override its generation.
        foreach ($this->urlGeneratorManager->getDefinitions() as $plugin) {
gbyte.co's avatar
gbyte.co committed
95
          if (isset($plugin['settings']['overrides_entity_type'])
96
            && $plugin['settings']['overrides_entity_type'] === $entity_type_name) {
97 98 99
            continue 2;
          }
        }
100

101 102
        $entityTypeStorage = $this->entityTypeManager->getStorage($entity_type_name);
        $keys = $sitemap_entity_types[$entity_type_name]->getKeys();
103

104
        foreach ($bundles as $bundle_name => $bundle_settings) {
105
          if (!empty($bundle_settings['index'])) {
106
            $query = $entityTypeStorage->getQuery();
107 108

            if (empty($keys['id'])) {
109
              $query->sort($keys['id'], 'ASC');
110 111
            }
            if (!empty($keys['bundle'])) {
112
              $query->condition($keys['bundle'], $bundle_name);
113 114
            }
            if (!empty($keys['status'])) {
115
              $query->condition($keys['status'], 1);
116 117
            }

118
            foreach ($query->execute() as $entity_id) {
119 120 121 122 123
              $data_sets[] = [
                'entity_type' => $entity_type_name,
                'id' => $entity_id,
              ];
            }
124
          }
125
        }
126
      }
127
    }
128

129 130
    return $data_sets;
  }
131

gbyte.co's avatar
gbyte.co committed
132 133 134
  /**
   * @inheritdoc
   */
135
  protected function processDataSet($data_set) {
136 137 138
    if (empty($entity = $this->entityTypeManager->getStorage($data_set['entity_type'])->load($data_set['id']))) {
      return FALSE;
    }
139

gbyte.co's avatar
gbyte.co committed
140 141
    $entity_id = $entity->id();
    $entity_type_name = $entity->getEntityTypeId();
142

143 144 145
    $entity_settings = $this->generator
      ->setVariants($this->sitemapVariant)
      ->getEntityInstanceSettings($entity_type_name, $entity_id);
146

gbyte.co's avatar
gbyte.co committed
147 148 149 150
    if (empty($entity_settings['index'])) {
      return FALSE;
    }

151
    $url_object = $entity->toUrl();
gbyte.co's avatar
gbyte.co committed
152 153 154 155 156 157 158 159 160 161 162 163

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

    $path = $url_object->getInternalPath();

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

    return [
      'url' => $url_object,
164
      'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
gbyte.co's avatar
gbyte.co committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
      '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,
        ],
      ]
    ];
180 181
  }
}