SettingPluginManager.php in Fasttoggle 8.2
Contains \Drupal\fasttoggle\FasttoggleObjectPluginManager.
Namespace
Drupal\fasttoggleFile
src/SettingPluginManager.phpView source
<?php
/**
* @file
* Contains \Drupal\fasttoggle\FasttoggleObjectPluginManager.
*/
namespace Drupal\fasttoggle;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Manages Fasttoggle Setting plugins.
*
* The SettingsPluginManager class extends the DefaultPluginManager
* to provide a way to manage Fastoggle's Settings plugins.
*
*/
class SettingPluginManager extends DefaultPluginManager {
/**
* Creates the discovery 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) {
// We replace the $subdir parameter with our own value.
// This tells the plugin system to look for plugins in the 'Plugin/Setting'
// subfolder inside modules' 'src' folder.
$subdir = 'Plugin/Setting';
// The name of the interface that plugins should adhere to. Drupal will
// enforce this as a requirement. If a plugin does not implement this
// interface, than Drupal will throw an error.
$plugin_interface = 'Drupal\\fasttoggle\\Plugin\\Setting\\SettingInterface';
// The name of the annotation class that contains the plugin definition.
$plugin_definition_annotation_name = 'Drupal\\Component\\Annotation\\Plugin';
parent::__construct($subdir, $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
// This allows the plugin definitions to be altered by an alter hook. The
// parameter defines the name of the hook, thus: hook_sandwich_info_alter().
// In this example, we implement this hook to change the plugin definitions:
// see plugin_type_example_sandwich_info_alter().
$this
->alterInfo('fasttoggle_setting_info');
// This sets the caching method for our plugin definitions. Plugin
// definitions are cached using the provided cache backend. For our
// Sandwich plugin type, we've specified the @cache.default service be used
// in the plugin_type_example.services.yml file. The second argument is a
// cache key prefix. Out of the box Drupal with the default cache backend
// setup will store our plugin definition in the cache_default table using
// the sandwich_info key. All that is implementation details however,
// all we care about it that caching for our plugin definition is taken
// care of by this call.
$this
->setCacheBackend($cache_backend, 'fasttoggle_setting_info');
}
/**
* {@inheritdoc}
*/
public function getDefinitions() {
$definitions = parent::getDefinitions();
$result = [];
foreach ($definitions as $setting => $definition) {
$entityType = $definition['entityType'];
$group = $definition['group'];
if (!isset($result[$entityType])) {
$result[$entityType] = [];
}
if (!isset($result[$entityType][$group])) {
$result[$entityType][$group] = [];
}
$result[$entityType][$group][$setting] = $definition;
}
return $result;
}
/**
* Find a plugin that matches a field definition.
*
* @param $seeking
* The field definition to use.
*
* @return \Drupal\fasttoggle\SettingPlugin or NULL
* The matching plugin.
*/
public function match($seeking) {
$definitions = parent::getDefinitions();
foreach ($definitions as $setting => $definition) {
$plugin = $this
->createInstance($setting);
if ($plugin
->matches($seeking)) {
return $plugin;
}
}
return NULL;
}
}
Classes
Name | Description |
---|---|
SettingPluginManager | Manages Fasttoggle Setting plugins. |