Simplesitemap.php 19.1 KB
Newer Older
1 2
<?php

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

Pawel G's avatar
Pawel G committed
5 6 7
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathValidator;
Pawel G's avatar
Pawel G committed
8 9
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
10

11
/**
Pawel G's avatar
Pawel G committed
12
 * Class Simplesitemap
Pawel G's avatar
Pawel G committed
13
 * @package Drupal\simple_sitemap
14 15 16
 */
class Simplesitemap {

Pawel G's avatar
Pawel G committed
17 18 19
  /**
   * @var \Drupal\simple_sitemap\SitemapGenerator
   */
20
  protected $sitemapGenerator;
Pawel G's avatar
Pawel G committed
21 22 23 24

  /**
   * @var \Drupal\simple_sitemap\EntityHelper
   */
25
  protected $entityHelper;
Pawel G's avatar
Pawel G committed
26 27 28 29

  /**
   * @var \Drupal\Core\Config\ConfigFactory
   */
30
  protected $configFactory;
Pawel G's avatar
Pawel G committed
31 32 33 34

  /**
   * @var \Drupal\Core\Database\Connection
   */
35
  protected $db;
Pawel G's avatar
Pawel G committed
36 37 38 39

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
40
  protected $entityTypeManager;
Pawel G's avatar
Pawel G committed
41 42 43 44

  /**
   * @var \Drupal\Core\Path\PathValidator
   */
45
  protected $pathValidator;
Pawel G's avatar
Pawel G committed
46

47 48 49 50 51
  /**
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

Pawel G's avatar
Pawel G committed
52 53 54
  /**
   * @var array
   */
55
  protected static $allowed_link_settings = [
56
    'entity' => ['index', 'priority', 'changefreq', 'include_images'],
57 58 59 60
    'custom' => ['priority', 'changefreq'],
  ];

  protected static $link_setting_defaults = [
61
    'index' => 1,
62
    'priority' => 0.5,
63
    'changefreq' => '',
64
    'include_images' => 0,
65
  ];
66

67 68
  /**
   * Simplesitemap constructor.
Pawel G's avatar
Pawel G committed
69
   * @param \Drupal\simple_sitemap\SitemapGenerator $sitemapGenerator
70
   * @param \Drupal\simple_sitemap\EntityHelper $entityHelper
Pawel G's avatar
Pawel G committed
71 72 73 74 75
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   * @param \Drupal\Core\Database\Connection $database
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   * @param \Drupal\Core\Path\PathValidator $pathValidator
   * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
76
   */
77
  public function __construct(
Pawel G's avatar
Pawel G committed
78
    SitemapGenerator $sitemapGenerator,
79
    EntityHelper $entityHelper,
Pawel G's avatar
Pawel G committed
80
    ConfigFactory $configFactory,
Pawel G's avatar
Pawel G committed
81 82 83
    Connection $database,
    EntityTypeManagerInterface $entityTypeManager,
    PathValidator $pathValidator,
84
    DateFormatter $dateFormatter
85 86
  ) {
    $this->sitemapGenerator = $sitemapGenerator;
87
    $this->entityHelper = $entityHelper;
Pawel G's avatar
Pawel G committed
88
    $this->configFactory = $configFactory;
89 90
    $this->db = $database;
    $this->entityTypeManager = $entityTypeManager;
91
    $this->pathValidator = $pathValidator;
92
    $this->dateFormatter = $dateFormatter;
93 94
  }

95
  /**
96 97
   * Returns a specific sitemap setting or a default value if setting does not
   * exist.
Pawel G's avatar
Pawel G committed
98
   *
99 100 101 102 103 104 105 106
   * @param string $name
   *   Name of the setting, like 'max_links'.
   *
   * @param mixed $default
   *   Value to be returned if the setting does not exist in the configuration.
   *
   * @return mixed
   *   The current setting from configuration or a default value.
Pawel G's avatar
Pawel G committed
107
   */
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  public function getSetting($name, $default = FALSE) {
    $setting = $this->configFactory
      ->get('simple_sitemap.settings')
      ->get($name);
    return NULL !== $setting ? $setting : $default;
  }

