You are here

abstract class EntityUrlGeneratorBase in Simple XML sitemap 4.x

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

Class EntityUrlGeneratorBase

Hierarchy

Expanded class hierarchy of EntityUrlGeneratorBase

1 file declares its use of EntityUrlGeneratorBase
ViewsUrlGenerator.php in modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php

File

src/Plugin/simple_sitemap/UrlGenerator/EntityUrlGeneratorBase.php, line 21

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator
View source
abstract class EntityUrlGeneratorBase extends UrlGeneratorBase {

  /**
   * @var \Drupal\Core\Language\LanguageInterface[]
   */
  protected $languages;

  /**
   * @var string
   */
  protected $defaultLanguageId;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * @var \Drupal\simple_sitemap\Entity\EntityHelper
   */
  protected $entityHelper;

  /**
   * EntityUrlGeneratorBase 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
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Logger $logger, Settings $settings, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityHelper $entity_helper) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $settings);
    $this->languages = $language_manager
      ->getLanguages();
    $this->defaultLanguageId = $language_manager
      ->getDefaultLanguage()
      ->getId();
    $this->entityTypeManager = $entity_type_manager;
    $this->anonUser = new AnonymousUserSession();
    $this->entityHelper = $entity_helper;
  }
  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'));
  }

  /**
   * @param array $path_data
   * @param \Drupal\Core\Url $url_object
   * @return array
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function getUrlVariants(array $path_data, Url $url_object) : array {
    $url_variants = [];
    if (!$this->sitemapVariant
      ->isMultilingual() || !$url_object
      ->isRouted()) {

      // Not a routed URL or URL language negotiation disabled: Including only default variant.
      $alternate_urls = $this
        ->getAlternateUrlsForDefaultLanguage($url_object);
    }
    elseif ($this->settings
      ->get('skip_untranslated') && ($entity = $this->entityHelper
      ->getEntityFromUrlObject($url_object)) instanceof ContentEntityInterface) {

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

        // 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);
      }
    }
    else {

      // Not a content entity or including all untranslated variants.
      $alternate_urls = $this
        ->getAlternateUrlsForAllLanguages($url_object);
    }
    foreach ($alternate_urls as $langcode => $url) {
      $url_variants[] = $path_data + [
        'langcode' => $langcode,
        'url' => $url,
        'alternate_urls' => $alternate_urls,
      ];
    }
    return $url_variants;
  }

  /**
   * @param \Drupal\Core\Url $url_object
   * @return array
   */
  protected function getAlternateUrlsForDefaultLanguage(Url $url_object) : array {
    $alternate_urls = [];
    if ($url_object
      ->access($this->anonUser)) {
      $alternate_urls[$this->defaultLanguageId] = $this
        ->replaceBaseUrlWithCustom($url_object
        ->setAbsolute()
        ->setOption('language', $this->languages[$this->defaultLanguageId])
        ->toString());
    }
    return $alternate_urls;
  }

  /**
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   * @param \Drupal\Core\Url $url_object
   * @return array
   */
  protected function getAlternateUrlsForTranslatedLanguages(ContentEntityInterface $entity, Url $url_object) : array {
    $alternate_urls = [];

    /** @var Language $language */
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      if (!isset($this->settings
        ->get('excluded_languages')[$language
        ->getId()]) || $language
        ->isDefault()) {
        if ($entity
          ->getTranslation($language
          ->getId())
          ->access('view', $this->anonUser)) {
          $alternate_urls[$language
            ->getId()] = $this
            ->replaceBaseUrlWithCustom($url_object
            ->setAbsolute()
            ->setOption('language', $language)
            ->toString());
        }
      }
    }
    return $alternate_urls;
  }

  /**
   * @param \Drupal\Core\Url $url_object
   * @return array
   */
  protected function getAlternateUrlsForAllLanguages(Url $url_object) : array {
    $alternate_urls = [];
    if ($url_object
      ->access($this->anonUser)) {
      foreach ($this->languages as $language) {
        if (!isset($this->settings
          ->get('excluded_languages')[$language
          ->getId()]) || $language
          ->isDefault()) {
          $alternate_urls[$language
            ->getId()] = $this
            ->replaceBaseUrlWithCustom($url_object
            ->setAbsolute()
            ->setOption('language', $language)
            ->toString());
        }
      }
    }
    return $alternate_urls;
  }

  /**
   * @param mixed $data_set
   * @return array
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function generate($data_set) : array {
    $path_data = $this
      ->processDataSet($data_set);
    if (isset($path_data['url']) && $path_data['url'] instanceof Url) {
      $url_object = $path_data['url'];
      unset($path_data['url']);
      return $this
        ->getUrlVariants($path_data, $url_object);
    }
    return FALSE !== $path_data ? [
      $path_data,
    ] : [];

    // todo: May not want to return array here.
  }

  /**
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *
   * @return array
   */
  protected function getEntityImageData(ContentEntityInterface $entity) : array {
    $image_data = [];
    foreach ($entity
      ->getFieldDefinitions() as $field) {
      if ($field
        ->getType() === 'image') {
        foreach ($entity
          ->get($field
          ->getName())
          ->getValue() as $value) {
          if (!empty($file = File::load($value['target_id']))) {
            $image_data[] = [
              'path' => $this
                ->replaceBaseUrlWithCustom(file_create_url($file
                ->getFileUri())),
              'alt' => $value['alt'],
              'title' => $value['title'],
            ];
          }
        }
      }
    }
    return $image_data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::create public static function Creates an instance of the plugin. Overrides UrlGeneratorBase::create 4
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
EntityUrlGeneratorBase::__construct public function EntityUrlGeneratorBase constructor. Overrides UrlGeneratorBase::__construct 4
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::getDataSets abstract public function @todo Throw and catch SkipElementException here and children. Overrides UrlGeneratorInterface::getDataSets 4
UrlGeneratorBase::processDataSet abstract protected function 4
UrlGeneratorBase::replaceBaseUrlWithCustom protected function
UrlGeneratorBase::setSitemapVariant public function Overrides UrlGeneratorInterface::setSitemapVariant