You are here

public function SimplesitemapCustomLinksForm::submitForm in Simple XML sitemap 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/SimplesitemapCustomLinksForm.php, line 100
Contains \Drupal\simplesitemap\Form\SimplesitemapCustomLinksForm.

Class

SimplesitemapCustomLinksForm
SimplesitemapCustomLinksFrom

Namespace

Drupal\simplesitemap\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $sitemap = new Simplesitemap();
  $custom_links_string = str_replace("\r\n", "\n", $form_state
    ->getValue('custom_links'));
  $custom_links_string_lines = array_filter(explode("\n", $custom_links_string), 'trim');
  $custom_link_config = array();
  foreach ($custom_links_string_lines as $line) {
    $line_settings = explode(' ', $line, 2);
    $custom_link_config[]['path'] = $line_settings[0];
    if (isset($line_settings[1])) {
      end($custom_link_config);
      $key = key($custom_link_config);
      $custom_link_config[$key]['priority'] = number_format((double) $line_settings[1], 1, '.', '');
    }
  }
  $sitemap
    ->save_config('custom', $custom_link_config);
  parent::submitForm($form, $form_state);

  // Regenerate sitemaps according to user setting.
  if ($form_state
    ->getValue('simplesitemap_regenerate_now')) {
    $sitemap
      ->generate_sitemap();
    drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.", array(
      '@url' => $GLOBALS['base_url'] . '/sitemap.xml',
    )));
  }
}