CustomUrlGenerator.php 5.21 KB
Newer Older
Pawel G's avatar
Pawel G committed
1 2
<?php

3
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator;
Pawel G's avatar
Pawel G committed
4 5

use Drupal\Core\Url;
6
use Drupal\simple_sitemap\Annotation\UrlGenerator;
Pawel G's avatar
Pawel G committed
7 8 9 10 11 12 13
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
Pawel G's avatar
Pawel G committed
15 16

/**
Pawel G's avatar
Pawel G committed
17
 * Class CustomUrlGenerator
18 19 20
 * @package Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
 *
 * @UrlGenerator(
Pawel G's avatar
Pawel G committed
21
 *   id = "custom",
22 23 24
 *   title = @Translation("Custom URL generator"),
 *   description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
 *   enabled = TRUE,
25
 *   weight = 0,
26 27
 *   settings = {
 *   },
28
 * )
29
 *
Pawel G's avatar
Pawel G committed
30
 */
31
class CustomUrlGenerator extends UrlGeneratorBase {
Pawel G's avatar
Pawel G committed
32

Pawel G's avatar
Pawel G committed
33
  const PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE = 'The custom path @path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users. You can review custom paths <a href="@custom_paths_url">here</a>.';
Pawel G's avatar
Pawel G committed
34

Pawel G's avatar
Pawel G committed
35 36 37 38 39 40

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

Pawel G's avatar
Pawel G committed
41 42 43
  /**
   * @var bool
   */
44 45
  protected $includeImages;

Pawel G's avatar
Pawel G committed
46 47
  /**
   * CustomUrlGenerator constructor.
48 49 50
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
Pawel G's avatar
Pawel G committed
51 52 53 54 55 56 57 58 59
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
   * @param \Drupal\simple_sitemap\SitemapGenerator $sitemap_generator
   * @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\Core\Path\PathValidator $path_validator
   */
  public function __construct(
60 61 62
    array $configuration,
    $plugin_id,
    $plugin_definition,
Pawel G's avatar
Pawel G committed
63 64 65 66 67 68
    Simplesitemap $generator,
    SitemapGenerator $sitemap_generator,
    LanguageManagerInterface $language_manager,
    EntityTypeManagerInterface $entity_type_manager,
    Logger $logger,
    EntityHelper $entityHelper,
Pawel G's avatar
Pawel G committed
69
    PathValidator $path_validator) {
Pawel G's avatar
Pawel G committed
70
    parent::__construct(
71 72 73
      $configuration,
      $plugin_id,
      $plugin_definition,
Pawel G's avatar
Pawel G committed
74 75 76 77 78 79 80
      $generator,
      $sitemap_generator,
      $language_manager,
      $entity_type_manager,
      $logger,
      $entityHelper
    );
81
    $this->pathValidator = $path_validator;
82
  }
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  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'),
      $container->get('simple_sitemap.sitemap_generator'),
      $container->get('language_manager'),
      $container->get('entity_type.manager'),
      $container->get('simple_sitemap.logger'),
      $container->get('simple_sitemap.entity_helper'),
      $container->get('path.validator')
    );
101 102 103
  }

  /**
Pawel G's avatar
Pawel G committed
104
   * @inheritdoc
105
   */
106
  public function getDataSets() {
Pawel G's avatar
Pawel G committed
107 108 109
    $this->includeImages = $this->generator->getSetting('custom_links_include_images', FALSE);

    return array_values($this->generator->getCustomLinks());
Pawel G's avatar
Pawel G committed
110 111
  }

Pawel G's avatar
Pawel G committed
112
  /**
Pawel G's avatar
Pawel G committed
113
   * @inheritdoc
Pawel G's avatar
Pawel G committed
114
   */
Pawel G's avatar
Pawel G committed
115
  protected function processDataSet($data_set) {
Pawel G's avatar
Pawel G committed
116 117

      // todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush. Use getUrlIfValidWithoutAccessCheck()?
Pawel G's avatar
Pawel G committed
118
      if (!$this->pathValidator->isValid($data_set['path'])) {
Pawel G's avatar
Pawel G committed
119
//        if (!(bool) $this->pathValidator->getUrlIfValidWithoutAccessCheck($data['path'])) {
Pawel G's avatar
Pawel G committed
120
        $this->logger->m(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE,
Pawel G's avatar
Pawel G committed
121
          ['@path' => $data_set['path'], '@custom_paths_url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/custom'])
Pawel G's avatar
Pawel G committed
122 123
          ->display('warning', 'administer sitemap settings')
          ->log('warning');
Pawel G's avatar
Pawel G committed
124
        return FALSE;
Pawel G's avatar
Pawel G committed
125 126
      }

Pawel G's avatar
Pawel G committed
127 128 129 130
      $url_object = Url::fromUserInput($data_set['path'], ['absolute' => TRUE]);
      $path = $url_object->getInternalPath();

      if ($this->batchSettings['remove_duplicates'] && $this->pathProcessed($path)) {
Pawel G's avatar
Pawel G committed
131
        return FALSE;
Pawel G's avatar
Pawel G committed
132 133
      }

134
      $entity = $this->entityHelper->getEntityFromUrlObject($url_object);
Pawel G's avatar
Pawel G committed
135 136

      $path_data = [
Pawel G's avatar
Pawel G committed
137
        'url' => $url_object,
Pawel G's avatar
Pawel G committed
138 139
        'lastmod' => method_exists($entity, 'getChangedTime')
          ? date_iso8601($entity->getChangedTime()) : NULL,
Pawel G's avatar
Pawel G committed
140 141
        'priority' => isset($data_set['priority']) ? $data_set['priority'] : NULL,
        'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
142 143
        'images' => $this->includeImages && method_exists($entity, 'getEntityTypeId')
          ? $this->getImages($entity->getEntityTypeId(), $entity->id())
Pawel G's avatar
Pawel G committed
144 145
          : [],
        'meta' => [
Pawel G's avatar
Pawel G committed
146
          'path' => $path,
Pawel G's avatar
Pawel G committed
147
        ]
Pawel G's avatar
Pawel G committed
148
      ];
Pawel G's avatar
Pawel G committed
149

Pawel G's avatar
Pawel G committed
150 151 152 153 154 155 156 157 158
      // Additional info useful in hooks.
      if (NULL !== $entity) {
        $path_data['meta']['entity_info'] = [
          'entity_type' => $entity->getEntityTypeId(),
          'id' => $entity->id(),
        ];
      }

      return $path_data;
Pawel G's avatar
Pawel G committed
159 160
  }
}