You are here

trait VariantSetterTrait in Simple XML sitemap 4.x

Class Simplesitemap

Hierarchy

File

src/Manager/VariantSetterTrait.php, line 10

Namespace

Drupal\simple_sitemap\Manager
View source
trait VariantSetterTrait {

  /**
   * @var array
   */
  protected $variants;

  /**
   * @param array|string|true|null $variants
   *  array: Array of variants to be set.
   *  string: A particular variant to be set.
   *  null: Default variant will be set.
   *  true: All existing variants will be set.
   *
   * @todo Check if variants exist and throw exception.
   */
  public function setVariants($variants = NULL) {
    if (NULL === $variants) {
      $this->variants = !empty($default_variant = \Drupal::service('simple_sitemap.settings')
        ->get('default_variant', '')) ? [
        $default_variant,
      ] : [];
    }
    elseif ($variants === TRUE) {
      $this->variants = array_keys(SimpleSitemap::loadMultiple());
    }
    else {
      $this->variants = (array) $variants;
    }
    return $this;
  }

  /**
   * Gets the currently set variants, the default variant, or all variants.
   *
   * @param bool $default_get_all
   *  If true and no variants are set, all variants are returned. If false and
   *  no variants are set, only the default variant is returned.
   *
   * @return array
   */
  protected function getVariants(bool $default_get_all = TRUE) : array {
    if (NULL === $this->variants) {
      $this
        ->setVariants($default_get_all ? TRUE : NULL);
    }
    return $this->variants;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VariantSetterTrait::$variants protected property
VariantSetterTrait::getVariants protected function Gets the currently set variants, the default variant, or all variants.
VariantSetterTrait::setVariants public function @todo Check if variants exist and throw exception.