You are here

public static function UiPatternsSettings::preprocess in UI Patterns Settings 8

Same name and namespace in other branches
  1. 8.2 src/UiPatternsSettings.php \Drupal\ui_patterns_settings\UiPatternsSettings::preprocess()

Preprocess all settings variables.

Parameters

string $pattern_id: Pattern ID for which to preprocess.

array $settings: The stored settings.

string $variant: The variant.

bool $preview: Is preview.

\Drupal\Core\Entity\EntityInterface $entity: The entity of the pattern. Useful for dynamic settings.

Return value

array The processed settings.

1 call to UiPatternsSettings::preprocess()
PatternSettings::processSettings in src/Element/PatternSettings.php
Process settings.

File

src/UiPatternsSettings.php, line 44

Class

UiPatternsSettings
UI Patterns setting factory class.

Namespace

Drupal\ui_patterns_settings

Code

public static function preprocess($pattern_id, array $settings, $variant, $preview, EntityInterface $entity = NULL) {
  $processed_settings = [];
  $definition = UiPatterns::getPatternDefinition($pattern_id);
  $context = [];
  $context['entity'] = $entity;
  $settings_definition = UiPatternsSettings::getPatternDefinitionSettings($definition);
  foreach ($settings_definition as $key => $setting_definition) {
    if ($setting_definition
      ->getForcedValue()) {
      $value = $setting_definition
        ->getForcedValue();
    }
    elseif (isset($settings[$key])) {
      $value = $settings[$key];
    }
    elseif ($preview && !empty($setting_definition
      ->getPreview())) {
      $value = $setting_definition
        ->getPreview();
    }
    else {
      $value = $setting_definition
        ->getDefaultValue();
    }
    if ($variant != 'default' && $variant != NULL) {
      $variant_ob = $definition
        ->getVariant($variant);
      if ($variant_ob != NULL) {
        $variant_ary = $variant_ob
          ->toArray();
        if (isset($variant_ary['settings']) && isset($variant_ary['settings'][$key])) {
          $value = $variant_ary['settings'][$key];
        }
      }
    }
    $settingType = UiPatternsSettings::createSettingType($setting_definition);
    $processed_settings[$key] = $settingType
      ->preprocess($value, $context);
  }
  return $processed_settings;
}