You are here

function simple_sitemap_entity_form_submit in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 simple_sitemap.module \simple_sitemap_entity_form_submit()
  2. 8.2 simple_sitemap.module \simple_sitemap_entity_form_submit()

Form submission handler called in hook_form_alter.

Parameters

$form:

\Drupal\Core\Form\FormStateInterface $form_state:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'simple_sitemap_entity_form_submit'
simple_sitemap_form_alter in ./simple_sitemap.module
Implements hook_form_alter().

File

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

Code

function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {

  /** @var Drupal\simple_sitemap\Form\FormHelper $f */
  $f = \Drupal::service('simple_sitemap.form_helper');
  if (!$f
    ->processForm($form_state)) {
    return;
  }
  $values = $form_state
    ->getValues();

  // Fix for values appearing in a sub array on a commerce product entity.
  $values = $values['simple_sitemap'] ?? $values;
  if ($f
    ->valuesChanged($form, $values)) {

    /** @var \Drupal\simple_sitemap\Manager\Generator $generator */
    $generator = \Drupal::service('simple_sitemap.generator');
    foreach (SimpleSitemap::loadMultiple() as $variant_id => $variant) {
      if (isset($values['index_' . $variant_id . '_' . $f
        ->getEntityTypeId() . '_settings'])) {

        // Variants may have changed since form load.
        $settings = [
          'index' => (bool) $values['index_' . $variant_id . '_' . $f
            ->getEntityTypeId() . '_settings'],
          'priority' => $values['priority_' . $variant_id . '_' . $f
            ->getEntityTypeId() . '_settings'],
          'changefreq' => $values['changefreq_' . $variant_id . '_' . $f
            ->getEntityTypeId() . '_settings'],
          'include_images' => (bool) $values['include_images_' . $variant_id . '_' . $f
            ->getEntityTypeId() . '_settings'],
        ];
        $generator
          ->setVariants($variant_id);
        switch ($f
          ->getEntityCategory()) {
          case 'bundle':
            $generator
              ->entityManager()
              ->setBundleSettings($f
              ->getEntityTypeId(), $f
              ->getBundleName(), $settings);
            break;
          case 'instance':
            if (!$f
              ->entityIsNew()) {

              // Make sure the entity is saved first for multi-step forms, see https://www.drupal.org/project/simple_sitemap/issues/3080510.
              $generator
                ->entityManager()
                ->setEntityInstanceSettings($f
                ->getEntityTypeId(), $f
                ->getInstanceId(), $settings);
            }
            break;
        }
      }
    }

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
      $generator
        ->setVariants(TRUE)
        ->rebuildQueue()
        ->generateSitemap();
    }
  }
}