  /**
   * Stores a specific sitemap setting in configuration.
   *
   * @param string $name
   *   Setting name, like 'max_links'.
   * @param mixed $setting
   *   The setting to be saved.
   *
   * @return $this
   */
  public function saveSetting($name, $setting) {
    $this->configFactory->getEditable("simple_sitemap.settings")
      ->set($name, $setting)->save();
    return $this;
  }

  /**
   * Returns the whole sitemap, a requested sitemap chunk,
   * or the sitemap index file.
   *
   * @param int $chunk_id
   *
   * @return string|false
   *   If no sitemap id provided, either a sitemap index is returned, or the
   *   whole sitemap, if the amount of links does not exceed the max links
   *   setting. If a sitemap id is provided, a sitemap chunk is returned. False
   *   if sitemap is not retrievable from the database.
   */
  public function getSitemap($chunk_id = NULL) {
    $chunk_info = $this->fetchSitemapChunkInfo();

    if (NULL === $chunk_id || !isset($chunk_info[$chunk_id])) {

      if (count($chunk_info) > 1) {
        // Return sitemap index, if there are multiple sitemap chunks.
        return $this->getSitemapIndex($chunk_info);
      }
      else {
        // Return sitemap if there is only one chunk.
        return count($chunk_info) === 1
        && isset($chunk_info[SitemapGenerator::FIRST_CHUNK_INDEX])
          ? $this->fetchSitemapChunk(SitemapGenerator::FIRST_CHUNK_INDEX)
            ->sitemap_string
          : FALSE;
      }
    }
    else {
      // Return specific sitemap chunk.
      return $this->fetchSitemapChunk($chunk_id)->sitemap_string;
    }
  }

  /**
   * Fetches all sitemap chunk timestamps keyed by chunk ID.
   *
   * @return array
   *   An array containing chunk creation timestamps keyed by chunk ID.
   */
  protected function fetchSitemapChunkInfo() {
174
    return $this->db
175
      ->query("SELECT id, sitemap_created FROM {simple_sitemap}")
176 177 178
      ->fetchAllAssoc('id');
  }

179 180 181 182 183 184 185 186 187
  /**
   * Fetches a single sitemap chunk by ID.
   *
   * @param int $id
   *   The chunk ID.
   *
   * @return object
   *   A sitemap chunk object.
   */
188
  protected function fetchSitemapChunk($id) {
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    return $this->db->query('SELECT * FROM {simple_sitemap} WHERE id = :id',
      [':id' => $id])->fetchObject();
  }

  /**
   * Generates the sitemap for all languages and saves it to the db.
   *
   * @param string $from
   *   Can be 'form', 'cron', 'drush' or 'nobatch'.
   *   This decides how the batch process is to be run.
   */
  public function generateSitemap($from = 'form') {
    $this->sitemapGenerator
      ->setGenerator($this)
      ->setGenerateFrom($from)
      ->startGeneration();
  }

  /**
   * Generates and returns the sitemap index as string.
   *
Pawel G's avatar
Pawel G committed
210 211
   * @param array $chunk_info
   *   Array containing chunk creation timestamps keyed by chunk ID.
212 213 214 215
   *
   * @return string
   *   The sitemap index.
   */
Pawel G's avatar
Pawel G committed
216
  protected function getSitemapIndex($chunk_info) {
217 218
    return $this->sitemapGenerator
      ->setGenerator($this)
Pawel G's avatar
Pawel G committed
219
      ->generateSitemapIndex($chunk_info);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  }

