Repository URL to install this package:
|
Version:
2.5.1 ▾
|
<?php
namespace Drupal\dds_global_content;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
class GlobalContentManager extends DefaultPluginManager {
/**
* Constructs a ImportManager 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/GlobalContent',
$namespaces,
$module_handler,
'Drupal\dds_global_content\GlobalContentPluginInterface',
'Drupal\dds_global_content\Annotation\GlobalContent'
);
$this->alterInfo('integration_service');
$this->setCacheBackend($cache_backend, 'global_content_plugins');
}
/**
* @param $type
*
* @return GlobalContentPluginInterface|null
*/
public function createInstanceFromType($type) {
static $plugins = [];
$cached = $this->cacheBackend->get($this->cacheKey . ':types');
if (empty($cached->data)) {
$this->setCachedGlobalContentTypes();
$cached = $this->cacheBackend->get($this->cacheKey . ':types');
}
if (!empty($cached->data)) {
$types = $cached->data;
if (!empty($types[$type])) {
if (!isset($plugins[$types[$type]])) {
try {
$plugins[$types[$type]] = $this->createInstance($types[$type]);
} catch (PluginException $e) {
$plugins[$types[$type]] = NULL;
}
}
return $plugins[$types[$type]];
} else {
$this->setCachedGlobalContentTypes();
}
}
return NULL;
}
public function setCachedGlobalContentTypes() {
$definitions = $this->getDefinitions();
$types = [];
foreach ($definitions as $key => $definition) {
if (!empty($definition['types'])) {
foreach ($definition['types'] as $type) {
$types[$type] = $key;
}
}
}
$this->cacheBackend->set(
$this->cacheKey . ':types',
$types,
self::getCacheMaxAge()
);
}
/**
* Returns an array of names of content types for use in the administration interface.
*
* @return array
*/
public function getTypeNames() {
static $names = [];
if (empty($names)) {
$cached = $this->cacheBackend->get($this->cacheKey . ':types:names');
if (!empty($cached->data)) {
$names = $cached->data;
}
else {
foreach ($this->getDefinitions() as $plugin_id => $definition) {
if (!empty($definition['types'])) {
try {
/** @var \Drupal\dds_global_content\GlobalContentPluginInterface $plugin */
$plugin = $this->createInstance($plugin_id);
foreach ($plugin->getTypeNames() as $type => $name) {
$names[$type] = $name;
}
} catch (PluginException $e) {
}
}
}
$this->cacheBackend->set(
$this->cacheKey . ':types:names',
$names,
self::getCacheMaxAge()
);
}
}
return $names;
}
}