View source
<?php
namespace Drupal\webform;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\webform\Element\WebformCompositeBase;
use Drupal\webform\Plugin\WebformElementManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformOptionsStorage extends ConfigEntityStorage implements WebformOptionsStorageInterface {
protected $elementInfo;
protected $elementManager;
protected $usedByCompositeElements;
protected $usedByWebforms;
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, ElementInfoManagerInterface $element_info, WebformElementManagerInterface $element_manager, MemoryCacheInterface $memory_cache = NULL) {
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
$this->elementInfo = $element_info;
$this->elementManager = $element_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('plugin.manager.element_info'), $container
->get('plugin.manager.webform.element'), $container
->get('entity.memory_cache'));
}
public function getCategories() {
$webform_options = $this
->loadMultiple();
$categories = [];
foreach ($webform_options as $webform_option) {
if ($category = $webform_option
->get('category')) {
$categories[$category] = $category;
}
}
ksort($categories);
return $categories;
}
public function getOptions() {
$webform_options = $this
->loadMultiple();
@uasort($webform_options, [
$this->entityType
->getClass(),
'sort',
]);
$uncategorized_options = [];
$categorized_options = [];
foreach ($webform_options as $id => $webform_option) {
if ($category = $webform_option
->get('category')) {
$categorized_options[$category][$id] = $webform_option
->label();
}
else {
$uncategorized_options[$id] = $webform_option
->label();
}
}
return $uncategorized_options + $categorized_options;
}
public function getLikerts() {
$webform_options = $this
->loadByProperties([
'likert' => TRUE,
]);
@uasort($webform_options, [
$this->entityType
->getClass(),
'sort',
]);
$likert_options = [];
foreach ($webform_options as $id => $webform_option) {
$likert_options[$id] = str_replace(t('Likert') . ': ', '', $webform_option
->label());
}
return $likert_options;
}
public function getUsedByCompositeElements(WebformOptionsInterface $webform_options) {
if (!isset($this->usedByCompositeElements)) {
$this->usedByCompositeElements = [];
$definitions = $this->elementInfo
->getDefinitions();
foreach (array_keys($definitions) as $plugin_id) {
$element = $this->elementInfo
->createInstance($plugin_id);
if (!$element instanceof WebformCompositeBase || in_array($plugin_id, [
'webform_composite',
])) {
continue;
}
$composite_elements = $element
->getCompositeElements([]);
foreach ($composite_elements as $composite_element_key => $composite_element) {
if (isset($composite_element['#options'])) {
$webform_element_definition = $this->elementManager
->getDefinition($plugin_id);
$f_args = [
'@composite' => $webform_element_definition['label'],
'@element' => $composite_element['#title'],
];
$this->usedByCompositeElements[$composite_element_key]["{$plugin_id}:{$composite_element_key}"] = new FormattableMarkup('@composite (@element)', $f_args);
}
}
}
}
$options_id = $webform_options
->id();
$used_by = [];
foreach ($this->usedByCompositeElements as $key => $elements) {
if (strpos($options_id, $key) === 0) {
$used_by = array_merge($used_by, $elements);
}
}
if ($used_by) {
$used_by = array_unique($used_by);
asort($used_by);
}
return $used_by;
}
public function getUsedByWebforms(WebformOptionsInterface $webform_options) {
if (!isset($this->usedByWebforms)) {
$this->usedByWebforms = [];
foreach ($this->configFactory
->listAll('webform.webform.') as $webform_config_name) {
$config = $this->configFactory
->get($webform_config_name);
$element_data = Yaml::encode($config
->get('elements'));
if (preg_match_all('/(?:options|answers)\'\\: ([a-z_]+)/', $element_data, $matches)) {
$webform_id = $config
->get('id');
$webform_title = $config
->get('title');
foreach ($matches[1] as $options_id) {
$this->usedByWebforms[$options_id][$webform_id] = $webform_title;
}
}
}
}
$options_id = $webform_options
->id();
return isset($this->usedByWebforms[$options_id]) ? $this->usedByWebforms[$options_id] : [];
}
}