You are here

sitemap.module in Sitemap 2.0.x

Same filename and directory in other branches
  1. 8.2 sitemap.module
  2. 8 sitemap.module

Provides sitemap functionality.

File

sitemap.module
View source
<?php

use Drupal\taxonomy\VocabularyInterface;
use Drupal\system\MenuInterface;
use Drupal\node\NodeInterface;

/**
 * @file
 * Provides sitemap functionality.
 */
use Drupal\Component\Utility\Html;

/**
 * Implements hook_theme().
 */
function sitemap_theme() {
  return [
    'sitemap' => [
      'variables' => [
        'message' => NULL,
        'sitemap_items' => [],
      ],
    ],
    'sitemap_item' => [
      'variables' => [
        'title' => '',
        'content' => [],
        'sitemap' => '',
      ],
      'file' => 'sitemap.theme.inc',
    ],
    'sitemap_taxonomy_term' => [
      'variables' => [
        'name' => '',
        'show_link' => FALSE,
        'url' => '',
        'show_count' => FALSE,
        'count' => '',
        'show_feed' => FALSE,
        'feed' => '',
        'feed_icon' => '',
      ],
      'file' => 'sitemap.theme.inc',
    ],
    'sitemap_frontpage_item' => [
      'variables' => [
        'text' => '',
        'url' => '',
        'feed' => '',
        'feed_icon' => '',
      ],
      'file' => 'sitemap.theme.inc',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_sitemap_item
 */
function sitemap_theme_suggestions_sitemap_item(array $variables) {
  $suggestions = array();
  if (isset($variables['sitemap'])) {
    if ($id = $variables['sitemap']
      ->getPluginDefinition()['id']) {
      $filter = [
        ' ' => '_',
        '-' => '_',
        '/' => '_',
        '[' => '_',
        ']' => '_',
        ':' => '_',
      ];
      $type = Html::cleanCssIdentifier($id, $filter);
      $suggestions[] = 'sitemap_item__' . $type;
      $suggestions[] = 'sitemap_item__' . $type . '__' . Html::cleanCssIdentifier($variables['sitemap']
        ->getPluginId(), $filter);
    }
  }
  return $suggestions;
}

/**
 * Count the number of published nodes classified by a term.
 *
 * This is a re-implementation of taxonomy_term_count_nodes() that has been
 * removed from D7 core.
 *
 * Implementation note: the normal way to count field instances is through
 * field_attach_query(), but taxonomy.module has a special denormalized
 * table taxonomy_index which we can use for more speed. THX to taxonews.
 *
 * @param string $tid
 *   The term's ID.
 *
 * @return string
 *   An integer representing a number of nodes. Results are statically cached.
 */
function sitemap_taxonomy_term_count_nodes($tid) {
  $query = \Drupal::database()
    ->select('taxonomy_index', 'ti');
  $query
    ->addExpression('COUNT(ti.nid)');
  $count = $query
    ->condition('ti.tid', $tid)
    ->execute()
    ->fetchCol();
  return $count[0];
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function sitemap_taxonomy_vocabulary_insert(VocabularyInterface $vocabulary) {
  _sitemap_clear_plugin_cache();
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function sitemap_taxonomy_vocabulary_delete(VocabularyInterface $vocabulary) {
  _sitemap_clear_plugin_cache();
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function sitemap_menu_delete(MenuInterface $menu) {

  // @TODO

  //_sitemap_clear_plugin_cache();
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function sitemap_node_delete(NodeInterface $node) {
  if (\Drupal::moduleHandler()
    ->moduleExists('book')) {

    // @TODO: Can we tell if the deleted node was a parent book node?
    _sitemap_clear_plugin_cache();
  }
}

/**
 * Clears the sitemap plugin cache.
 */
function _sitemap_clear_plugin_cache() {
  \Drupal::service('plugin.manager.sitemap')
    ->clearCachedDefinitions();
}

Functions

Namesort descending Description
sitemap_menu_delete Implements hook_ENTITY_TYPE_delete().
sitemap_node_delete Implements hook_ENTITY_TYPE_delete().
sitemap_taxonomy_term_count_nodes Count the number of published nodes classified by a term.
sitemap_taxonomy_vocabulary_delete Implements hook_ENTITY_TYPE_delete().
sitemap_taxonomy_vocabulary_insert Implements hook_ENTITY_TYPE_insert().
sitemap_theme Implements hook_theme().
sitemap_theme_suggestions_sitemap_item Implements hook_theme_suggestions_sitemap_item
_sitemap_clear_plugin_cache Clears the sitemap plugin cache.