CustomUrlGenerator.php 4.35 KB
Newer Older
1 2
<?php

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

use Drupal\Core\Url;
6
use Drupal\simple_sitemap\Annotation\UrlGenerator;
gbyte.co's avatar
gbyte.co committed
7 8 9 10 11 12
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 Drupal\Core\Path\PathValidator;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14 15

/**
gbyte.co's avatar
gbyte.co committed
16
 * Class CustomUrlGenerator
17 18 19
 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
 *
 * @UrlGenerator(
20
 *   id = "custom",
21
 *   label = @Translation("Custom URL generator"),
22
 *   description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
23
 * )
24
 *
25
 */
gbyte.co's avatar
gbyte.co committed
26
class CustomUrlGenerator extends EntityUrlGeneratorBase {
27

28
  const PATH_DOES_NOT_EXIST_MESSAGE = 'The custom path @path has been omitted from the XML sitemaps as it does not exist. You can review custom paths <a href="@custom_paths_url">here</a>.';
29

gbyte.co's avatar
gbyte.co committed
30 31 32 33 34 35

  /**
   * @var \Drupal\Core\Path\PathValidator
   */
  protected $pathValidator;

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

gbyte.co's avatar
gbyte.co committed
41 42
  /**
   * CustomUrlGenerator constructor.
43
   * @param array $configuration
44 45
   * @param $plugin_id
   * @param $plugin_definition
gbyte.co's avatar
gbyte.co committed
46
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
gbyte.co's avatar
gbyte.co committed
47
   * @param \Drupal\simple_sitemap\Logger $logger
gbyte.co's avatar
gbyte.co committed
48 49 50 51 52 53
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
   * @param \Drupal\Core\Path\PathValidator $path_validator
   */
  public function __construct(
54 55 56
    array $configuration,
    $plugin_id,
    $plugin_definition,
gbyte.co's avatar
gbyte.co committed
57
    Simplesitemap $generator,
gbyte.co's avatar
gbyte.co committed
58
    Logger $logger,
gbyte.co's avatar
gbyte.co committed
59 60 61
    LanguageManagerInterface $language_manager,
    EntityTypeManagerInterface $entity_type_manager,
    EntityHelper $entityHelper,
gbyte.co's avatar
gbyte.co committed
62
    PathValidator $path_validator) {
gbyte.co's avatar
gbyte.co committed
63
    parent::__construct(
64 65 66
      $configuration,
      $plugin_id,
      $plugin_definition,
gbyte.co's avatar
gbyte.co committed
67
      $generator,
gbyte.co's avatar
gbyte.co committed
68
      $logger,
gbyte.co's avatar
gbyte.co committed
69 70 71 72
      $language_manager,
      $entity_type_manager,
      $entityHelper
    );
73
    $this->pathValidator = $path_validator;
74
  }
75

76 77 78 79 80 81 82 83 84 85
  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
86
      $container->get('simple_sitemap.logger'),
87 88 89 90 91
      $container->get('language_manager'),
      $container->get('entity_type.manager'),
      $container->get('simple_sitemap.entity_helper'),
      $container->get('path.validator')
    );
92 93 94
  }

  /**
gbyte.co's avatar
gbyte.co committed
95
   * @inheritdoc
96
   */
97
  public function getDataSets() {
gbyte.co's avatar
gbyte.co committed
98
    $this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);
99
    return array_values($this->generator->setVariants($this->sitemapVariant)->getCustomLinks());
gbyte.co's avatar
gbyte.co committed
100 101
  }

102
  /**
gbyte.co's avatar
gbyte.co committed
103
   * @inheritdoc
104
   */
gbyte.co's avatar
gbyte.co committed
105
  protected function processDataSet($data_set) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($data_set['path'])) {
      $this->logger->m(self::PATH_DOES_NOT_EXIST_MESSAGE,
        ['@path' => $data_set['path'], '@custom_paths_url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/custom'])
        ->display('warning', 'administer sitemap settings')
        ->log('warning');
      return FALSE;
    }

    $url_object = Url::fromUserInput($data_set['path'], ['absolute' => TRUE]);
    $path = $url_object->getInternalPath();

    $entity = $this->entityHelper->getEntityFromUrlObject($url_object);

    $path_data = [
      'url' => $url_object,
      'lastmod' => method_exists($entity, 'getChangedTime')
        ? date_iso8601($entity->getChangedTime()) : NULL,
      'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL,
      'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
125 126
      'images' => $this->includeImages && !empty($entity)
        ? $this->getEntityImageData($entity)
127 128 129 130 131 132 133 134 135 136 137
        : [],
      'meta' => [
        'path' => $path,
      ]
    ];

    // Additional info useful in hooks.
    if (NULL !== $entity) {
      $path_data['meta']['entity_info'] = [
        'entity_type' => $entity->getEntityTypeId(),
        'id' => $entity->id(),
138
      ];
139
    }
gbyte.co's avatar
gbyte.co committed
140

141
    return $path_data;
142 143
  }
}