  /**
   * Returns a 'time ago' string of last timestamp generation.
   *
   * @return string|false
   *   Formatted timestamp of last sitemap generation, otherwise FALSE.
   */
  public function getGeneratedAgo() {
    $chunks = $this->fetchSitemapChunkInfo();
    if (isset($chunks[SitemapGenerator::FIRST_CHUNK_INDEX]->sitemap_created)) {
      return $this->dateFormatter
        ->formatInterval(REQUEST_TIME - $chunks[SitemapGenerator::FIRST_CHUNK_INDEX]
            ->sitemap_created);
    }
    return FALSE;
  }

238 239
  /**
   * Enables sitemap support for an entity type. Enabled entity types show
240 241
   * sitemap settings on their bundle setting forms. If an enabled entity type
   * features bundles (e.g. 'node'), it needs to be set up with
242 243 244
   * setBundleSettings() as well.
   *
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
245
   *   Entity type id like 'node'.
246
   *
Pawel G's avatar
Pawel G committed
247
   * @return $this
248 249
   */
  public function enableEntityType($entity_type_id) {
250 251 252 253
    $enabled_entity_types = $this->getSetting('enabled_entity_types');
    if (!in_array($entity_type_id, $enabled_entity_types)) {
      $enabled_entity_types[] = $entity_type_id;
      $this->saveSetting('enabled_entity_types', $enabled_entity_types);
254
    }
Pawel G's avatar
Pawel G committed
255
    return $this;
256 257 258 259 260 261 262 263
  }

  /**
   * Disables sitemap support for an entity type. Disabling support for an
   * entity type deletes its sitemap settings permanently and removes sitemap
   * settings from entity forms.
   *
   * @param string $entity_type_id
264
   *  Entity type id like 'node'.
265
   *
Pawel G's avatar
Pawel G committed
266
   * @return $this
267 268
   */
  public function disableEntityType($entity_type_id) {
269 270 271

    // Updating settings.
    $enabled_entity_types = $this->getSetting('enabled_entity_types');
272
    if (FALSE !== ($key = array_search($entity_type_id, $enabled_entity_types))) {
273
      unset ($enabled_entity_types[$key]);
274
      $this->saveSetting('enabled_entity_types', array_values($enabled_entity_types));
275 276 277
    }

    // Deleting inclusion settings.
278
    $config_names = $this->configFactory->listAll("simple_sitemap.bundle_settings.$entity_type_id.");
279 280
    foreach($config_names as $config_name) {
      $this->configFactory->getEditable($config_name)->delete();
281
    }
282 283 284

    // Deleting entity overrides.
    $this->removeEntityInstanceSettings($entity_type_id);
Pawel G's avatar
Pawel G committed
285
    return $this;
286 287 288
  }

  /**
Pawel G's avatar
Pawel G committed
289
   * Sets sitemap settings for a non-bundle entity type (e.g. user) or a bundle
290 291 292
   * of an entity type (e.g. page).
   *
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
293
   *   Entity type id like 'node' the bundle belongs to.
294
   * @param string $bundle_name
Pawel G's avatar
Pawel G committed
295
   *   Name of the bundle. NULL if entity type has no bundles.
296
   * @param array $settings
Pawel G's avatar
Pawel G committed
297
   *   An array of sitemap settings for this bundle/entity type.
298
   *   Example: ['index' => TRUE, 'priority' => 0.5, 'changefreq' => 'never', 'include_images' => FALSE].
Pawel G's avatar
Pawel G committed
299 300
   *
   * @return $this
301 302
   *
   * @todo: enableEntityType automatically
303
   */
304
  public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings = []) {
305
    $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
306

307 308 309 310 311
    $old_settings = $this->getBundleSettings($entity_type_id, $bundle_name);
    $settings = !empty($old_settings) ? array_merge($old_settings, $settings) : $this->supplementDefaultSettings('entity', $settings);

    $bundle_settings = $this->configFactory
      ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name");
312
    foreach($settings as $setting_key => $setting) {
313
      if ($setting_key === 'index') {
314 315
        $setting = intval($setting);
      }
316
      $bundle_settings->set($setting_key, $setting);
317
    }
318
    $bundle_settings->save();
319 320

    // Delete entity overrides which are identical to new bundle setting.
Pawel G's avatar
Pawel G committed
321
    $sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
322 323 324
    if (isset($sitemap_entity_types[$entity_type_id])) {
      $entity_type = $sitemap_entity_types[$entity_type_id];
      $keys = $entity_type->getKeys();
325 326

      // Menu fix.
327 328
      $keys['bundle'] = $entity_type_id == 'menu_link_content' ? 'menu_name' : $keys['bundle'];

329
      $query = $this->entityTypeManager->getStorage($entity_type_id)->getQuery();
330
      if (!$this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
331
        $query->condition($keys['bundle'], $bundle_name);
332
      }
333 334 335 336 337 338 339 340 341
      $entity_ids = $query->execute();

      $query = $this->db->select('simple_sitemap_entity_overrides', 'o')
        ->fields('o', ['id', 'inclusion_settings'])
        ->condition('o.entity_type', $entity_type_id);
      if (!empty($entity_ids)) {
        $query->condition('o.entity_id', $entity_ids, 'IN');
      }

342
      $delete_instances = [];
343
      foreach($query->execute()->fetchAll() as $result) {
344 345 346
        $delete = TRUE;
        $instance_settings = unserialize($result->inclusion_settings);
        foreach ($instance_settings as $setting_key => $instance_setting) {
347
          if ($instance_setting != $settings[$setting_key]) {
348 349 350 351 352
            $delete = FALSE;
            break;
          }
        }
        if ($delete) {
353
          $delete_instances[] = $result->id;
354
        }
355
      }
356 357 358 359 360
      if (!empty($delete_instances)) {
        $this->db->delete('simple_sitemap_entity_overrides')
          ->condition('id', $delete_instances, 'IN')
          ->execute();
      }
361
    }
362 363 364
    else {
      //todo: log error
    }
Pawel G's avatar
Pawel G committed
365
    return $this;
366 367
  }

