UrlGeneratorBase.php 7.66 KB
Newer Older
1 2
<?php

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

5
use Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7 8
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Url;
9
use Drupal\simple_sitemap\EntityHelper;
gbyte.co's avatar
gbyte.co committed
10
use Drupal\simple_sitemap\Logger;
gbyte.co's avatar
gbyte.co committed
11
use Drupal\simple_sitemap\Simplesitemap;
gbyte.co's avatar
gbyte.co committed
12 13
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
14
use Drupal\Core\Language\Language;
15
use Drupal\Core\Session\AnonymousUserSession;
16 17

/**
gbyte.co's avatar
gbyte.co committed
18
 * Class UrlGeneratorBase
19
 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
20
 */
21
abstract class UrlGeneratorBase extends SimplesitemapPluginBase implements UrlGeneratorInterface {
22

gbyte.co's avatar
gbyte.co committed
23 24 25
  /**
   * @var \Drupal\simple_sitemap\Simplesitemap
   */
gbyte.co's avatar
gbyte.co committed
26
  protected $generator;
gbyte.co's avatar
gbyte.co committed
27 28 29 30

  /**
   * @var \Drupal\Core\Language\LanguageInterface[]
   */
31
  protected $languages;
gbyte.co's avatar
gbyte.co committed
32 33 34 35

  /**
   * @var string
   */
36
  protected $defaultLanguageId;
gbyte.co's avatar
gbyte.co committed
37 38 39 40

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
41
  protected $entityTypeManager;
gbyte.co's avatar
gbyte.co committed
42 43 44 45

  /**
   * @var \Drupal\simple_sitemap\Logger
   */
46
  protected $logger;
gbyte.co's avatar
gbyte.co committed
47 48 49 50

  /**
   * @var \Drupal\Core\Entity\EntityInterface|null
   */
51 52
  protected $anonUser;

gbyte.co's avatar
gbyte.co committed
53 54 55
  /**
   * @var array
   */
56
  protected $settings;
57

58
  /**
59 60 61 62
   * @var \Drupal\simple_sitemap\EntityHelper
   */
  protected $entityHelper;

63 64 65 66 67
  /**
   * @var string
   */
  protected $sitemapVariant;

68 69
  /**
   * UrlGeneratorBase constructor.
70
   * @param array $configuration
71 72
   * @param $plugin_id
   * @param $plugin_definition
gbyte.co's avatar
gbyte.co committed
73 74 75 76
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\simple_sitemap\Logger $logger
77
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
78
   */
79
  public function __construct(
80 81 82
    array $configuration,
    $plugin_id,
    $plugin_definition,
gbyte.co's avatar
gbyte.co committed
83
    Simplesitemap $generator,
gbyte.co's avatar
gbyte.co committed
84 85
    LanguageManagerInterface $language_manager,
    EntityTypeManagerInterface $entity_type_manager,
86 87
    Logger $logger,
    EntityHelper $entityHelper
88
  ) {
89
    parent::__construct($configuration, $plugin_id, $plugin_definition);
gbyte.co's avatar
gbyte.co committed
90
    $this->generator = $generator;
91
    $this->languages = $language_manager->getLanguages();
92
    $this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
93
    $this->entityTypeManager = $entity_type_manager;
94
    $this->anonUser = new AnonymousUserSession();
95 96
    $this->logger = $logger;
    $this->entityHelper = $entityHelper;
97 98
  }

gbyte.co's avatar
gbyte.co committed
99
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
100 101 102 103 104 105 106 107 108 109 110 111
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('simple_sitemap.generator'),
      $container->get('language_manager'),
      $container->get('entity_type.manager'),
      $container->get('simple_sitemap.logger'),
      $container->get('simple_sitemap.entity_helper')
    );
  }

112
  /**
113
   * @param array $settings
114
   * @return $this
115
   */
116 117
  public function setSettings(array $settings) {
    $this->settings = $settings;
118
    return $this;
119 120
  }

121 122 123 124 125 126 127 128 129
  /**
   * @param string $sitemap_variant
   * @return $this
   */
  public function setSitemapVariant($sitemap_variant) {
    $this->sitemapVariant = $sitemap_variant;
    return $this;
  }

gbyte.co's avatar
gbyte.co committed
130 131
  /**
   * @param array $path_data
132 133
   * @param \Drupal\Core\Url $url_object
   * @return array
gbyte.co's avatar
gbyte.co committed
134
   */
