BreadcrumbTitleResolverManager.php in Breadcrumb Manager 8
File
src/Plugin/BreadcrumbTitleResolverManager.php
View source
<?php
namespace Drupal\breadcrumb_manager\Plugin;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class BreadcrumbTitleResolverManager extends DefaultPluginManager {
protected $config;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
parent::__construct('Plugin/BreadcrumbTitleResolver', $namespaces, $module_handler, 'Drupal\\breadcrumb_manager\\Plugin\\BreadcrumbTitleResolverInterface', 'Drupal\\breadcrumb_manager\\Annotation\\BreadcrumbTitleResolver');
$this->config = $config_factory
->get('breadcrumb_manager.config');
$this
->alterInfo('breadcrumb_manager_breadcrumb_title_resolver_info');
$this
->setCacheBackend($cache_backend, 'breadcrumb_manager_breadcrumb_title_resolver_plugins');
}
public function getInstances() {
$instances = [];
foreach ($this
->getDefinitions() as $pluginId => $definition) {
try {
$instance = $this
->createInstance($pluginId);
$instance
->setActive($definition['enabled']);
$instances[] = $instance;
} catch (PluginException $e) {
\Drupal::messenger()
->addError($e
->getMessage());
}
}
return $instances;
}
public function getDefinitions() {
$definitions = parent::getDefinitions();
$resolvers = $this->config
->get('title_resolvers');
foreach ($definitions as $id => $definition) {
if (isset($resolvers[$id]['weight'])) {
$definitions[$id]['weight'] = $resolvers[$id]['weight'];
}
if (isset($resolvers[$id]['enabled'])) {
$definitions[$id]['enabled'] = $resolvers[$id]['enabled'];
}
}
uasort($definitions, [
$this,
'sortByWeight',
]);
return $definitions;
}
public function sortByWeight(array $a, array $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return $a['weight'] < $b['weight'] ? -1 : 1;
}
}