You are here

function simple_sitemap_form_alter in Simple XML sitemap 8.2

Same name and namespace in other branches
  1. 8.3 simple_sitemap.module \simple_sitemap_form_alter()
  2. 4.x simple_sitemap.module \simple_sitemap_form_alter()

Implements hook_form_alter.

Adds sitemap settings to entity types that are supported via plugins.

Parameters

$form:

\Drupal\Core\Form\FormStateInterface $form_state: _id

File

./simple_sitemap.module, line 35
Main module file containing hooks.

Code

function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /**
   * @var Drupal\simple_sitemap\Form\FormHelper $f
   */
  $f = \Drupal::service('simple_sitemap.form_helper');
  if (!$f
    ->processForm($form_state)) {
    return;
  }
  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Simple XML sitemap'),
    '#description' => $f
      ->getEntityCategory() === 'instance' ? t('Settings for this entity can be overridden here.') : '',
  ];

  // Attach some js magic to forms.
  // todo: JS not working on comment entity form, hence disabling.
  if ($f
    ->getEntityTypeId() !== 'comment' || $f
    ->getEntityCategory() !== 'instance') {
    $form['#attached']['library'][] = 'simple_sitemap/form';
  }

  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  if (isset($form['additional_settings'])) {
    $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
  }
  $f
    ->displayEntitySettings($form['simple_sitemap'])
    ->displayRegenerateNow($form['simple_sitemap']);

  // Add submission handler.
  if (isset($form['actions']['submit']['#submit'])) {
    foreach (array_keys($form['actions']) as $action) {
      if ($action !== 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
      }
    }
  }
  else {
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
  }
}