EntityUrlGeneratorBase.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Drupal\Core\Entity\ContentEntityBase;
  5. use Drupal\Core\Url;
  6. use Drupal\simple_sitemap\EntityHelper;
  7. use Drupal\simple_sitemap\Logger;
  8. use Drupal\simple_sitemap\Simplesitemap;
  9. use Drupal\Core\Language\LanguageManagerInterface;
  10. use Drupal\Core\Entity\EntityTypeManagerInterface;
  11. use Drupal\Core\Language\Language;
  12. use Drupal\Core\Session\AnonymousUserSession;
  13. /**
  14. * Class EntityUrlGeneratorBase
  15. * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
  16. */
  17. abstract class EntityUrlGeneratorBase extends UrlGeneratorBase {
  18. /**
  19. * @var \Drupal\Core\Language\LanguageInterface[]
  20. */
  21. protected $languages;
  22. /**
  23. * @var string
  24. */
  25. protected $defaultLanguageId;
  26. /**
  27. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  28. */
  29. protected $entityTypeManager;
  30. /**
  31. * @var \Drupal\Core\Entity\EntityInterface|null
  32. */
  33. protected $anonUser;
  34. /**
  35. * @var \Drupal\simple_sitemap\EntityHelper
  36. */
  37. protected $entityHelper;
  38. /**
  39. * UrlGeneratorBase constructor.
  40. * @param array $configuration
  41. * @param $plugin_id
  42. * @param $plugin_definition
  43. * @param \Drupal\simple_sitemap\Simplesitemap $generator
  44. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  45. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  46. * @param \Drupal\simple_sitemap\Logger $logger
  47. * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
  48. */
  49. public function __construct(
  50. array $configuration,
  51. $plugin_id,
  52. $plugin_definition,
  53. Simplesitemap $generator,
  54. Logger $logger,
  55. LanguageManagerInterface $language_manager,
  56. EntityTypeManagerInterface $entity_type_manager,
  57. EntityHelper $entityHelper
  58. ) {
  59. parent::__construct($configuration, $plugin_id, $plugin_definition, $generator, $logger);
  60. $this->languages = $language_manager->getLanguages();
  61. $this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
  62. $this->entityTypeManager = $entity_type_manager;
  63. $this->anonUser = new AnonymousUserSession();
  64. $this->entityHelper = $entityHelper;
  65. }
  66. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  67. return new static(
  68. $configuration,
  69. $plugin_id,
  70. $plugin_definition,
  71. $container->get('simple_sitemap.generator'),
  72. $container->get('simple_sitemap.logger'),
  73. $container->get('language_manager'),
  74. $container->get('entity_type.manager'),
  75. $container->get('simple_sitemap.entity_helper')
  76. );
  77. }
  78. /**
  79. * @param array $path_data
  80. * @param \Drupal\Core\Url $url_object
  81. * @return array
  82. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  83. * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  84. */
  85. protected function getUrlVariants(array $path_data, Url $url_object) {
  86. $url_variants = [];
  87. if (!$url_object->isRouted()) {
  88. // Not a routed URL, including only default variant.
  89. $alternate_urls = $this->getAlternateUrlsForDefaultLanguage($url_object);
  90. }
  91. elseif ($this->settings['skip_untranslated']
  92. && ($entity = $this->entityHelper->getEntityFromUrlObject($url_object)) instanceof ContentEntityBase) {
  93. /** @var ContentEntityBase $entity */
  94. $translation_languages = $entity->getTranslationLanguages();
  95. if (isset($translation_languages[Language::LANGCODE_NOT_SPECIFIED])
  96. || isset($translation_languages[Language::LANGCODE_NOT_APPLICABLE])) {
  97. // Content entity's language is unknown, including only default variant.
  98. $alternate_urls = $this->getAlternateUrlsForDefaultLanguage($url_object);
  99. }
  100. else {
  101. // Including only translated variants of content entity.
  102. $alternate_urls = $this->getAlternateUrlsForTranslatedLanguages($entity, $url_object);
  103. }
  104. }
  105. else {
  106. // Not a content entity or including all untranslated variants.
  107. $alternate_urls = $this->getAlternateUrlsForAllLanguages($url_object);
  108. }
  109. foreach ($alternate_urls as $langcode => $url) {
  110. $url_variants[] = $path_data + [
  111. 'langcode' => $langcode,
  112. 'url' => $url,
  113. 'alternate_urls' => $alternate_urls
  114. ];
  115. }
  116. return $url_variants;
  117. }
  118. /**
  119. * @param \Drupal\Core\Url $url_object
  120. * @return array
  121. */
  122. protected function getAlternateUrlsForDefaultLanguage(Url $url_object) {
  123. $alternate_urls = [];
  124. if ($url_object->access($this->anonUser)) {
  125. $alternate_urls[$this->defaultLanguageId] = $this->replaceBaseUrlWithCustom($url_object
  126. ->setOption('language', $this->languages[$this->defaultLanguageId])->toString()
  127. );
  128. }
  129. return $alternate_urls;
  130. }
  131. /**
  132. * @param \Drupal\Core\Entity\ContentEntityBase $entity
  133. * @param \Drupal\Core\Url $url_object
  134. * @return array
  135. */
  136. protected function getAlternateUrlsForTranslatedLanguages(ContentEntityBase $entity, Url $url_object) {
  137. $alternate_urls = [];
  138. /** @var Language $language */
  139. foreach ($entity->getTranslationLanguages() as $language) {
  140. if (!isset($this->settings['excluded_languages'][$language->getId()]) || $language->isDefault()) {
  141. if ($entity->getTranslation($language->getId())->access('view', $this->anonUser)) {
  142. $alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object
  143. ->setOption('language', $language)->toString()
  144. );
  145. }
  146. }
  147. }
  148. return $alternate_urls;
  149. }
  150. /**
  151. * @param \Drupal\Core\Url $url_object
  152. * @return array
  153. */
  154. protected function getAlternateUrlsForAllLanguages(Url $url_object) {
  155. $alternate_urls = [];
  156. if ($url_object->access($this->anonUser)) {
  157. foreach ($this->languages as $language) {
  158. if (!isset($this->settings['excluded_languages'][$language->getId()]) || $language->isDefault()) {
  159. $alternate_urls[$language->getId()] = $this->replaceBaseUrlWithCustom($url_object
  160. ->setOption('language', $language)->toString()
  161. );
  162. }
  163. }
  164. }
  165. return $alternate_urls;
  166. }
  167. /**
  168. * @param mixed $data_set
  169. * @return array
  170. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
  171. * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
  172. */
  173. public function generate($data_set) {
  174. $path_data = $this->processDataSet($data_set);
  175. if (isset($path_data['url']) && $path_data['url'] instanceof Url) {
  176. $url_object = $path_data['url'];
  177. unset($path_data['url']);
  178. return $this->getUrlVariants($path_data, $url_object);
  179. }
  180. else {
  181. return FALSE !== $path_data ? [$path_data] : [];
  182. }
  183. }
  184. /**
  185. * @param string $entity_type_name
  186. * @param string $entity_id
  187. * @return array
  188. */
  189. protected function getImages($entity_type_name, $entity_id) {
  190. $images = [];
  191. foreach ($this->entityHelper->getEntityImageUrls($entity_type_name, $entity_id) as $url) {
  192. $images[]['path'] = $this->replaceBaseUrlWithCustom($url);
  193. }
  194. return $images;
  195. }
  196. }