public static function PatternSettings::processSettings in UI Patterns Settings 8.2
Same name and namespace in other branches
- 8 src/Element/PatternSettings.php \Drupal\ui_patterns_settings\Element\PatternSettings::processSettings()
Process settings.
Parameters
array $element: Render array.
bool $preview: True when called in pattern preview mode.
Return value
array Render array.
1 call to PatternSettings::processSettings()
- PatternSettings::processPreviewSettings in src/
Element/ PatternSettings.php - Process settings for preview.
File
- src/
Element/ PatternSettings.php, line 40
Class
- PatternSettings
- Renders a pattern element.
Namespace
Drupal\ui_patterns_settings\ElementCode
public static function processSettings(array $element, $preview = FALSE) {
$alter_context = [];
$context = $element['#context'];
$pattern_id = $element['#id'];
/** @var \Drupal\Core\Entity\ContentEntityBase $entity */
$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;
}
}
}
// Handling variant token for layout builder.
if (empty($element['#variant_token']) && isset($element['#layout'])) {
/** @var \Drupal\ui_patterns_layout_builder\Plugin\Layout\PatternLayoutBuilder $layout */
$layout = $element['#layout'];
$configuration = $layout
->getConfiguration();
$element['#variant_token'] = isset($configuration['pattern']['variant_token']) ? $configuration['pattern']['variant_token'] : NULL;
}
// Handle Variant token.
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;
}
// Do nothing inside layout builder.
}
}
$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;
// Make sure we don't render anything in case fields are empty.
if (self::hasSettings($element)) {
$settings = isset($element['#settings']) ? $element['#settings'] : [];
// Handling display suite pattern.
if (empty($settings)) {
$settings = isset($element['#ds_configuration']['layout']['settings']['pattern']['settings']) ? $element['#ds_configuration']['layout']['settings']['pattern']['settings'] : [];
}
// Handling layout builder.
if (empty($settings) && isset($element['#layout'])) {
/** @var \Drupal\ui_patterns_layout_builder\Plugin\Layout\PatternLayoutBuilder $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;
}