simplesitemap.module 5.37 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\simplesitemap\Simplesitemap;
8
use Drupal\simplesitemap\SitemapGenerator;
9 10 11

/**
 * Implements hook_form_alter.
12 13
 *
 * Adds sitemap settings to entity types that are supported via plugins.
14
 */
15
function simplesitemap_form_alter(&$form, $form_state, $form_id) {
16

17
  // Get all simplesitemap plugins.
18 19
  $manager = \Drupal::service('plugin.manager.simplesitemap');
  $plugins = $manager->getDefinitions();
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  // Go through simplesitemap plugins and check if one of them declares usage
  // of this particular form. If that's the case, get entity type id of the
  // plugin definition and assume the bundle to be of the same name as the
  // entity type id.
  foreach($plugins as $plugin) {
    if (isset($plugin['form_id']) && $plugin['form_id'] === $form_id) {
      $entity_type_id = $plugin['id'];
      $bundle_name = $entity_type_id;
    }
  }

  // Else get entity type id and bundle name from the form if available and only
  // if a simplesitemap plugin of the same entity type exists.
  if (empty($entity_type_id)) {
    $form_entity = Simplesitemap::get_form_entity($form_state);
    if ($form_entity !== FALSE) {
      $form_entity_type_id = $form_entity->getEntityTypeId();
      if (isset($plugins[$form_entity_type_id])) {
        if (!isset($plugins[$form_entity_type_id]['form_id'])
          || $plugins[$form_entity_type_id]['form_id'] === $form_id) {
          $entity_type_id = $form_entity_type_id;
          $bundle_name = $form_entity->Id();
        }
      }
    }
  }

  // If both methods of getting simplesitemap configuration for this form
  // failed, return.
  if (empty($entity_type_id))
    return;
Pawel G's avatar
Pawel G committed
52

53
  $sitemap = new Simplesitemap;
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  // Get current entity type sitemap settings.
  $entity_types = $sitemap->get_entity_types();

  $form['simplesitemap'] = array(
    '#group' => 'additional_settings',
    '#title' => t('Simple XML sitemap'),
    '#type' => 'details'
  );

  $index_content_checked = isset($entity_types[$entity_type_id][$bundle_name]['index']) ? $entity_types[$entity_type_id][$bundle_name]['index'] : FALSE;
  $form['simplesitemap']['simplesitemap_index_content'] = array(
    '#type' => 'checkbox',
    '#title' => t('Index content of this type'),
    '#default_value' => $index_content_checked,
  );

  $priority = isset($entity_types[$entity_type_id][$bundle_name]['priority']) ? $entity_types[$entity_type_id][$bundle_name]['priority'] : SitemapGenerator::PRIORITY_DEFAULT;
  $form['simplesitemap']['simplesitemap_priority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#description' => t('The priority entities of this bundle will have in the eyes of search engine bots.'),
    '#options' => SitemapGenerator::get_priority_select_values(),
    '#default_value' => $priority,
  );

  $form['simplesitemap']['simplesitemap_rebuild_now'] = array(
    '#type' => 'checkbox',
    '#title' => t('Rebuild sitemap after hitting Save'),
    '#description' => t('This setting will rebuild the whole sitemap including the above changes.<br/>Otherwise the sitemap will be rebuilt on next cron run.'),
    '#default_value' => FALSE,
  );

87 88 89
  $form['#simplesitemap']['entity_type_id'] = $entity_type_id;
  $form['#simplesitemap']['bundle_name'] = $bundle_name;

90 91
  // Add submission handler.
  $form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
Pawel G's avatar
Pawel G committed
92 93 94

}

95
/**
96
 * Form submission handler called in hook_form_alter.
97
 */
98
function simplesitemap_entity_form_submit($form, &$form_state) {
99

100
  $sitemap = new Simplesitemap;
101 102
  $values = $form_state->getValues();

103
  // Only make changes in DB if sitemap settings actually changed.
104 105
  if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value']
    || $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) {
106

107 108
    $entity_type_id = $form['#simplesitemap']['entity_type_id'];
    $bundle_name = $form['#simplesitemap']['bundle_name'];
109 110 111

    // Get current entity type sitemap settings.
    $entity_types = $sitemap->get_entity_types();
112

113 114
    $entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
    $entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
115

116 117
    // Save new entity type settings.
    $sitemap->save_entity_types($entity_types);
118
  }
119

120
  // Regenerate sitemaps according to user setting.
121
  if ($values['simplesitemap_rebuild_now']) {
122
    $sitemap->generate_sitemap();
123 124 125 126 127 128 129 130
  }
}

/**
 * Implements hook_cron.
 */
function simplesitemap_cron() {

131
  // Regenerate sitemap for all languages.
132
  $sitemap = new Simplesitemap;
133
  $sitemap->generate_sitemap();
Pawel G's avatar
Pawel G committed
134
}
135

136 137
/**
 * Implements hook_entity_bundle_delete().
138 139
 *
 * Removes settings of the removed bundle.
140 141 142 143 144 145 146 147 148
 */
function simplesitemap_entity_bundle_delete($entity_type_id, $bundle) {
  $sitemap = new Simplesitemap;
  $entity_types = $sitemap->get_entity_types();
  $bundle_entity_type_id = \Drupal::entityManager()->getDefinition($entity_type_id)->getBundleEntityType();

  if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
    unset($entity_types[$bundle_entity_type_id][$bundle]);
    $sitemap->save_entity_types($entity_types);
149 150 151
    drupal_set_message(t("You may want to <a href='@url'>rebuild</a> your XML sitemap now.
    Otherwise it will be rebuilt on the next cron run.",
      array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
152 153
  }
}