class Pattern in UI Patterns 8
Same name in this branch
- 8 src/Element/Pattern.php \Drupal\ui_patterns\Element\Pattern
- 8 modules/ui_patterns_ds/src/Plugin/DsFieldTemplate/Pattern.php \Drupal\ui_patterns_ds\Plugin\DsFieldTemplate\Pattern
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\ui_patterns\Element\Pattern
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\ElementView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Pattern:: |
public static | function | Make sure that we never pass through a value that is not a string. | |
Pattern:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
Pattern:: |
public static | function | Whereas pattern has field or not. | |
Pattern:: |
public static | function | Whereas pattern fields can accept multiple sources. | |
Pattern:: |
public static | function | Whereas pattern has a valid context, i.e. context "type" is set. | |
Pattern:: |
public static | function | Process context. | 1 |
Pattern:: |
public static | function | Process fields. | 1 |
Pattern:: |
public static | function | Process libraries. | |
Pattern:: |
public static | function | Process fields. | |
Pattern:: |
public static | function | Process render array. | |
Pattern:: |
public static | function | Process use property. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |