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

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * @file
 * Hooks provided by the Simple XML sitemap module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Alter the generated link data before the sitemap is saved.
 * This hook gets invoked for every sitemap chunk generated.
 *
 * @param array &$links
18 19 20 21 22
 *   Array containing multilingual links generated for each path to be indexed
 *
 * @param string|null $sitemap_variant
 *
 * @todo Make work for sitemap types.
23
 */
24
function hook_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
25 26

  // Remove German URL for a certain path in the hreflang sitemap.
27
  foreach ($links as $key => $link) {
28
    if ($link['path'] === 'node/1') {
gbyte.co's avatar
gbyte.co committed
29

30
      // Remove 'loc' URL if it points to a german site.
31
      if ($link['langcode'] === 'de') {
32
        unset($links[$key]);
33
      }
gbyte.co's avatar
gbyte.co committed
34

35 36 37
      // If this 'loc' URL points to a non-german site, make sure to remove
      // its german alternate URL.
      else {
38 39 40
        if ($link['alternate_urls']['de']) {
          unset($links[$key]['alternate_urls']['de']);
        }
41 42
      }
    }
43 44 45
  }
}

46
/**
gbyte.co's avatar
gbyte.co committed
47
 * Add arbitrary links to the sitemap.
48 49
 *
 * @param array &$arbitrary_links
50
 * @param string|null $sitemap_variant
51
 */
52
function hook_simple_sitemap_arbitrary_links_alter(array &$arbitrary_links, $sitemap_variant) {
53

54
  // Add an arbitrary link to all sitemap variants.
55
  $arbitrary_links[] = [
56
    'url' => 'http://some-arbitrary-link/',
57
    'priority' => '0.5',
gbyte.co's avatar
gbyte.co committed
58 59

    // An ISO8601 formatted date.
60
    'lastmod' => '2012-10-12T17:40:30+02:00',
gbyte.co's avatar
gbyte.co committed
61

62
    'changefreq' => 'weekly',
gbyte.co's avatar
gbyte.co committed
63
    'images' => [
gbyte.co's avatar
gbyte.co committed
64
      ['path' => 'http://path-to-image.png']
gbyte.co's avatar
gbyte.co committed
65 66 67 68 69 70 71
    ],

    // Add alternate URLs for every language of a multilingual site.
    // Not necessary for monolingual sites.
    'alternate_urls' => [
      'en' => 'http://this-is-your-life.net/de/tyler',
      'de' => 'http://this-is-your-life.net/en/tyler',
gbyte.co's avatar
gbyte.co committed
72
    ]
73
  ];
74 75 76 77 78 79 80 81 82

  // Add an arbitrary link to the 'fight_club' sitemap variant only.
  switch ($sitemap_variant) {
    case 'fight_club':
      $arbitrary_links[] = [
        'url' => 'http://this-is-your-life.net/tyler',
      ];
      break;
  }
83 84 85 86 87 88 89
}

/**
 * Alters the sitemap attributes shortly before XML document generation.
 * Attributes can be added, changed and removed.
 *
 * @param array &$attributes
90
 * @param string|null $sitemap_variant
91
 */
92
function hook_simple_sitemap_attributes_alter(array &$attributes, $sitemap_variant) {
93 94 95 96 97 98

  // Remove the xhtml attribute e.g. if no xhtml sitemap elements are present.
  unset($attributes['xmlns:xhtml']);
}

/**
gbyte.co's avatar
gbyte.co committed
99
 * Alters attributes of the sitemap index shortly before XML document generation.
100 101 102
 * Attributes can be added, changed and removed.
 *
 * @param array &$index_attributes
103
 * @param string|null $sitemap_variant
104
 */
105
function hook_simple_sitemap_index_attributes_alter(array &$index_attributes, $sitemap_variant) {
106 107 108 109 110

  // Add some attribute to the sitemap index.
  $index_attributes['name'] = 'value';
}

gbyte.co's avatar
gbyte.co committed
111 112 113
/**
 * Alter properties of and remove generator plugins.
 *
114
 * @param array $url_generators
gbyte.co's avatar
gbyte.co committed
115
 */
116
function hook_simple_sitemap_url_generators_alter(array &$url_generators) {
gbyte.co's avatar
gbyte.co committed
117

gbyte.co's avatar
gbyte.co committed
118
  // Remove the entity generator.
119 120 121 122 123 124 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
  unset($url_generators['entity']);
}

/**
 * Alter properties of and remove generator plugins.
 *
 * @param array $sitemap_generators
 */
function hook_simple_sitemap_sitemap_generators_alter(array &$sitemap_generators) {

  // Remove the default generator.
  unset($sitemap_generators['default']);
}

/**
 * @param array $sitemap_types
 */
function hook_simple_sitemap_types_alter(array &$sitemap_types) {

  // Remove the custom links generator from the default sitemap type definition.
  $key = array_search('custom', $sitemap_types['default_hreflang']['url_generators']);
  unset($sitemap_types['default_hreflang']['url_generators'][$key]);

  // Define a new sitemap type to be generated with the default sitemap generator.
  // Make it use only the custom and arbitrary link generators.
  $sitemap_types['fight_club_sitemap_type'] = [
    'label' => t('Fight Club Sitemap'),
    'description' => t('The second rule of Fight Club is...'),
    'sitemap_generator' => 'default',
    'url_generators' => [
      'custom',
      'arbitrary',
    ],
  ];
}

/**
 * @param array $variants
 */
function hook_simple_sitemap_variants_alter(array &$variants) {

  // Add a new sitemap variant of the 'fight_club_sitemap_type' type.
  $variants['fight_club'] = [
    'type' => 'fight_club_sitemap_type',
    'label' => t('Fight Club'),
  ];

}

/**
169 170 171
 * @param array $bundle_settings
 * @param array $bundle_context
 * @param string|null $sitemap_variant
172
 */
173
function hook_simple_sitemap_bundle_settings_alter(array &$bundle_settings, $bundle_context, $sitemap_variant) {
gbyte.co's avatar
gbyte.co committed
174 175 176

}

177 178 179
/**
 * @} End of "addtogroup hooks".
 */