You are here

public static function PatternSettings::processSettings in UI Patterns Settings 8

Same name and namespace in other branches
  1. 8.2 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 39

Class

PatternSettings
Renders a pattern element.

Namespace

Drupal\ui_patterns_settings\Element

Code

public static function processSettings(array $element, $preview = FALSE) {

  // 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'] : [];
    }
    $context = $element['#context'];
    $pattern_id = $element['#id'];
    $entity = $context
      ->getProperty('entity');
    $variant = isset($element['#variant']) ? $element['#variant'] : NULL;
    $settings = UiPatternsSettings::preprocess($pattern_id, $settings, $variant, $preview, $entity);
    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;
}