368
  /**
369 370
   * Gets sitemap settings for an entity bundle, a non-bundle entity type or for
   * all entity types and their bundles.
371
   *
372 373 374
   * @param string|null $entity_type_id
   *  If set to null, sitemap settings for all entity types and their bundles
   *  are fetched.
375 376 377
   * @param string|null $bundle_name
   *
   * @return array|false
Pawel G's avatar
Pawel G committed
378 379
   *  Array of sitemap settings for an entity bundle, a non-bundle entity type
   *  or for all entity types and their bundles.
380
   *  False if entity type does not exist.
381
   */
382
  public function getBundleSettings($entity_type_id = NULL, $bundle_name = NULL) {
383
    if (NULL !== $entity_type_id) {
384
      $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
385
      $bundle_settings = $this->configFactory
386 387
        ->get("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
        ->get();
388
      return !empty($bundle_settings) ? $bundle_settings : FALSE;
389
    }
390
    else {
391
      $config_names = $this->configFactory->listAll("simple_sitemap.bundle_settings.");
392
      $all_settings = [];
393 394
      foreach($config_names as $config_name) {
        $config_name_parts = explode('.', $config_name);
395
        $all_settings[$config_name_parts[2]][$config_name_parts[3]] = $this->configFactory->get($config_name)->get();
396 397 398 399 400 401
      }
      return $all_settings;
    }
  }

  /**
402 403
   * Supplements all missing link setting with default values.
   *
404 405 406 407 408 409 410
   * @param string $type
   * @param array $settings
   * @return array
   */
  protected function supplementDefaultSettings($type, $settings) {
    foreach(self::$allowed_link_settings[$type] as $allowed_link_setting) {
      if (!isset($settings[$allowed_link_setting])
411
        && isset(self::$link_setting_defaults[$allowed_link_setting])) {
412
        $settings[$allowed_link_setting] = self::$link_setting_defaults[$allowed_link_setting];
413 414
      }
    }
415
    return $settings;
416 417
  }

Pawel G's avatar
Pawel G committed
418 419 420
  /**
   * Overrides entity bundle/entity type sitemap settings for a single entity.
   *
Pawel G's avatar
Pawel G committed
421 422 423
   * @param string $entity_type_id
   * @param int $id
   * @param array $settings
Pawel G's avatar
Pawel G committed
424
   *
Pawel G's avatar
Pawel G committed
425 426
   * @return $this
   */
427
  public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
428
    $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id);
429 430 431
    $bundle_settings = $this->getBundleSettings(
      $entity_type_id, $this->entityHelper->getEntityInstanceBundleName($entity)
    );
