View source
<?php
namespace Drupal\yamlform;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
class YamlFormElementManager extends DefaultPluginManager implements FallbackPluginManagerInterface, YamlFormElementManagerInterface {
use CategorizingPluginManagerTrait;
protected $instances = [];
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/YamlFormElement', $namespaces, $module_handler, 'Drupal\\yamlform\\YamlFormElementInterface', 'Drupal\\yamlform\\Annotation\\YamlFormElement');
$this
->alterInfo('yamlform_element_info');
$this
->setCacheBackend($cache_backend, 'yamlform_element_plugins');
}
public function getFallbackPluginId($plugin_id, array $configuration = []) {
return 'yamlform_element';
}
public function createInstance($plugin_id, array $configuration = []) {
if (empty($configuration)) {
if (!isset($this->instances[$plugin_id])) {
$this->instances[$plugin_id] = parent::createInstance($plugin_id, $configuration);
}
return $this->instances[$plugin_id];
}
else {
return parent::createInstance($plugin_id, $configuration);
}
}
public function getInstances() {
$plugin_definitions = $this
->getDefinitions();
$plugin_definitions = $this
->getSortedDefinitions($plugin_definitions);
if (count($plugin_definitions) == count($this->instances)) {
return $this->instances;
}
foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
$this
->createInstance($plugin_id);
}
return $this->instances;
}
public function invokeMethod($method, array &$element, &$context1 = NULL, &$context2 = NULL) {
if (!isset($element['#type'])) {
return NULL;
}
$plugin_id = $this
->getElementPluginId($element);
$yamlform_element = $this
->createInstance($plugin_id);
return $yamlform_element
->{$method}($element, $context1, $context2);
}
public function getElementPluginId(array $element) {
if (isset($element['#type'])) {
if ($this
->hasDefinition($element['#type'])) {
return $element['#type'];
}
elseif ($this
->hasDefinition('yamlform_' . $element['#type'])) {
return 'yamlform_' . $element['#type'];
}
}
elseif (isset($element['#markup'])) {
return 'yamlform_markup';
}
return $this
->getFallbackPluginId(NULL);
}
public function getElementInstance(array $element) {
$plugin_id = $this
->getElementPluginId($element);
return $this
->createInstance($plugin_id, $element);
}
public function getSortedDefinitions(array $definitions = NULL, $sort_by = 'label') {
$definitions = isset($definitions) ? $definitions : $this
->getDefinitions();
switch ($sort_by) {
case 'category':
uasort($definitions, function ($a, $b) use ($sort_by) {
return strnatcasecmp($a['category'] . '-' . $a[$sort_by], $b['category'] . '-' . $b[$sort_by]);
});
break;
default:
uasort($definitions, function ($a, $b) use ($sort_by) {
return strnatcasecmp($a[$sort_by], $b[$sort_by]);
});
break;
}
return $definitions;
}
public function getTranslatableProperties() {
$properties = [];
$yamlform_elements = $this
->getInstances();
foreach ($yamlform_elements as $yamlform_element) {
$translatable_properties = $yamlform_element
->getTranslatableProperties();
$properties += array_combine($translatable_properties, $translatable_properties);
}
ksort($properties);
return $properties;
}
public function getAllProperties() {
$properties = [];
$yamlform_elements = $this
->getInstances();
foreach ($yamlform_elements as $yamlform_element) {
$default_properties = array_keys($yamlform_element
->getDefaultProperties());
$properties += array_combine($default_properties, $default_properties);
}
ksort($properties);
return $properties;
}
}