Commit 1513e386 authored by gbyte.co's avatar gbyte.co

Implementing Drupal 8 plugin support and making sitemap pluggable by other...

Implementing Drupal 8 plugin support and making sitemap pluggable by other modules providing sitemap links.
parent f1ad1349
......@@ -19,7 +19,12 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
return;
$entity_type_id = $form_entity->getEntityTypeId();
if (!Simplesitemap::get_plugin_path($entity_type_id))
//todo: wrap into helper function
// Return if a sitemap plugin of this entity type does not exist.
$manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions();
if (!isset($plugins[$entity_type_id]))
return;
$bundle_name = $form_entity->Id();
......
services:
plugin.manager.simplesitemap:
class: Drupal\simplesitemap\SimplesitemapManager
parent: default_plugin_manager
<?php
/**
* @file
* Contains \Drupal\simplesitemap\Annotation\LinkGenerator.
*/
namespace Drupal\simplesitemap\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a LinkGenerator item annotation object.
*
* @see \Drupal\simplesitemap\Plugin\SimplesitemapManager
* @see plugin_api
*
* @Annotation
*/
class LinkGenerator extends Plugin {
/**
* The plugin ID.
*
* @var string
*/
public $id;
}
......@@ -6,7 +6,7 @@
* Generates custom sitemap links provided by the user.
*/
namespace Drupal\simplesitemap\LinkGenerators;
namespace Drupal\simplesitemap;
use Drupal\Core\Url;
......
<?php
/**
* @file
* Contains \Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator.
*
* Abstract class to be extended for plugin creation.
* See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node for more
* documentation.
* Contains \Drupal\simplesitemap\LinkGeneratorBase.
*/
namespace Drupal\simplesitemap\LinkGenerators;
namespace Drupal\simplesitemap;
/**
* EntityLinkGenerator class.
* @abstract
*/
abstract class EntityLinkGenerator {
use Drupal\Component\Plugin\PluginBase;
abstract class LinkGeneratorBase extends PluginBase implements LinkGeneratorInterface {
private $entity_links = array();
/**
* {@inheritdoc}
*/
public function get_entity_links($entity_type, $bundles, $languages) {
$i = 0;
foreach($bundles as $bundle => $bundle_settings) {
......
<?php
/**
* @file
* Provides Drupal\simplesitemap\LinkGenerator.
*/
namespace Drupal\simplesitemap;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines an interface for simplesitemap plugins.
*/
interface LinkGeneratorInterface extends PluginInspectionInterface {
/**
*
*
* @return array
*/
public function get_entity_links($entity_type, $bundles, $languages);
}
<?php
/**
* @file
* Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\menu.
* Contains \Drupal\simplesitemap\LinkGenerator\Menu.
*
* Plugin for menu entity link generation.
* See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node_type for more
* documentation.
*/
namespace Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators;
namespace Drupal\simplesitemap\Plugin\LinkGenerator;
use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator;
use Drupal\simplesitemap\Annotation\LinkGenerator;
use Drupal\simplesitemap\LinkGeneratorBase;
use Drupal\Core\Url;
/**
* menu class.
* Menu class.
*
* @LinkGenerator(
* id = "menu"
* )
*/
class menu extends EntityLinkGenerator {
class Menu extends LinkGeneratorBase {
/**
* {@inheritdoc}
......
<?php
/**
* @file
* Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\node_type.
* Contains \Drupal\simplesitemap\Plugin\LinkGenerator\NodeType.
*
* Plugin for node entity link generation.
*
* This can be used as a template to create new plugins for other configuration
* entity types. To create a plugin simply create a new class file in the
* EntityTypeLinkGenerators folder. Name this file after the configuration
* entity type id (eg. 'node_type' or 'taxonomy_vocabulary'.)
* This class needs to extend the EntityLinkGenerator class and include
* the get_entity_bundle_links() method. - as shown here. This method has to
* return an array of pure urls to the entities of the configuration entity type
* in question.
* Plugin for taxonomy term entity link generation.
*/
namespace Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators;
namespace Drupal\simplesitemap\Plugin\LinkGenerator;
use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator;
use Drupal\simplesitemap\Annotation\LinkGenerator;
use Drupal\simplesitemap\LinkGeneratorBase;
use Drupal\Core\Url;
/**
* node_type class.
* NodeType class.
*
* @LinkGenerator(
* id = "node_type"
* )
*/
class node_type extends EntityLinkGenerator {
class NodeType extends LinkGeneratorBase {
/**
* {@inheritdoc}
......
<?php
/**
* @file
* Contains \Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators\taxonomy_vocabulary.
* Contains \Drupal\simplesitemap\LinkGenerator\TaxonomyVocabulary.
*
* Plugin for taxonomy term entity link generation.
* See \Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator\node_type for more
* documentation.
*/
namespace Drupal\simplesitemap\LinkGenerators\EntityTypeLinkGenerators;
namespace Drupal\simplesitemap\Plugin\LinkGenerator;
use Drupal\simplesitemap\LinkGenerators\EntityLinkGenerator;
use Drupal\simplesitemap\Annotation\LinkGenerator;
use Drupal\simplesitemap\LinkGeneratorBase;
use Drupal\Core\Url;
/**
* taxonomy_vocabulary class.
* TaxonomyVocabulary class.
*
* @LinkGenerator(
* id = "taxonomy_vocabulary"
* )
*/
class taxonomy_vocabulary extends EntityLinkGenerator {
class TaxonomyVocabulary extends LinkGeneratorBase {
/**
* {@inheritdoc}
......
......@@ -13,8 +13,6 @@ namespace Drupal\simplesitemap;
*/
class Simplesitemap {
const SITEMAP_PLUGIN_PATH = 'src/LinkGenerators/EntityTypeLinkGenerators';
private $config;
private $sitemap;
......@@ -37,21 +35,6 @@ class Simplesitemap {
return FALSE;
}
/**
* Returns path of a sitemap plugin.
*
* @param string $entity_type_name
* @return string $class_path or FALSE if non-existent.
*/
public static function get_plugin_path($entity_type_name) {
$class_path = drupal_get_path('module', 'simplesitemap')
. '/' . self::SITEMAP_PLUGIN_PATH . '/' . $entity_type_name . '.php';
if (file_exists($class_path)) {
return $class_path;
}
return FALSE;
}
private function initialize() {
$this->get_config_from_db();
$this->get_sitemap_from_db();
......
<?php
/**
* @file
* Contains SimplesitemapManager.
*/
namespace Drupal\simplesitemap;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Simplesitemap plugin manager.
*/
class SimplesitemapManager extends DefaultPluginManager {
/**
* Constructs an SimplesitemapManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/LinkGenerator', $namespaces, $module_handler, 'Drupal\simplesitemap\LinkGeneratorInterface', 'Drupal\simplesitemap\Annotation\LinkGenerator');
$this->alterInfo('simplesitemap_link_generators_info');
$this->setCacheBackend($cache_backend, 'simplesitemap_flavors');
}
}
......@@ -8,7 +8,6 @@
namespace Drupal\simplesitemap;
use Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator;
use \XMLWriter;
/**
......@@ -181,13 +180,16 @@ class SitemapGenerator {
* Gets entity type links.
*/
private function generate_entity_links() {
foreach($this->entity_types as $entity_type => $bundles) {
$class_path = Simplesitemap::get_plugin_path($entity_type);
if ($class_path !== FALSE) {
require_once $class_path;
$class_name = "Drupal\\simplesitemap\\LinkGenerators\\EntityTypeLinkGenerators\\$entity_type";
$link_generator = new $class_name();
$links = $link_generator->get_entity_links($entity_type, $bundles, $this->languages);
$manager = \Drupal::service('plugin.manager.simplesitemap');
$plugins = $manager->getDefinitions();
foreach ($plugins as $link_generator_plugin) {
if (isset($this->entity_types[$link_generator_plugin['id']])) {
$instance = $manager->createInstance($link_generator_plugin['id']);
$links = $instance->get_entity_links($link_generator_plugin['id'],
$this->entity_types[$link_generator_plugin['id']],
$this->languages);
$this->links = array_merge($this->links, $links);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment