WebformSourceEntityManager.php in Webform 6.x
File
src/Plugin/WebformSourceEntityManager.php
View source
<?php
namespace Drupal\webform\Plugin;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
class WebformSourceEntityManager extends DefaultPluginManager implements WebformSourceEntityManagerInterface {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/WebformSourceEntity', $namespaces, $module_handler, 'Drupal\\webform\\Plugin\\WebformSourceEntityInterface', 'Drupal\\webform\\Annotation\\WebformSourceEntity');
$this
->alterInfo('webform_source_entity_info');
$this
->setCacheBackend($cache_backend, 'webform_source_entity_info_plugins');
}
protected function alterDefinitions(&$definitions) {
foreach ($definitions as $element_key => $element_definition) {
foreach ($element_definition['dependencies'] as $dependency) {
if (!$this->moduleHandler
->moduleExists($dependency)) {
unset($definitions[$element_key]);
continue;
}
}
}
uasort($definitions, [
SortArray::class,
'sortByWeightElement',
]);
parent::alterDefinitions($definitions);
}
public static function getMainSourceEntity(EntityInterface $source_entity) {
if (\Drupal::moduleHandler()
->moduleExists('paragraphs')) {
while ($source_entity instanceof \Drupal\paragraphs\Entity\Paragraph) {
$source_entity = $source_entity
->getParentEntity();
}
}
return $source_entity;
}
public function getSourceEntity($ignored_types = []) {
if (!is_array($ignored_types)) {
$ignored_types = [
$ignored_types,
];
}
foreach ($this
->getDefinitions() as $plugin_id => $definition) {
$instance = $this
->createInstance($plugin_id);
$source_entity = $instance
->getSourceEntity($ignored_types);
if ($source_entity) {
return $source_entity;
}
}
return NULL;
}
}