432
    if (!empty($bundle_settings)) {
433 434 435 436

      // Check if overrides are different from bundle setting before saving.
      $override = FALSE;
      foreach ($settings as $key => $setting) {
437
        if (!isset($bundle_settings[$key]) || $setting != $bundle_settings[$key]) {
438 439 440 441
          $override = TRUE;
          break;
        }
      }
Pawel G's avatar
Pawel G committed
442 443
      // Save overrides for this entity if something is different.
      if ($override) {
444 445 446 447 448 449 450
        $this->db->merge('simple_sitemap_entity_overrides')
          ->key([
            'entity_type' => $entity_type_id,
            'entity_id' => $id])
          ->fields([
            'entity_type' => $entity_type_id,
            'entity_id' => $id,
451
            'inclusion_settings' => serialize(array_merge($bundle_settings, $settings)),])
452
          ->execute();
453
      }
Pawel G's avatar
Pawel G committed
454 455
      // Else unset override.
      else {
456
        $this->removeEntityInstanceSettings($entity_type_id, $id);
457
      }
458 459 460
    }
    else {
      //todo: log error
461
    }
Pawel G's avatar
Pawel G committed
462
    return $this;
463 464
  }

Pawel G's avatar
Pawel G committed
465
  /**
466
   * Gets sitemap settings for an entity instance which overrides the sitemap
467
   * settings of its bundle, or bundle settings, if they are not overridden.
Pawel G's avatar
Pawel G committed
468
   *
Pawel G's avatar
Pawel G committed
469
   * @param string $entity_type_id
470
   * @param int $id
Pawel G's avatar
Pawel G committed
471
   *
472
   * @return array|false
Pawel G's avatar
Pawel G committed
473
   */
474 475 476 477 478 479 480 481 482
  public function getEntityInstanceSettings($entity_type_id, $id) {
    $results = $this->db->select('simple_sitemap_entity_overrides', 'o')
      ->fields('o', ['inclusion_settings'])
      ->condition('o.entity_type', $entity_type_id)
      ->condition('o.entity_id', $id)
      ->execute()
      ->fetchField();

    if (!empty($results)) {
483
      return unserialize($results);
484 485
    }
    else {
486 487 488 489 490 491
      $entity = $this->entityTypeManager->getStorage($entity_type_id)
        ->load($id);
      return $this->getBundleSettings(
        $entity_type_id,
        $this->entityHelper->getEntityInstanceBundleName($entity)
      );
492 493 494
    }
  }

495 496 497 498 499 500 501 502 503 504 505 506
  /**
   * Removes sitemap settings for an entity that overrides the sitemap settings
   * of its bundle.
   *
   * @param string $entity_type_id
   * @param string|null $entity_ids
   *
   * @return $this
   */
  public function removeEntityInstanceSettings($entity_type_id, $entity_ids = NULL) {
    $query = $this->db->delete('simple_sitemap_entity_overrides')
      ->condition('entity_type', $entity_type_id);
507
    if (NULL !== $entity_ids) {
508 509 510 511 512 513 514
      $entity_ids = !is_array($entity_ids) ? [$entity_ids] : $entity_ids;
      $query->condition('entity_id', $entity_ids, 'IN');
    }
    $query->execute();
    return $this;
  }

Pawel G's avatar
Pawel G committed
515 516 517 518
  /**
   * Checks if an entity bundle (or a non-bundle entity type) is set to be
   * indexed in the sitemap settings.
   *
519 520
   * @param string $entity_type_id
   * @param string|null $bundle_name
Pawel G's avatar
Pawel G committed
521
   *
Pawel G's avatar
Pawel G committed
522 523
   * @return bool
   */
524
  public function bundleIsIndexed($entity_type_id, $bundle_name = NULL) {
525 526 527 528
    $settings = $this->getBundleSettings($entity_type_id, $bundle_name);
    return !empty($settings['index']);
  }

Pawel G's avatar
Pawel G committed
529 530 531
  /**
   * Checks if an entity type is enabled in the sitemap settings.
   *
532
   * @param string $entity_type_id
Pawel G's avatar
Pawel G committed
533
   *
Pawel G's avatar
Pawel G committed
534 535
   * @return bool
   */
536
  public function entityTypeIsEnabled($entity_type_id) {
537
    return in_array($entity_type_id, $this->getSetting('enabled_entity_types', []));
538 539
  }

