simple_sitemap.module 5.59 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1
<?php
gbyte.co's avatar
gbyte.co committed
2

3 4 5 6
/**
 * @file
 * Main module file containing hooks.
 */
gbyte.co's avatar
gbyte.co committed
7

gbyte.co's avatar
gbyte.co committed
8
use \Drupal\Core\Form\FormStateInterface;
gbyte.co's avatar
gbyte.co committed
9 10
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
11
use Drupal\system\MenuInterface;
12

13 14 15
/**
 * Implements hook_help.
 */
gbyte.co's avatar
gbyte.co committed
16
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
17
  return $route_name === 'help.page.simple_sitemap' ?
18
    check_markup(file_get_contents(dirname(__FILE__) . "/README.md")) : NULL;
19 20
}

21 22
/**
 * Implements hook_form_alter.
23 24
 *
 * Adds sitemap settings to entity types that are supported via plugins.
25
 */
gbyte.co's avatar
gbyte.co committed
26
function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
27

28
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
gbyte.co's avatar
gbyte.co committed
29
  if (!$f->alteringForm) {
30
    return;
gbyte.co's avatar
gbyte.co committed
31
  }
gbyte.co's avatar
gbyte.co committed
32 33 34 35 36

  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Simple XML sitemap'),
37
    '#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '',
gbyte.co's avatar
gbyte.co committed
38
  ];
39

gbyte.co's avatar
gbyte.co committed
40
  // Attach some js magic to forms.
gbyte.co's avatar
gbyte.co committed
41 42
  // todo: JS not working on comment entity form, hence disabling.
  if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') {
gbyte.co's avatar
gbyte.co committed
43
    $form['#attached']['library'][] = 'simple_sitemap/form';
gbyte.co's avatar
gbyte.co committed
44
  }
45

gbyte.co's avatar
gbyte.co committed
46 47 48
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  if (isset($form['additional_settings'])) {
    $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
49
  }
50

51
  $f->displayEntitySettings($form['simple_sitemap'])
gbyte.co's avatar
gbyte.co committed
52 53
  // todo: do not show setting when creating new bundle.
    ->displayRegenerateNow($form['simple_sitemap']);
54

55
  // Add submission handler.
56
  if (isset($form['actions']['submit']['#submit'])) {
57 58 59 60 61
    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';
      }
    }
62
  }
gbyte.co's avatar
gbyte.co committed
63 64
  // Fix for account page rendering other submit handlers not usable.
  else {
gbyte.co's avatar
gbyte.co committed
65
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
66
  }
gbyte.co's avatar
gbyte.co committed
67 68
}

69
/**
70
 * Form submission handler called in hook_form_alter.
71
 */
gbyte.co's avatar
gbyte.co committed
72
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
73

74
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
75

76 77 78 79
  $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;
80

81
  // Only make changes in DB if sitemap settings actually changed.
82
  if ($f->valuesChanged($form, $values)) {
gbyte.co's avatar
gbyte.co committed
83
    $generator = \Drupal::service('simple_sitemap.generator');
84

85
    switch ($f->entityCategory) {
86 87

      case 'bundle':
gbyte.co's avatar
gbyte.co committed
88
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
gbyte.co's avatar
gbyte.co committed
89
        $generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
90 91
        break;

92
      case 'instance':
gbyte.co's avatar
gbyte.co committed
93
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
gbyte.co's avatar
gbyte.co committed
94
        $generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
95
        break;
96
    }
97 98 99

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
gbyte.co's avatar
gbyte.co committed
100
      $generator->generateSitemap();
101
    }
102 103 104 105 106 107
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
108
function simple_sitemap_cron() {
gbyte.co's avatar
gbyte.co committed
109 110 111
  $generator = \Drupal::service('simple_sitemap.generator');
  if ($generator->getSetting('cron_generate')) {
    $generator->generateSitemap('backend');
112
  }
gbyte.co's avatar
gbyte.co committed
113
}
114

115 116 117 118
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
119 120
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
121
 */
gbyte.co's avatar
gbyte.co committed
122
function simple_sitemap_entity_delete(EntityInterface $entity) {
123 124
  \Drupal::service('simple_sitemap.generator')
    ->removeEntityInstanceSettings($entity->getEntityTypeId(), $entity->id());
125
}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

/**
 * Implements hook_entity_bundle_delete().
 *
 * Removes settings of the removed bundle.
 */
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
  simple_sitemap_delete_bundle_config($entity_type_id, $bundle);
}

/**
 * Implements hook_menu_delete().
 *
 * Removes settings for the removed menu.
 *
 * @param \Drupal\system\MenuInterface $menu
 */
function simple_sitemap_menu_delete(MenuInterface $menu) {
  simple_sitemap_delete_bundle_config('menu_link_content', $menu->id());
}

/**
 * Helper function used by simple_sitemap_entity_bundle_delete() and
 * simple_sitemap_menu_delete() hooks. This is needed, as menus are technically
 * not bundles.
 *
 * @param string $entity_type_id
 * @param string $bundle
 */
function simple_sitemap_delete_bundle_config($entity_type_id, $bundle) {
  $generator = \Drupal::service('simple_sitemap.generator');
  $deleted_bundle_settings = $generator->getBundleSettings($entity_type_id, $bundle);
  if ($deleted_bundle_settings !== FALSE) {

    // Delete bundle settings.
    \Drupal::service('config.factory')->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle")->delete();

    $message = "You may want to <a href='@url'>regenerate</a> your XML sitemap now.";
    if ($generator->getSetting('cron_generate')) {
      $message .= ' Otherwise the sitemap will be regenerated on the next cron run.';
    }
    drupal_set_message(t($message, ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']));
  }
}