PatternSettings.php in UI Patterns Settings 8.2
File
src/Element/PatternSettings.php
View source
<?php
namespace Drupal\ui_patterns_settings\Element;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Template\Attribute;
use Drupal\ui_patterns\UiPatterns;
use Drupal\ui_patterns_settings\UiPatternsSettings;
use Drupal\Core\Entity\EntityMalformedException;
class PatternSettings implements TrustedCallbackInterface {
public static function processPreviewSettings(array $element) {
return PatternSettings::processSettings($element, TRUE);
}
public static function processSettings(array $element, $preview = FALSE) {
$alter_context = [];
$context = $element['#context'];
$pattern_id = $element['#id'];
$entity = $context
->getProperty('entity');
if ($entity !== NULL) {
$mappings = UiPatternsSettings::getConfigManager()
->findVariantMappings($entity
->getEntityTypeId());
foreach ($mappings as $field_name) {
if ($entity
->hasField($field_name) && !empty($entity
->get($field_name)->value)) {
$element['#variant'] = $entity
->get($field_name)->value;
}
}
}
if (empty($element['#variant_token']) && isset($element['#layout'])) {
$layout = $element['#layout'];
$configuration = $layout
->getConfiguration();
$element['#variant_token'] = isset($configuration['pattern']['variant_token']) ? $configuration['pattern']['variant_token'] : NULL;
}
if (!empty($element['#variant_token'])) {
$variant_token = $element['#variant_token'];
$token_data = [];
if ($entity !== NULL) {
$token_data[$entity
->getEntityTypeId()] = $entity;
}
try {
$element['#variant'] = \Drupal::token()
->replace($variant_token, $token_data, [
'clear' => TRUE,
]);
} catch (EntityMalformedException $e) {
if (!ui_patterns_settings_is_layout_builder_route()) {
throw $e;
}
}
}
$variant_alter_context['#pattern_id'] = $pattern_id;
if (isset($element['#layout'])) {
$variant_alter_context['#layout'] = $element['#layout'];
}
$variant_alter_context['#pattern_context'] = $context;
$variant = $element['#variant'];
\Drupal::moduleHandler()
->alter('ui_pattern_settings_variant', $variant, $variant_alter_context);
$element['#variant'] = $variant;
if (self::hasSettings($element)) {
$settings = isset($element['#settings']) ? $element['#settings'] : [];
if (empty($settings)) {
$settings = isset($element['#ds_configuration']['layout']['settings']['pattern']['settings']) ? $element['#ds_configuration']['layout']['settings']['pattern']['settings'] : [];
}
if (empty($settings) && isset($element['#layout'])) {
$layout = $element['#layout'];
$configuration = $layout
->getConfiguration();
$settings = isset($configuration['pattern']['settings']) ? $configuration['pattern']['settings'] : [];
}
$entity = $context
->getProperty('entity');
$variant = isset($element['#variant']) ? $element['#variant'] : NULL;
$settings = UiPatternsSettings::preprocess($pattern_id, $settings, $variant, $preview, $entity);
if (isset($element['#layout'])) {
$alter_context['#layout'] = $element['#layout'];
}
$alter_context['#pattern_id'] = $pattern_id;
$alter_context['#variant'] = $variant;
$alter_context['#pattern_context'] = $context;
\Drupal::moduleHandler()
->alter('ui_pattern_settings_settings', $settings, $alter_context);
unset($element['#settings']);
foreach ($settings as $name => $setting) {
$key = '#' . $name;
if (!isset($element[$key])) {
$element[$key] = $setting;
}
else {
if ($setting instanceof Attribute && $element[$key] instanceof Attribute) {
$element[$key] = new Attribute(array_merge($setting
->toArray(), $element[$key]
->toArray()));
}
elseif (is_array($element[$key]) && is_array($setting)) {
$element[$key] = array_merge($element[$key], $setting);
}
}
}
}
return $element;
}
public static function hasSettings($element) {
$definition = UiPatterns::getPatternDefinition($element['#id']);
$settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
if ($definition != NULL && count($settings) != 0) {
return TRUE;
}
else {
return FALSE;
}
}
public static function trustedCallbacks() {
return [
'processSettings',
'processPreviewSettings',
];
}
}