pm-4.1.x-dev/src/PmDashboardItemPluginManager.php
src/PmDashboardItemPluginManager.php
<?php
declare(strict_types=1);
namespace Drupal\pm;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
/**
* Defines a plugin manager to deal with pm_dashboard_items.
*
* Modules can define pm_dashboard_items in a MODULE_NAME.pm_dashboard_items.yml file contained
* in the module's base directory. Each pm_dashboard_item has the following structure:
*
* @code
* MACHINE_NAME:
* label: STRING
* description: STRING
* @endcode
*
* @see \Drupal\pm\PmDashboardItemDefault
* @see \Drupal\pm\PmDashboardItemInterface
*/
final class PmDashboardItemPluginManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
protected $defaults = [
// The pm_dashboard_item id. Set by the plugin system based on the top-level YAML key.
'id' => '',
// The pm_dashboard_item label.
'label' => '',
// The pm_dashboard_item description.
'description' => '',
// Default plugin class.
'class' => PmDashboardItemDefault::class,
];
/**
* Constructs PmDashboardItemPluginManager object.
*/
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->factory = new ContainerFactory($this);
$this->moduleHandler = $module_handler;
$this->alterInfo('pm_dashboard_item_info');
$this->setCacheBackend($cache_backend, 'pm_dashboard_item_plugins');
}
/**
* {@inheritdoc}
*/
protected function getDiscovery(): YamlDiscovery {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('pm_dashboard_items', $this->moduleHandler->getModuleDirectories());
$this->discovery->addTranslatableProperty('label', 'label_context');
$this->discovery->addTranslatableProperty('description', 'description_context');
}
return $this->discovery;
}
}
