simple_sitemap.install 4.16 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/**
 * @file
 * Module install and update procedures.
 */

/**
 * Implements hook_requirements().
 */
function simple_sitemap_requirements($phase) {
  $requirements = array();

  if (!extension_loaded('xmlwriter')) {
    $requirements['simple_sitemap_php_extensions'] = array(
      'title' => t('Simple XML sitemap PHP extensions'),
      'value' => t('Missing PHP xmlwriter extension'),
      'description' => t("In order to be able to generate sitemaps, the Simple XML sitemap module requires the <em>xmlwriter</em> PHP extension to be enabled."),
      'severity' => REQUIREMENT_ERROR,
    );
  }

  switch($phase) {
    case 'runtime':
24
      $sitemap = \Drupal::service('simple_sitemap.generator');
25
      $generated_ago = $sitemap->getGeneratedAgo();
gbyte.co's avatar
gbyte.co committed
26 27
      if (!$generated_ago) {
        $value = t('Not available');
gbyte.co's avatar
gbyte.co committed
28
        $description = t("The sitemap has not been generated yet. Run cron, or <a href='@generate'>generate</a> the sitemap manually.", array('@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
gbyte.co's avatar
gbyte.co committed
29 30 31 32
        $severity = REQUIREMENT_WARNING;
      }
      else {
        $value = t('XML sitemap is available');
gbyte.co's avatar
gbyte.co committed
33
        $description = t("The <a href='@sitemap'>XML sitemap</a> was generated @ago ago. <a href='@generate'>Regenerate</a>.", array('@ago' => $generated_ago, '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml', '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
gbyte.co's avatar
gbyte.co committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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
        $severity = REQUIREMENT_INFO;
      }
      $requirements['simple_sitemap_generated'] = array(
        'title' => 'Simple XML sitemap',
        'value' => $value,
        'description' => $description,
        'severity' => $severity,
      );
      break;
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function simple_sitemap_schema() {
  $schema['simple_sitemap'] = array(
    'description' => 'Holds XML sitemaps as strings for quick retrieval.',
    'fields' => array(
      'id' => array(
        'description' => 'Sitemap chunk unique identifier.',
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
      ),
      'sitemap_string' => array(
        'description' => 'XML sitemap chunk string.',
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
      'sitemap_created' => array(
        'description' => 'Timestamp of sitemap chunk generation.',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('id'),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function simple_sitemap_install() {
82
  $sitemap = \Drupal::service('simple_sitemap.generator');
83
  $sitemap->generateSitemap('nobatch');
gbyte.co's avatar
gbyte.co committed
84
  $base_url = $GLOBALS['base_url'];
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>node type edit pages</a>) These settings can then be overridden for single entities.<br/> Support for additional entity types and custom links can be added on <a href='@config_url' target='_blank'>the module's configuration pages</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@config_url' => "$base_url/admin/config/search/simplesitemap")));
}

/**
 * Changing the data structure of the module's configuration.
 */
function simple_sitemap_update_8201() {
  $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
  $entity_types = is_array($entity_types) ? $entity_types : array();
  $naming_changes = [
    'node_type' => 'node',
    'taxonomy_vocabulary' => 'taxonomy_term',
    'menu' => 'menu_link_content',
    'commerce_product_type' => 'commerce_product',
    'media_bundle' => 'media',
  ];
  foreach($entity_types as $entity_type_name => $settings) {
    if (isset($naming_changes[$entity_type_name])) {
      $entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
      unset($entity_types[$entity_type_name]);
    }
  }
  \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
    ->set('entity_types', $entity_types)->save();
gbyte.co's avatar
gbyte.co committed
109
}