You are here

protected function SimplesitemapCustomLinksForm::stringToCustomLinks in Simple XML sitemap 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/SimplesitemapCustomLinksForm.php \Drupal\simple_sitemap\Form\SimplesitemapCustomLinksForm::stringToCustomLinks()

Parameters

$custom_links_string:

Return value

array

2 calls to SimplesitemapCustomLinksForm::stringToCustomLinks()
SimplesitemapCustomLinksForm::submitForm in src/Form/SimplesitemapCustomLinksForm.php
Form submission handler.
SimplesitemapCustomLinksForm::validateForm in src/Form/SimplesitemapCustomLinksForm.php
Form validation handler.

File

src/Form/SimplesitemapCustomLinksForm.php, line 112

Class

SimplesitemapCustomLinksForm
Class SimplesitemapCustomLinksForm @package Drupal\simple_sitemap\Form

Namespace

Drupal\simple_sitemap\Form

Code

protected function stringToCustomLinks($custom_links_string) {

  // Unify newline characters and explode into array.
  $custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));

  // Remove empty values and whitespaces from array.
  $custom_links_string_lines = array_filter(array_map('trim', $custom_links_string_lines));
  $custom_links = [];
  foreach ($custom_links_string_lines as $i => &$line) {
    $link_settings = explode(' ', $line);
    $custom_links[$i]['path'] = $link_settings[0];

    // If two arguments are provided for a link, assume the first to be
    // priority, the second to be changefreq.
    if (!empty($link_settings[1]) && !empty($link_settings[2])) {
      $custom_links[$i]['priority'] = $link_settings[1];
      $custom_links[$i]['changefreq'] = $link_settings[2];
    }
    else {

      // If one argument is provided for a link, guess if it is priority or
      // changefreq.
      if (!empty($link_settings[1])) {
        if (is_numeric($link_settings[1])) {
          $custom_links[$i]['priority'] = $link_settings[1];
        }
        else {
          $custom_links[$i]['changefreq'] = $link_settings[1];
        }
      }
    }
  }
  return $custom_links;
}