135 136
  protected function getUrlVariants(array $path_data, Url $url_object) {
    $url_variants = [];
137

138 139 140 141
    if (!$url_object->isRouted()) {
      // Not a routed URL, including only default variant.
      $alternate_urls = $this->getAlternateUrlsForDefaultLanguage($url_object);
    }
142
    elseif ($this->settings['skip_untranslated']
143
      && ($entity = $this->entityHelper->getEntityFromUrlObject($url_object)) instanceof ContentEntityBase) {
144 145

      /** @var ContentEntityBase $entity */
146 147 148
      $translation_languages = $entity->getTranslationLanguages();
      if (isset($translation_languages[Language::LANGCODE_NOT_SPECIFIED])
        || isset($translation_languages[Language::LANGCODE_NOT_APPLICABLE])) {
149

150 151 152 153 154 155
        // Content entity's language is unknown, including only default variant.
        $alternate_urls = $this->getAlternateUrlsForDefaultLanguage($url_object);
      }
      else {
        // Including only translated variants of content entity.
        $alternate_urls = $this->getAlternateUrlsForTranslatedLanguages($entity, $url_object);
156
      }
157 158
    }
    else {
159
      // Not a content entity or including all untranslated variants.
160 161 162 163
      $alternate_urls = $this->getAlternateUrlsForAllLanguages($url_object);
    }

    foreach ($alternate_urls as $langcode => $url) {
164 165 166 167 168
      $url_variants[] = $path_data + [
        'langcode' => $langcode,
          'url' => $url,
          'alternate_urls' => $alternate_urls
        ];
169
    }
170 171

    return $url_variants;
172 173
  }

174
  protected function getAlternateUrlsForDefaultLanguage(Url $url_object) {
175 176
    $alternate_urls = [];
    if ($url_object->access($this->anonUser)) {
gbyte.co's avatar
gbyte.co committed
177 178 179
      $alternate_urls[$this->defaultLanguageId] = $this->replaceBaseUrlWithCustom($url_object
        ->setOption('language', $this->languages[$this->defaultLanguageId])->toString()
      );
180 181 182 183
    }
    return $alternate_urls;
  }

184
  protected function getAlternateUrlsForTranslatedLanguages(ContentEntityBase $entity, Url $url_object) {
185
    $alternate_urls = [];
186

gbyte.co's avatar
gbyte.co committed
187 188
    /** @var Language $language */
    foreach ($entity->getTranslationLanguages() as $language) {
189
      if (!isset($this->settings['excluded_languages'][$language->getId()]) || $language->isDefault()) {
gbyte.co's avatar
gbyte.co committed
190 191 192 193
        if ($entity->getTranslation($language->getId())->access('view', $this->anonUser)) {
          $alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object
            ->setOption('language', $language)->toString()
          );
194
        }
195 196
      }
    }
197 198
    return $alternate_urls;
  }
199

200
  protected function getAlternateUrlsForAllLanguages(Url $url_object) {
201 202 203
    $alternate_urls = [];
    if ($url_object->access($this->anonUser)) {
      foreach ($this->languages as $language) {
204
        if (!isset($this->settings['excluded_languages'][$language->getId()]) || $language->isDefault()) {
gbyte.co's avatar
gbyte.co committed
205 206 207
          $alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object
            ->setOption('language', $language)->toString()
          );
208 209
        }
      }
210
    }
211
    return $alternate_urls;
212 213
  }

214 215 216 217 218
  /**
   * @param string $url
   * @return string
   */
  protected function replaceBaseUrlWithCustom($url) {
219 220
    return !empty($this->settings['base_url'])
      ? str_replace($GLOBALS['base_url'], $this->settings['base_url'], $url)
221 222
      : $url;
  }
223

gbyte.co's avatar
gbyte.co committed
224
  /**
225
   * @return mixed
gbyte.co's avatar
gbyte.co committed
226
   */
227
  abstract public function getDataSets();
gbyte.co's avatar
gbyte.co committed
228 229

  /**
gbyte.co's avatar
gbyte.co committed
230
   * @param $data_set
gbyte.co's avatar
gbyte.co committed
231 232
   * @return array
   */
gbyte.co's avatar
gbyte.co committed
233
  abstract protected function processDataSet($data_set);
gbyte.co's avatar
gbyte.co committed
234

gbyte.co's avatar
gbyte.co committed
235 236 237 238
  /**
   * @param $data_set
   * @return array
   */
239 240 241 242 243
  public function generate($data_set) {
    $path_data = $this->processDataSet($data_set);
    if (!$path_data) {
      return [];
    }
gbyte.co's avatar
gbyte.co committed
244
    if (isset($path_data['url']) && $path_data['url'] instanceof Url) {
245 246 247 248 249 250
      $url_object = $path_data['url'];
      unset($path_data['url']);
      return $this->getUrlVariants($path_data, $url_object);
    }
    else {
      return [$path_data];
gbyte.co's avatar
gbyte.co committed
251 252 253
    }
  }

gbyte.co's avatar
gbyte.co committed
254 255 256 257
  /**
   * @param $entity_type_name
   * @param $entity_id
   * @return array
258 259
   *
   * @todo
gbyte.co's avatar
gbyte.co committed
260 261 262
   */
  protected function getImages($entity_type_name, $entity_id) {
    $images = [];
gbyte.co's avatar
gbyte.co committed
263 264
    foreach ($this->entityHelper->getEntityImageUrls($entity_type_name, $entity_id) as $url) {
      $images[]['path'] = $this->replaceBaseUrlWithCustom($url);
gbyte.co's avatar
gbyte.co committed
265 266 267
    }
    return $images;
  }
268
}