You are here

class PatternSettings in UI Patterns Settings 8.2

Same name and namespace in other branches
  1. 8 src/Element/PatternSettings.php \Drupal\ui_patterns_settings\Element\PatternSettings

Renders a pattern element.

Hierarchy

Expanded class hierarchy of PatternSettings

File

src/Element/PatternSettings.php, line 14

Namespace

Drupal\ui_patterns_settings\Element
View source
class PatternSettings implements TrustedCallbackInterface {

  /**
   * Process settings for preview.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function processPreviewSettings(array $element) {
    return PatternSettings::processSettings($element, TRUE);
  }

  /**
   * Process settings.
   *
   * @param array $element
   *   Render array.
   * @param bool $preview
   *   True when called in pattern preview mode.
   *
   * @return array
   *   Render array.
   */
  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;
  }

  /**
   * Whereas pattern has settings or not.
   *
   * @return bool
   *   TRUE or FALSE.
   */
  public static function hasSettings($element) {
    $definition = UiPatterns::getPatternDefinition($element['#id']);
    $settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
    if ($definition != NULL && count($settings) != 0) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'processSettings',
      'processPreviewSettings',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PatternSettings::hasSettings public static function Whereas pattern has settings or not.
PatternSettings::processPreviewSettings public static function Process settings for preview.
PatternSettings::processSettings public static function Process settings.
PatternSettings::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.