simple_sitemap.module 5.36 KB
Newer Older
Pawel G's avatar
Pawel G committed
1
<?php
2 3 4 5
/**
 * @file
 * Main module file containing hooks.
 */
Pawel G's avatar
Pawel G committed
6

7
use Drupal\simple_sitemap\Form\Form;
8

9 10 11 12 13 14 15 16
/**
 * Implements hook_help.
 */
function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
  return $route_name === 'help.page.simple_sitemap' ?
    check_markup(file_get_contents(dirname(__FILE__) . "/README.txt")) : NULL;
}

17 18
/**
 * Implements hook_form_alter.
19 20
 *
 * Adds sitemap settings to entity types that are supported via plugins.
21
 */
Pawel G's avatar
Pawel G committed
22
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
23
  $f = \Drupal::service('simple_sitemap.form')->processForm($form_state);
Pawel G's avatar
Pawel G committed
24
  if (!$f->alteringForm)
25
    return;
Pawel G's avatar
Pawel G committed
26 27 28 29 30

  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Simple XML sitemap'),
31
    '#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '',
Pawel G's avatar
Pawel G committed
32 33 34 35 36 37 38
  ];
  // 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';
39
  }
Pawel G's avatar
Pawel G committed
40 41
  $f->displayEntitySettings($form['simple_sitemap'])
    ->displayRegenerateNow($form['simple_sitemap']); // todo: do not show setting when creating new bundle
42

43
  // Add submission handler.
44
  if (isset($form['actions']['submit']['#submit'])) {
45 46 47 48 49
    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';
      }
    }
50
  }
51
  else { // Fix for account page rendering other submit handlers not usable.
Pawel G's avatar
Pawel G committed
52
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
53
  }
Pawel G's avatar
Pawel G committed
54 55
}

56
/**
57
 * Form submission handler called in hook_form_alter.
58
 */
Pawel G's avatar
Pawel G committed
59
function simple_sitemap_entity_form_submit($form, &$form_state) {
60

61 62
  $f = \Drupal::service('simple_sitemap.form')->processForm($form_state);

63 64 65 66
  $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;
67

68
  // Only make changes in DB if sitemap settings actually changed.
69
  if ($f->valuesChanged($form, $values)) {
Pawel G's avatar
Pawel G committed
70
    $generator = \Drupal::service('simple_sitemap.generator');
71

72
    switch ($f->entityCategory) {
73 74

      case 'bundle':
Pawel G's avatar
Pawel G committed
75
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
Pawel G's avatar
Pawel G committed
76
        $generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
77 78
        break;

79
      case 'instance':
Pawel G's avatar
Pawel G committed
80
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
Pawel G's avatar
Pawel G committed
81
        $generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
82
        break;
83
    }
84 85 86

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
Pawel G's avatar
Pawel G committed
87
      $generator->generateSitemap();
88
    }
89 90 91 92 93 94
  }
}

/**
 * Implements hook_cron.
 */
Pawel G's avatar
Pawel G committed
95
function simple_sitemap_cron() {
Pawel G's avatar
Pawel G committed
96 97 98
  $generator = \Drupal::service('simple_sitemap.generator');
  if ($generator->getSetting('cron_generate')) {
    $generator->generateSitemap('backend');
99
  }
Pawel G's avatar
Pawel G committed
100
}
101

102 103
/**
 * Implements hook_entity_bundle_delete().
104 105
 *
 * Removes settings of the removed bundle.
106
 *
107
 * @todo Not working for menu bundles, as they are technically not bundles. Implement hook_menu_delete().
108
 */
Pawel G's avatar
Pawel G committed
109
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
Pawel G's avatar
Pawel G committed
110 111
  $generator = \Drupal::service('simple_sitemap.generator');
  $entity_types = $generator->getConfig('entity_types');
112 113
  if (isset($entity_types[$entity_type_id][$bundle])) {
    unset($entity_types[$entity_type_id][$bundle]);
Pawel G's avatar
Pawel G committed
114
    $generator->saveConfig('entity_types', $entity_types);
Pawel G's avatar
Pawel G committed
115
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']);
Pawel G's avatar
Pawel G committed
116
    if ($generator->getSetting('cron_generate')) {
117
      $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
118
    }
119
//    drupal_set_message($message); // todo: Commented out, as html code is visible.
120 121
  }
}
122 123 124 125 126 127 128

/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
 */
function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
Pawel G's avatar
Pawel G committed
129 130
  $generator = \Drupal::service('simple_sitemap.generator');
  $entity_types = $generator->getConfig('entity_types');
131 132 133 134 135
  $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()]);
Pawel G's avatar
Pawel G committed
136
    $generator->saveConfig('entity_types', $entity_types);
137 138
  }
}
139