You are here

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

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

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;
  if ($entity instanceof ContentEntityBase) {
    $processed_settings = self::preprocessExposedFields($entity, $definition);
  }
  $settings_definition = UiPatternsSettings::getPatternDefinitionSettings($definition);
  foreach ($settings_definition as $key => $setting_definition) {
    if ($setting_definition
      ->getForcedValue()) {
      $value = $setting_definition
        ->getForcedValue();
    }
    elseif (!empty($settings[$key . '_token'])) {
      $token_value = $settings[$key . '_token'];
      $token_data = [];
      if ($entity !== NULL) {
        $token_data[$entity
          ->getEntityTypeId()] = $entity;
      }
      try {
        $value = \Drupal::token()
          ->replace($token_value, $token_data, [
          'clear' => TRUE,
        ]);
      } catch (EntityMalformedException $e) {
        if (!ui_patterns_settings_is_layout_builder_route()) {
          throw $e;
        }

        // Do nothing inside layout builder.
      }
    }
    elseif (isset($settings[$key])) {
      $value = $settings[$key];
    }
    elseif ($preview && !empty($setting_definition
      ->getPreview())) {
      $value = $setting_definition
        ->getPreview();
    }
    else {
      $value = $setting_definition
        ->getDefaultValue();
    }
    if (!empty($variant) && $definition
      ->hasVariant($variant)) {
      $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($definition, $setting_definition);
    $processed_value = $settingType
      ->preprocess($value, $context);
    if (!isset($processed_settings[$key]) || !empty($processed_value)) {
      $processed_settings[$key] = $processed_value;
    }
  }
  return $processed_settings;
}