You are here

function xmlsitemap_get_link_info in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap.module \xmlsitemap_get_link_info()
  2. 6.2 xmlsitemap.module \xmlsitemap_get_link_info()
  3. 7.2 xmlsitemap.module \xmlsitemap_get_link_info()

Returns information about supported sitemap link types.

Parameters

mixed $type: (optional) The link type to return information for. If omitted, information for all link types is returned.

mixed $reset: (optional) Boolean whether to reset the static cache and do nothing. Only used for tests.

Return value

array Info about sitemap link.

See also

hook_xmlsitemap_link_info()

hook_xmlsitemap_link_info_alter()

11 calls to xmlsitemap_get_link_info()
XmlSitemapGenerator::rebuildBatchFetch in src/XmlSitemapGenerator.php
Batch callback; fetch and add the sitemap links for a specific entity.
XmlSitemapSettingsForm::buildForm in src/Form/XmlSitemapSettingsForm.php
Form constructor.
xmlsitemap_add_link_bundle_settings in ./xmlsitemap.module
Add the link type XML sitemap options to the link type's form.
xmlsitemap_get_link_type_enabled_bundles in ./xmlsitemap.module
Returns enabled bundles of an entity type.
xmlsitemap_get_link_type_indexed_status in ./xmlsitemap.module
Returns statistics about specific entity links.

... See full list

File

./xmlsitemap.module, line 706
xmlsitemap XML sitemap

Code

function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $link_info =& drupal_static(__FUNCTION__);
  if ($reset) {
    $link_info = NULL;
    \Drupal::service('cache_tags.invalidator')
      ->invalidateTags([
      'xmlsitemap',
    ]);
  }
  if (!isset($link_info)) {
    $cid = 'xmlsitemap:link_info:' . $language
      ->getId();
    if ($cache = \Drupal::cache()
      ->get($cid)) {
      $link_info = $cache->data;
    }
    else {
      $link_info = [];
      $entity_types = \Drupal::entityTypeManager()
        ->getDefinitions();
      foreach ($entity_types as $key => $entity_type) {
        if (!xmlsitemap_is_entity_type_supported($entity_type)) {
          continue;
        }
        $link_info[$key] = [
          'label' => $entity_type
            ->getLabel(),
          'type' => $entity_type
            ->id(),
          'base table' => $entity_type
            ->getBaseTable(),
          'bundles' => \Drupal::service('entity_type.bundle.info')
            ->getBundleInfo($entity_type
            ->id()),
          'bundle label' => $entity_type
            ->getBundleLabel(),
          'entity keys' => [
            'id' => $entity_type
              ->getKey('id'),
            'bundle' => $entity_type
              ->getKey('bundle'),
          ],
          'xmlsitemap' => [
            // Add in the default callbacks for entity types.
            'process callback' => $entity_type
              ->get('xmlsitemap')['process callback'] ?? 'xmlsitemap_xmlsitemap_process_entity_links',
            'rebuild callback' => $entity_type
              ->get('xmlsitemap')['process callback'] ?? 'xmlsitemap_rebuild_batch_fetch',
          ],
        ];
      }
      $link_info = array_merge($link_info, \Drupal::moduleHandler()
        ->invokeAll('xmlsitemap_link_info'));
      foreach ($link_info as $key => &$info) {
        $info += [
          'type' => $key,
          'base table' => FALSE,
          'bundles' => [],
        ];
        foreach ($info['bundles'] as $bundle => &$bundle_info) {
          $bundle_info += [
            'xmlsitemap' => [],
          ];
          $bundle_info['xmlsitemap'] += xmlsitemap_link_bundle_load($key, $bundle, FALSE);
        }
      }
      \Drupal::moduleHandler()
        ->alter('xmlsitemap_link_info', $link_info);

      // Sort the entity types by label.
      uasort($link_info, function ($a, $b) {

        // Put frontpage first.
        if ($a['type'] === 'frontpage') {
          return -1;
        }
        if ($b['type'] === 'frontpage') {
          return 1;
        }
        return strnatcmp($a['label'], $b['label']);
      });

      // Cache by language since this info contains translated strings.
      // Also include entity type tags since this is tied to entity and bundle
      // information.
      \Drupal::cache()
        ->set($cid, $link_info, Cache::PERMANENT, [
        'xmlsitemap',
        'entity_types',
        'entity_bundles',
      ]);
    }
  }
  if (isset($type)) {
    return isset($link_info[$type]) ? $link_info[$type] : NULL;
  }
  return $link_info;
}