You are here

class CustomUrlGenerator in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/simple_sitemap/UrlGenerator/CustomUrlGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\CustomUrlGenerator
  2. 8.2 src/Plugin/simple_sitemap/UrlGenerator/CustomUrlGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\CustomUrlGenerator

Class CustomUrlGenerator

Plugin annotation


@UrlGenerator(
  id = "custom",
  label = @Translation("Custom URL generator"),
  description = @Translation("Generates URLs set in admin/config/search/simplesitemap/custom."),
)

Hierarchy

Expanded class hierarchy of CustomUrlGenerator

File

src/Plugin/simple_sitemap/UrlGenerator/CustomUrlGenerator.php, line 28

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
View source
class CustomUrlGenerator extends EntityUrlGeneratorBase {
  protected 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>.';

  /**
   * @var \Drupal\simple_sitemap\Manager\CustomLinkManager
   */
  protected $customLinks;

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

  /**
   * @var bool
   */
  protected $includeImages;

  /**
   * CustomUrlGenerator constructor.
   *
   * @param array $configuration
   * @param $plugin_id
   * @param $plugin_definition
   * @param \Drupal\simple_sitemap\Logger $logger
   * @param \Drupal\simple_sitemap\Settings $settings
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\simple_sitemap\Entity\EntityHelper $entity_helper
   * @param \Drupal\simple_sitemap\Manager\CustomLinkManager $custom_links
   * @param \Drupal\Core\Path\PathValidator $path_validator
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Logger $logger, Settings $settings, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityHelper $entity_helper, CustomLinkManager $custom_links, PathValidator $path_validator) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $settings, $language_manager, $entity_type_manager, $entity_helper);
    $this->customLinks = $custom_links;
    $this->pathValidator = $path_validator;
  }
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : SimpleSitemapPluginBase {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('simple_sitemap.logger'), $container
      ->get('simple_sitemap.settings'), $container
      ->get('language_manager'), $container
      ->get('entity_type.manager'), $container
      ->get('simple_sitemap.entity_helper'), $container
      ->get('simple_sitemap.custom_link_manager'), $container
      ->get('path.validator'));
  }

  /**
   * @inheritdoc
   */
  public function getDataSets() : array {
    $this->includeImages = $this->settings
      ->get('custom_links_include_images', FALSE);
    return array_values($this->customLinks
      ->setVariants($this->sitemapVariant
      ->id())
      ->get());
  }

  /**
   * @inheritdoc
   */
  protected function processDataSet($data_set) : array {
    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');
      throw new SkipElementException();
    }
    $url_object = Url::fromUserInput($data_set['path'])
      ->setAbsolute();
    $path = $url_object
      ->getInternalPath();
    $entity = $this->entityHelper
      ->getEntityFromUrlObject($url_object);
    $path_data = [
      'url' => $url_object,
      'lastmod' => !empty($entity) && method_exists($entity, 'getChangedTime') ? date('c', $entity
        ->getChangedTime()) : NULL,
      'priority' => $data_set['priority'] ?? NULL,
      'changefreq' => !empty($data_set['changefreq']) ? $data_set['changefreq'] : NULL,
      'images' => $this->includeImages && !empty($entity) ? $this
        ->getEntityImageData($entity) : [],
      'meta' => [
        'path' => $path,
      ],
    ];

    // Additional info useful in hooks.
    if (!empty($entity)) {
      $path_data['meta']['entity_info'] = [
        'entity_type' => $entity
          ->getEntityTypeId(),
        'id' => $entity
          ->id(),
      ];
    }
    return $path_data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomUrlGenerator::$customLinks protected property
CustomUrlGenerator::$includeImages protected property
CustomUrlGenerator::$pathValidator protected property
CustomUrlGenerator::create public static function Creates an instance of the plugin. Overrides EntityUrlGeneratorBase::create
CustomUrlGenerator::getDataSets public function @inheritdoc Overrides UrlGeneratorBase::getDataSets
CustomUrlGenerator::PATH_DOES_NOT_EXIST_MESSAGE protected constant
CustomUrlGenerator::processDataSet protected function @inheritdoc Overrides UrlGeneratorBase::processDataSet
CustomUrlGenerator::__construct public function CustomUrlGenerator constructor. Overrides EntityUrlGeneratorBase::__construct
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityUrlGeneratorBase::$anonUser protected property
EntityUrlGeneratorBase::$defaultLanguageId protected property
EntityUrlGeneratorBase::$entityHelper protected property
EntityUrlGeneratorBase::$entityTypeManager protected property
EntityUrlGeneratorBase::$languages protected property
EntityUrlGeneratorBase::generate public function Overrides UrlGeneratorBase::generate 1
EntityUrlGeneratorBase::getAlternateUrlsForAllLanguages protected function
EntityUrlGeneratorBase::getAlternateUrlsForDefaultLanguage protected function
EntityUrlGeneratorBase::getAlternateUrlsForTranslatedLanguages protected function
EntityUrlGeneratorBase::getEntityImageData protected function
EntityUrlGeneratorBase::getUrlVariants protected function
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SimpleSitemapPluginBase::description public function Overrides SimpleSitemapPluginInterface::description
SimpleSitemapPluginBase::label public function Overrides SimpleSitemapPluginInterface::label
SimpleSitemapPluginBase::settings public function Overrides SimpleSitemapPluginInterface::settings
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorBase::$logger protected property
UrlGeneratorBase::$settings protected property
UrlGeneratorBase::$sitemapVariant protected property
UrlGeneratorBase::replaceBaseUrlWithCustom protected function
UrlGeneratorBase::setSitemapVariant public function Overrides UrlGeneratorInterface::setSitemapVariant