processForm($form_state); if (!$f->alteringForm) return; $form['simple_sitemap'] = [ '#type' => 'details', '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced', '#title' => t('Simple XML sitemap'), '#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '', ]; // Attach some js magic to forms. if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') // todo: JS not working on comment entity form, hence disabling. $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']); // todo: do not show setting when creating new bundle // 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 { // Fix for account page rendering other submit handlers not usable. $form['#submit'][] = 'simple_sitemap_entity_form_submit'; } } /** * Form submission handler called in hook_form_alter. */ function simple_sitemap_entity_form_submit($form, &$form_state) { $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state); $values = $form_state->getValues(); // Fix for values appearing in a sub array on a commerce product entity. $values = isset($values['simple_sitemap']) ? $values['simple_sitemap'] : $values; // Only make changes in DB if sitemap settings actually changed. if ($f->valuesChanged($form, $values)) { $generator = \Drupal::service('simple_sitemap.generator'); switch ($f->entityCategory) { case 'bundle': $f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId(); $generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]); break; case 'instance': $f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId(); $generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]); break; } // Regenerate sitemaps according to user setting. if ($values['simple_sitemap_regenerate_now']) { $generator->generateSitemap(); } } } /** * Implements hook_cron. */ function simple_sitemap_cron() { $generator = \Drupal::service('simple_sitemap.generator'); if ($generator->getSetting('cron_generate')) { $generator->generateSitemap('backend'); } } /** * Implements hook_entity_bundle_delete(). * * Removes settings of the removed bundle. * * @todo Not working for menu bundles, as they are technically not bundles. Implement hook_menu_delete(). */ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) { $generator = \Drupal::service('simple_sitemap.generator'); $entity_types = $generator->getConfig('entity_types'); if (isset($entity_types[$entity_type_id][$bundle])) { unset($entity_types[$entity_type_id][$bundle]); $generator->saveConfig('entity_types', $entity_types); $message = t("You may want to regenerate your XML sitemap now.", ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']); if ($generator->getSetting('cron_generate')) { $message .= '
' . t('Otherwise the sitemap will be regenerated on the next cron run.'); } // drupal_set_message($message); // todo: Commented out, as html code is visible. } } /** * Implements hook_entity_delete(). * * Removes settings of the removed entity. */ function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) { $generator = \Drupal::service('simple_sitemap.generator'); $entity_types = $generator->getConfig('entity_types'); $entity_type_id = $entity->getEntityTypeId(); $bundle_name = !empty($entity->bundle()) ? $entity->bundle() : $entity_type_id; $bundle_name = $bundle_name == 'menu_link_content'&& method_exists($entity, 'getMenuName') ? $entity->getMenuName() : $bundle_name; // Menu fix. if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()])) { unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()]); $generator->saveConfig('entity_types', $entity_types); } }