View source
<?php
namespace Drupal\charts;
use Drupal\charts\Event\ChartsEvents;
use Drupal\charts\Event\TypesInfoEvent;
use Drupal\charts\Plugin\chart\Library\ChartInterface;
use Drupal\charts\Plugin\chart\Type\Type;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class TypeManager extends DefaultPluginManager {
protected $defaults = [
'id' => '',
'label' => '',
'axis' => ChartInterface::DUAL_AXIS,
'axis_inverted' => FALSE,
'stacking' => FALSE,
'class' => Type::class,
];
protected $eventDispatcher;
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, EventDispatcherInterface $event_dispatcher) {
$this->moduleHandler = $module_handler;
$this->eventDispatcher = $event_dispatcher;
$this
->setCacheBackend($cache_backend, 'charts_type', [
'charts_type',
]);
$this
->alterInfo('charts_type_info');
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('charts_types', $this->moduleHandler
->getModuleDirectories());
$this->discovery
->addTranslatableProperty('label', 'label_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
$definition['id'] = $plugin_id;
foreach ([
'label',
] as $required_property) {
if (empty($definition[$required_property])) {
throw new PluginException(sprintf('The charts type %s must define the %s property.', $plugin_id, $required_property));
}
}
}
public function getDefinitions() {
$definitions = parent::getDefinitions();
$event = new TypesInfoEvent($definitions);
$this->eventDispatcher
->dispatch(ChartsEvents::TYPE_INFO, $event);
$definitions = $event
->getTypes();
return $definitions;
}
}