Pawel G's avatar
Pawel G committed
540
  /**
541
   * Stores a custom path along with its sitemap settings to configuration.
Pawel G's avatar
Pawel G committed
542
   *
Pawel G's avatar
Pawel G committed
543 544
   * @param string $path
   * @param array $settings
Pawel G's avatar
Pawel G committed
545
   *
Pawel G's avatar
Pawel G committed
546
   * @return $this
547 548
   *
   * @todo Validate $settings and throw exceptions
Pawel G's avatar
Pawel G committed
549
   */
550
  public function addCustomLink($path, $settings = []) {
Pawel G's avatar
Pawel G committed
551 552 553 554 555 556 557 558
    if (!$this->pathValidator->isValid($path)) {
      // todo: log error.
      return $this;
    }
    if ($path[0] != '/') {
      // todo: log error.
      return $this;
    }
Pawel G's avatar
Pawel G committed
559

560
    $custom_links = $this->getCustomLinks(FALSE);
Pawel G's avatar
Pawel G committed
561
    foreach ($custom_links as $key => $link) {
562
      if ($link['path'] === $path) {
563 564 565 566 567
        $link_key = $key;
        break;
      }
    }
    $link_key = isset($link_key) ? $link_key : count($custom_links);
568
    $custom_links[$link_key] = ['path' => $path] + $settings;
569
    $this->configFactory->getEditable("simple_sitemap.custom")
570
      ->set('links', $custom_links)->save();
Pawel G's avatar
Pawel G committed
571
    return $this;
572 573
  }

574 575 576
  /**
   * Returns an array of custom paths and their sitemap settings.
   *
577
   * @param bool $supplement_default_settings
578 579
   * @return array
   */
580
  public function getCustomLinks($supplement_default_settings = TRUE) {
581 582
    $custom_links = $this->configFactory
      ->get('simple_sitemap.custom')
583
      ->get('links');
584 585 586 587 588 589 590

    if ($supplement_default_settings) {
      foreach ($custom_links as $i => $link_settings) {
        $custom_links[$i] = $this->supplementDefaultSettings('custom', $link_settings);
      }
    }

591
    return $custom_links !== NULL ? $custom_links : [];
592 593
  }

Pawel G's avatar
Pawel G committed
594 595 596
  /**
   * Returns settings for a custom path added to the sitemap settings.
   *
Pawel G's avatar
Pawel G committed
597
   * @param string $path
Pawel G's avatar
Pawel G committed
598
   *
Pawel G's avatar
Pawel G committed
599
   * @return array|false
Pawel G's avatar
Pawel G committed
600
   */
601
  public function getCustomLink($path) {
602
    foreach ($this->getCustomLinks() as $key => $link) {
603
      if ($link['path'] === $path) {
604
        return $link;
605 606 607 608 609
      }
    }
    return FALSE;
  }

Pawel G's avatar
Pawel G committed
610 611 612
  /**
   * Removes a custom path from the sitemap settings.
   *
Pawel G's avatar
Pawel G committed
613
   * @param string $path
Pawel G's avatar
Pawel G committed
614
   *
Pawel G's avatar
Pawel G committed
615 616
   * @return $this
   */
617
  public function removeCustomLink($path) {
618
    $custom_links = $this->getCustomLinks(FALSE);
Pawel G's avatar
Pawel G committed
619
    foreach ($custom_links as $key => $link) {
620
      if ($link['path'] === $path) {
621 622
        unset($custom_links[$key]);
        $custom_links = array_values($custom_links);
623
        $this->configFactory->getEditable("simple_sitemap.custom")
624
          ->set('links', $custom_links)->save();
625
        break;
626 627
      }
    }
Pawel G's avatar
Pawel G committed
628
    return $this;
629 630
  }

Pawel G's avatar
Pawel G committed
631 632
  /**
   * Removes all custom paths from the sitemap settings.
Pawel G's avatar
Pawel G committed
633 634
   *
   * @return $this
Pawel G's avatar
Pawel G committed
635
   */
636
  public function removeCustomLinks() {
637
    $this->configFactory->getEditable("simple_sitemap.custom")
638
      ->set('links', [])->save();
Pawel G's avatar
Pawel G committed
639
    return $this;
640
  }
641
}