class CustomLinkManager in Simple XML sitemap 4.x
Class CustomLinkManager
Hierarchy
- class \Drupal\simple_sitemap\Manager\CustomLinkManager uses LinkSettingsTrait, VariantSetterTrait
Expanded class hierarchy of CustomLinkManager
1 file declares its use of CustomLinkManager
- CustomUrlGenerator.php in src/
Plugin/ simple_sitemap/ UrlGenerator/ CustomUrlGenerator.php
1 string reference to 'CustomLinkManager'
1 service uses CustomLinkManager
File
- src/
Manager/ CustomLinkManager.php, line 11
Namespace
Drupal\simple_sitemap\ManagerView source
class CustomLinkManager {
use VariantSetterTrait;
use LinkSettingsTrait;
/**
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* @var \Drupal\Core\Path\PathValidator
*/
protected $pathValidator;
/**
* @var array
*/
protected static $linkSettingDefaults = [
'priority' => '0.5',
'changefreq' => '',
];
/**
* CustomLinks constructor.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* @param \Drupal\Core\Path\PathValidator $path_validator
*/
public function __construct(ConfigFactory $config_factory, PathValidator $path_validator) {
$this->configFactory = $config_factory;
$this->pathValidator = $path_validator;
}
/**
* Stores a custom path along with its settings to configuration for the
* currently set variants.
*
* @param string $path
* @param array $settings
* Settings that are not provided are supplemented by defaults.
*
* @return \Drupal\simple_sitemap\Manager\CustomLinkManager
* @todo Validate $settings and throw exceptions
*/
public function add(string $path, array $settings = []) : CustomLinkManager {
if (empty($variants = $this
->getVariants(FALSE))) {
return $this;
}
if (!(bool) $this->pathValidator
->getUrlIfValidWithoutAccessCheck($path)) {
// todo: log error.
return $this;
}
if ($path[0] !== '/') {
// todo: log error.
return $this;
}
$variant_links = $this
->get(NULL, FALSE, TRUE);
foreach ($variants as $variant) {
$links = [];
$link_key = 0;
if (isset($variant_links[$variant])) {
$links = $variant_links[$variant];
$link_key = count($links);
foreach ($links as $key => $link) {
if ($link['path'] === $path) {
$link_key = $key;
break;
}
}
}
$links[$link_key] = [
'path' => $path,
] + $settings;
$this->configFactory
->getEditable("simple_sitemap.custom_links.{$variant}")
->set('links', $links)
->save();
}
return $this;
}
/**
* Gets custom link settings for the currently set variants.
*
* @param string|null $path
* Limits the result set by an internal path.
* @param bool $supplement_defaults
* Supplements the result set with default custom link settings.
* @param bool $multiple_variants
* If true, returns an array of results keyed by variant name, otherwise it
* returns the result set for the first variant only.
*
* @return array|mixed|null
*
*/
public function get(?string $path = NULL, bool $supplement_defaults = TRUE, bool $multiple_variants = FALSE) : array {
$all_custom_links = [];
foreach ($this
->getVariants(FALSE) as $variant) {
$custom_links = $this->configFactory
->get("simple_sitemap.custom_links.{$variant}")
->get('links');
$custom_links = !empty($custom_links) ? $custom_links : [];
if (!empty($custom_links) && $path !== NULL) {
foreach ($custom_links as $key => $link) {
if ($link['path'] !== $path) {
unset($custom_links[$key]);
}
}
}
if (!empty($custom_links) && $supplement_defaults) {
foreach ($custom_links as $i => $link_settings) {
self::supplementDefaultSettings($link_settings);
$custom_links[$i] = $link_settings;
}
}
$custom_links = $path !== NULL && !empty($custom_links) ? array_values($custom_links)[0] : array_values($custom_links);
if (!empty($custom_links)) {
if ($multiple_variants) {
$all_custom_links[$variant] = $custom_links;
}
else {
return $custom_links;
}
}
}
return $all_custom_links;
}
/**
* Removes custom links from currently set variants.
*
* @param array|string|null $paths
* Limits the removal to certain paths.
*
* @return \Drupal\simple_sitemap\Manager\CustomLinkManager
*/
public function remove($paths = NULL) : CustomLinkManager {
if (empty($variants = $this
->getVariants(FALSE))) {
return $this;
}
if (NULL === $paths) {
foreach ($variants as $variant) {
$this->configFactory
->getEditable("simple_sitemap.custom_links.{$variant}")
->delete();
}
}
else {
$variant_links = $this
->get(NULL, FALSE, TRUE);
foreach ($variant_links as $variant => $links) {
$custom_links = $links;
$save = FALSE;
foreach ((array) $paths as $path) {
foreach ($custom_links as $key => $link) {
if ($link['path'] === $path) {
unset($custom_links[$key]);
$save = TRUE;
break 2;
}
}
}
if ($save) {
$this->configFactory
->getEditable("simple_sitemap.custom_links.{$variant}")
->set('links', array_values($custom_links))
->save();
}
}
}
return $this;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CustomLinkManager:: |
protected | property | ||
CustomLinkManager:: |
protected static | property | ||
CustomLinkManager:: |
protected | property | ||
CustomLinkManager:: |
public | function | Stores a custom path along with its settings to configuration for the currently set variants. | |
CustomLinkManager:: |
public | function | Gets custom link settings for the currently set variants. | |
CustomLinkManager:: |
public | function | Removes custom links from currently set variants. | |
CustomLinkManager:: |
public | function | CustomLinks constructor. | |
LinkSettingsTrait:: |
public static | function | Supplements all missing link setting with default values. | |
VariantSetterTrait:: |
protected | property | ||
VariantSetterTrait:: |
protected | function | Gets the currently set variants, the default variant, or all variants. | |
VariantSetterTrait:: |
public | function | @todo Check if variants exist and throw exception. |