You are here

class Pattern in UI Patterns 8

Same name in this branch
  1. 8 src/Element/Pattern.php \Drupal\ui_patterns\Element\Pattern
  2. 8 modules/ui_patterns_ds/src/Plugin/DsFieldTemplate/Pattern.php \Drupal\ui_patterns_ds\Plugin\DsFieldTemplate\Pattern
  3. 8 modules/ui_patterns_views/src/Plugin/views/row/Pattern.php \Drupal\ui_patterns_views\Plugin\views\row\Pattern

Renders a pattern element.

Plugin annotation

@RenderElement("pattern");

Hierarchy

Expanded class hierarchy of Pattern

3 string references to 'Pattern'
PatternDisplayFormTrait::buildPatternDisplayForm in src/Form/PatternDisplayFormTrait.php
Build pattern display form.
UiPatternsFieldGroupSettingsTest::testWarningMessage in modules/ui_patterns_field_group/tests/src/FunctionalJavascript/UiPatternsFieldGroupSettingsTest.php
Make sure a warning message is displayed when using pattern formatter.
UiPatternsFieldSettingsTest::testUiPatternsFieldSettings in modules/ui_patterns_ds/tests/src/FunctionalJavascript/UiPatternsFieldSettingsTest.php
Tests field template settings.
4 #type uses of Pattern
FieldTemplateProcessor::process in modules/ui_patterns_ds/src/FieldTemplateProcessor.php
Process field template variables.
PatternLayout::build in modules/ui_patterns_layouts/src/Plugin/Layout/PatternLayout.php
Build a render array for layout with regions.
template_preprocess_pattern_views_row in modules/ui_patterns_views/ui_patterns_views.module
Preprocess hook.
TwigExtension::renderPattern in src/Template/TwigExtension.php
Render given pattern.

File

src/Element/Pattern.php, line 14

Namespace

Drupal\ui_patterns\Element
View source
class Pattern extends RenderElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => FALSE,
      '#multiple_sources' => FALSE,
      '#pre_render' => [
        [
          $class,
          'processContext',
        ],
        [
          $class,
          'processRenderArray',
        ],
        [
          $class,
          'processLibraries',
        ],
        [
          $class,
          'processMultipleSources',
        ],
        [
          $class,
          'processFields',
        ],
        [
          $class,
          'ensureVariant',
        ],
        [
          $class,
          'processUse',
        ],
      ],
    ];
  }

  /**
   * Process render array.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function processRenderArray(array $element) {
    $element['#theme'] = UiPatterns::getPatternDefinition($element['#id'])
      ->getThemeHook();
    if (isset($element['#attributes']) && !empty($element['#attributes']) && is_array($element['#attributes'])) {
      $element['#attributes'] = new Attribute($element['#attributes']);
    }
    else {
      $element['#attributes'] = new Attribute();
    }
    unset($element['#type']);
    return $element;
  }

  /**
   * Process libraries.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function processLibraries(array $element) {
    foreach (UiPatterns::getPatternDefinition($element['#id'])
      ->getLibrariesNames() as $library) {
      $element['#attached']['library'][] = $library;
    }
    return $element;
  }

  /**
   * Process fields.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function processFields(array $element) {

    // Make sure we don't render anything in case fields are empty.
    if (self::hasFields($element)) {
      $fields = $element['#fields'];
      unset($element['#fields']);
      foreach ($fields as $name => $field) {
        $key = '#' . $name;
        $element[$key] = $field;
      }
    }
    else {
      $element['#markup'] = '';
    }
    return $element;
  }

  /**
   * Make sure that we never pass through a value that is not a string.
   *
   * This would prevent accidental assignments of a render array as variant
   * which would break hook_ui_patterns_suggestions_alter().
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function ensureVariant(array $element) {
    if (!isset($element['#variant']) || !is_string($element['#variant'])) {
      $element['#variant'] = '';
    }
    return $element;
  }

  /**
   * Process use property.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function processUse(array $element) {
    $definition = UiPatterns::getPatternDefinition($element['#id']);
    if ($definition
      ->hasUse()) {
      $element['#use'] = $definition
        ->getUse();
    }
    return $element;
  }

  /**
   * Process fields.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   */
  public static function processMultipleSources(array $element) {

    // Make sure we don't render anything in case fields are empty.
    if (self::hasFields($element) && self::hasMultipleSources($element)) {
      foreach ($element['#fields'] as $name => $field) {

        // This guarantees backward compatibility: single sources be simple.
        $element['#fields'][$name] = reset($field);
        if (count($field) > 1) {

          /** @var \Drupal\ui_patterns\Element\PatternContext $context */
          $context = $element['#context'];
          $context
            ->setProperty('pattern', $element['#id']);
          $context
            ->setProperty('field', $name);

          // Render multiple sources with "patterns_destination" template.
          $element['#fields'][$name] = [
            '#sources' => $field,
            '#context' => $context,
            '#theme' => 'patterns_destination',
          ];
        }
      }
    }
    return $element;
  }

  /**
   * Process context.
   *
   * @param array $element
   *   Render array.
   *
   * @return array
   *   Render array.
   *
   * @throws \Drupal\ui_patterns\Exception\PatternRenderException
   *    Throws an exception if no context type is specified.
   */
  public static function processContext(array $element) {
    if (self::hasValidContext($element)) {
      $context = $element['#context'];
      $element['#context'] = new PatternContext($context['type'], $element['#context']);
    }
    else {
      $element['#context'] = new PatternContext('empty');
    }
    return $element;
  }

  /**
   * Whereas pattern has field or not.
   *
   * @param array $element
   *   Render array.
   *
   * @return bool
   *   TRUE or FALSE.
   */
  public static function hasFields(array $element) {
    return isset($element['#fields']) && !empty($element['#fields']) && is_array($element['#fields']);
  }

  /**
   * Whereas pattern fields can accept multiple sources.
   *
   * @param array $element
   *   Render array.
   *
   * @return bool
   *   TRUE or FALSE.
   */
  public static function hasMultipleSources(array $element) {
    return isset($element['#multiple_sources']) && $element['#multiple_sources'] === TRUE;
  }

  /**
   * Whereas pattern has a valid context, i.e. context "type" is set.
   *
   * @param array $element
   *   Render array.
   *
   * @return bool
   *   TRUE or FALSE.
   */
  public static function hasValidContext(array $element) {
    return isset($element['#context']) && is_array($element['#context']) && !empty($element['#context']['type']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
Pattern::ensureVariant public static function Make sure that we never pass through a value that is not a string.
Pattern::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Pattern::hasFields public static function Whereas pattern has field or not.
Pattern::hasMultipleSources public static function Whereas pattern fields can accept multiple sources.
Pattern::hasValidContext public static function Whereas pattern has a valid context, i.e. context "type" is set.
Pattern::processContext public static function Process context. 1
Pattern::processFields public static function Process fields. 1
Pattern::processLibraries public static function Process libraries.
Pattern::processMultipleSources public static function Process fields.
Pattern::processRenderArray public static function Process render array.
Pattern::processUse public static function Process use property.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.