You are here

class PatternSettings 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

Renders a pattern element.

Hierarchy

Expanded class hierarchy of PatternSettings

File

src/Element/PatternSettings.php, line 13

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) {

    // 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;
  }

  /**
   * 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.