You are here

class UiPatternsSettings in UI Patterns Settings 8

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

UI Patterns setting factory class.

@package Drupal\ui_patterns_settings

Hierarchy

Expanded class hierarchy of UiPatternsSettings

3 files declare their use of UiPatternsSettings
PatternSettings.php in src/Element/PatternSettings.php
SettingsFormBuilder.php in src/Form/SettingsFormBuilder.php
ui_patterns_settings.module in ./ui_patterns_settings.module
Contains ui_patterns_settings.module.

File

src/UiPatternsSettings.php, line 15

Namespace

Drupal\ui_patterns_settings
View source
class UiPatternsSettings {

  /**
   * Get pattern manager setting instance.
   *
   * @return \Drupal\ui_patterns_settings\UiPatternsSettingsManager
   *   UI Patterns setting manager instance.
   */
  public static function getManager() {
    return \Drupal::service('plugin.manager.ui_patterns_settings');
  }

  /**
   * Preprocess all settings variables.
   *
   * @param string $pattern_id
   *   Pattern ID for which to preprocess.
   * @param array $settings
   *   The stored settings.
   * @param string $variant
   *   The variant.
   * @param bool $preview
   *   Is preview.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity of the pattern. Useful for dynamic settings.
   *
   * @return array
   *   The processed settings.
   */
  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;
  }

  /**
   * Get setting definitions for a pattern definition.
   *
   * @param \Drupal\ui_patterns\Definition\PatternDefinition $definition
   *   The definition.
   *
   * @return \Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting[]
   *   Setting pattern definitons.
   */
  public static function getPatternDefinitionSettings(PatternDefinition $definition) {
    $additional = $definition
      ->getAdditional();
    $settings_ary = isset($additional['settings']) ? $additional['settings'] : [];
    $settings = [];
    if (!empty($settings_ary)) {
      foreach ($settings_ary as $key => $setting_ary) {
        $settings[$key] = new PatternDefinitionSetting($key, $setting_ary);
      }
    }
    return $settings;
  }

  /**
   * Create setting type plugin.
   *
   * @param \Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting $settingDefintion
   *   The setting defintion.
   *
   * @return \Drupal\ui_patterns_settings\Plugin\PatternSettingTypeInterface
   *   UI Patterns setting manager instance.
   */
  public static function createSettingType(PatternDefinitionSetting $settingDefintion) {
    $configuration = [];
    $configuration['pattern_setting_definition'] = $settingDefintion;
    return \Drupal::service('plugin.manager.ui_patterns_settings')
      ->createInstance($settingDefintion
      ->getType(), $configuration);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UiPatternsSettings::createSettingType public static function Create setting type plugin.
UiPatternsSettings::getManager public static function Get pattern manager setting instance.
UiPatternsSettings::getPatternDefinitionSettings public static function Get setting definitions for a pattern definition.
UiPatternsSettings::preprocess public static function Preprocess all settings variables.