Creating Sitemap plugins (8.x-2.x only)
The individual bits of Sitemap content (front page, menus, etc) are defined using annotation-based plugins. The base class for these plugins is SitemapBase (/src/SitemapBase.php) which extends SitemapInterface (/src/SitemapInteface.php).
For examples, see SitemapFrontpage for a single-instance plugin, and SitemapMenus for derived plugins.
Define your plugin
Here are the steps to create a Sitemap plugin named SitemapRabbits:
Step 1: Create the file
Create a new file and save it in mymodule/src/Plugin/Sitemap/SitemapRabbits.php:
<?php
namespace Drupal\mymodule\Plugin\Sitemap;
use Drupal\sitemap\SitemapBase;
Step 2: Add the annotation and define the plugin class
/**
* A description of my Rabbits plugin.
*
* @Sitemap(
* id = "rabbits",
* title = @Translation("Rabbits"),
* description = @Translation("Displays a list of rabbits."),
* settings = {
* "title" = "Rabbits",
* },
* enabled = TRUE,
* )
*/
class SitemapRabbits extends SitemapBase {
/** class definition code here **/
}
Detailed documentation on the annotation options can be found in /src/Annotation/Sitemap.php. The super-short version: id, title, and description are required, everything else is optional and plugins are disabled by default.
Step 3: Add a view() method
This method is required and must return a renderable array. This can be as simple as:
/**
* {@inheritdoc}
*/
public function view() {
return ['#markup' => 'I like rabbits'];
}
but in practice will likely be closer to:
/**
* {@inheritdoc}
*/
public function view() {
/** your code that generates $content as a renderable array **/
$title = $this->settings['title'];
$attributes = new Attribute();
$attributes->addClass('sitemap-rabbits');
return [
'#theme' => 'sitemap_map',
'#title' => $title,
'#content' => $content,
'#attributes' => $attributes,
];
}
For more info:
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion