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

7 8 9 10 11 12 13 14
/**
 * 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;
}

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

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

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

54
/**
55
 * Form submission handler called in hook_form_alter.
56
 */
gbyte.co's avatar
gbyte.co committed
57
function simple_sitemap_entity_form_submit($form, &$form_state) {
58

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

61 62 63 64
  $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;
65

66
  // Only make changes in DB if sitemap settings actually changed.
67
  if ($f->valuesChanged($form, $values)) {
68
    $generator = \Drupal::service('simple_sitemap.generator');
69

70
    switch ($f->entityCategory) {
71 72

      case 'bundle':
73
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
74
        $generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
75 76
        break;

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

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
85
      $generator->generateSitemap();
86
    }
87 88 89 90 91 92
  }
}

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

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

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