### Example `meta:enum` Usage Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Demonstrates the use of `meta:enum` to provide human-readable labels for enum values. This is a non-standard extension supported by certain tools. ```yaml props: type: object properties: position: type: string enum: - top - bottom "meta:enum": top: Top bottom: Bottom ``` -------------------------------- ### Block Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML example configures a 'block' source plugin in UI Patterns 2, specifying a plugin ID for 'system_branding_block' and its related configuration. This is an example of a 'source of sources' used for dynamic content inclusion. ```yaml source_id: block source: plugin_id: system_branding_block system_branding_block: id: system_branding_block use_site_name: false use_site_slogan: false ``` -------------------------------- ### Full YAML Story Example with Nested Components and Lists Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library A comprehensive example of a story definition, including a media slot with an HTML tag, a nested component for transcription, and a list of buttons within an actions slot. Also includes component props. ```yaml name: Video card description: A card with a video slots: media: type: html_tag tag: img attributes: src: http://example.org/path/to/media.png bottom: type: component component: "my_project:transcription" slots: text: "When they got to the road he stopped the boy with his hand." actions: - type: component component: "material:button" slots: label: Bookmark - type: component component: "material:button" slots: label: Bookmark props: title: Wanna learn about Drupal? date: "2024-05-22 - 11:38pm" ``` -------------------------------- ### UI Patterns 2: Prop Schema with Attributes Reference Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Example of using a JSON schema reference (`ui-patterns://attributes`) for defining a prop that accepts arbitrary HTML attributes. ```yaml props: type: object properties: figcaption_attributes: title: "Figcaption attributes" description: "The attributes to customize the figcaption tag." "$ref": "ui-patterns://attributes" ``` -------------------------------- ### UI Patterns 2: String Enum Prop Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Example of a string enum prop, restricting the input to a predefined list of string values. ```yaml enum: - "Foo" - "Bar" - "Baz" ``` -------------------------------- ### Twig Template Function Migration (UI Patterns) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/3-migration-from-UIP1 Demonstrates the conversion of the `pattern()` function in UI Patterns 1.x to the `include()` function in UI Patterns 2.x. The example shows how component IDs are prefixed and variants are now passed as props. ```twig pattern("card", {slot_1: foo, slot_2: bar, prop_1: baz, prop_2: true}, variant_id) ``` ```twig include("bootstrap:card", {slot_1: foo, slot_2: bar, prop_1: baz, prop_2: true, variant: variant_id}, with_context = false) ``` -------------------------------- ### Select Widget Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML example shows a 'select' source plugin for a widget. It specifies the source value as 'container'. This configuration is part of the form-building process for UI Patterns 2, handling single select inputs. ```yaml source_id: select source: value: container ``` -------------------------------- ### UI Patterns 2: Specific Enum List Prop Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Example of an array prop with defined minimum and maximum items, where each item must be an integer from a specific list of values (e.g., grid column spans). ```yaml type: array minItems: 2 maxItems: 4 items: type: integer enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] default: [4, 4, 4, 4] ``` -------------------------------- ### Install Twig CS Fixer for Template Linting Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Installs the twig-cs-fixer Composer package, a tool that follows official Twig Coding Standard Rules to detect syntax errors and structural problems in Twig templates. ```bash composer require --dev vincentlanglet/twig-cs-fixer ``` -------------------------------- ### UI Patterns 2: Component Definition with Variants Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Illustrates the `variants` property in a UI Patterns 2 component, allowing for predefined variations of a component with custom titles and descriptions. ```yaml name: Card variants: default: title: Default description: An ordinary card. highlighted: title: Highlighted description: A special card. slots: {} props: {} ``` -------------------------------- ### UI Patterns 2: Number Enum Prop Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Example of a numeric enum prop, restricting the input to a predefined list of numeric values. ```yaml enum: - 5 - 10 - 15 ``` -------------------------------- ### UI Patterns 2: Component Definition with Links Metadata Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Demonstrates the `links` metadata property in a UI Patterns 2 component definition, supporting both compact and full syntaxes for documentation URLs. ```yaml name: Example links: - "https://test.com" - url: "https://example.com" title: "Example" props: {} slots: {} ``` -------------------------------- ### Component Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This example demonstrates how a component can be defined as a source in UI Patterns 2. It specifies the component ID and allows for configuration of slots and props. This is part of the process of building a form for each component by loading source plugins, enabling UI Patterns to work with external SDC components. ```yaml source_id: component source: component: component_id: "ui_suite_bootstrap:accordion" slots: {} props: {} ``` -------------------------------- ### Component Props Definition (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Defines the properties (props) for a component, including their types, titles, and formats. This example shows props for a Call to Action (CTA) with text and a link. ```yaml props: type: object properties: cta: type: string title: CTA text cta_href: type: string format: uri title: CTA link ``` -------------------------------- ### Render Pattern Preview Element Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/3-migration-from-UIP1 Illustrates the transformation of a `pattern_preview` render element for a blockquote into a standardized 'component' representation. This conversion is essential for runtime processing and display of pattern previews. ```php [ '#type' => 'pattern_preview', '#id' => 'blockquote', '#variant' => 'highlighted', ] ``` ```php [ '#type' => 'component', '#component' => 'provider:blockquote', '#story' => 'preview', '#props' => [ 'variant' => 'highlighted' ] ] ``` -------------------------------- ### Menu Block Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML snippet defines a 'system_menu_block' source plugin in UI Patterns 2, including its ID, label, and display settings. This is another example of a 'source of sources', used for embedding menu blocks. ```yaml plugin_id: "system_menu_block:account" "system_menu_block:account": id: "system_menu_block:account" label: "User account menu" label_display: "1" provider: system level: 1 depth: 1 expand_all_items: false ``` -------------------------------- ### Component Template using Slots (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Renders content injected into a component's slot. This example displays the content assigned to the 'picture' slot. ```twig {{ picture }} ``` -------------------------------- ### Flat Slot Structure (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Demonstrates a preferred way to structure slots by keeping them flat. This example shows two separate 'material:card' components defined as slots. ```yaml slots: - type: component component: "material:card" story: long_text - type: component component: "material:card" story: long_text ``` -------------------------------- ### UI Patterns 2: Component Definition with Tags Metadata Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Shows the `tags` metadata property for documentation purposes within a UI Patterns 2 component definition. ```yaml name: Example tags: [Foo, Bar, Baz] slots: {} props: {} ``` -------------------------------- ### Component Slots Definition (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Defines slots within a component, allowing for dynamic content injection. This example defines a 'picture' slot for injecting an image element. ```yaml slots: picture: title: "Picture" ``` -------------------------------- ### UI Patterns 2: Resolved Schema for Attributes Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component The resolved JSON schema for the `ui-patterns://attributes` reference, defining a pattern for key-value pairs typical for HTML attributes. ```json { "type": "object", "patternProperties": { ".+": { "anyOf": [ { "type": [ "string", "number" ] }, { "type": "array", "items": { "anyOf": [ { "type": "number" }, { "type": "string" } ] } } ] } } } ``` -------------------------------- ### Component Template using Props (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Illustrates how component props are used within a Twig template to render HTML elements. This example uses 'cta' and 'cta_href' props to create a link. ```twig ``` -------------------------------- ### Complex Clean Drupal Template Example (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/1-users/5-presenter-templates Presents a more elaborate example of a cleaned Drupal template, including prefix and suffix variables, content rendering, and nested attributes. This pattern is useful when components or utility styles require specific markup structures. ```twig {% if attributes.storage() %} {% endif %} {{ title_prefix }} {{ title_suffix }} {{ content }} {% if attributes.storage() %} {% endif %} {{ content }} {% if attributes.storage() %} {% endif %} {% if attributes.storage() %} {% endif %} ``` -------------------------------- ### Injected Slot Data (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Provides data to be injected into a component's slot. This example defines the attributes for an 'img' tag to be used with the 'picture' slot. ```yaml slots: picture: type: "html_tag" tag: "img" attributes: src: "/path/to/photo.jpg" alt: "Hello world" ``` -------------------------------- ### Derived Entity Field Property Source Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML example illustrates a derived source for an entity field property in UI Patterns 2. It uses 'derivable_context' to link to a file entity based on the user picture and specifies its value sources. This showcases advanced derivation for entity properties. ```yaml source_id: entity:field_property:user:user_picture:target_id source: derivable_context: entity_reference:user:user:user_picture:file:file entity_reference:user:user:user_picture:file:file: value: sources: [] ``` -------------------------------- ### Drupal Layout Plugin Definition Example Source: https://project.pages.drupalcode.org/ui_patterns/faq This code snippet demonstrates how a layout plugin might be defined in Drupal. It specifies the theme hook to be used for rendering the layout. This is relevant when discussing compatibility issues with modules like ckeditor_layouts. ```php $template = ['#theme' => $layout->getThemeHook()]; ``` -------------------------------- ### Nested Slot Structure (YAML - Anti-pattern) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices An example of an anti-pattern where slots are nested in a way that can lead to maintenance issues and difficulties in UI administration. ```yaml slots: - type: component component: "material:card" ``` -------------------------------- ### UI Patterns 2: Boolean Prop Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Defines a simple boolean prop type using the `ui-patterns://boolean` reference or directly as `type: boolean`. ```yaml type: boolean ``` -------------------------------- ### Checkbox Widget Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML example shows a 'checkbox' source plugin for a widget. It sets the source value to 1, typically representing a checked state. This is part of the form-building mechanism in UI Patterns 2 for handling boolean inputs. ```yaml source_id: checkbox source: value: 1 ``` -------------------------------- ### Define String Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Defines a basic string type. This is the most fundamental schema for text-based data. ```json type: string ``` -------------------------------- ### UI Patterns 2: Props Schema with Variant Enum Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Defines the props schema for a component that uses variants, exposing the variant selection as a string prop with an enum. ```yaml name: Card props: type: object properties: variants: type: string enum: [default, highlighted] ``` -------------------------------- ### Render Blockquote Pattern Elements Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/3-migration-from-UIP1 Demonstrates the conversion of different 'blockquote' pattern render element structures into a unified 'component' type. This process standardizes how blockquote patterns, including variants and fields, are represented internally for rendering. ```php [ '#type' => 'pattern', '#id' => 'blockquote', '#variant' => 'highlighted', '#fields' => [ 'quote' => 'You must do the things you think you cannot do.', 'attribution' => 'Eleanor Roosevelt', ], '#settings' => [ 'url' => 'https://example.org' ] ] ``` ```php [ '#type' => 'pattern', '#id' => 'blockquote', '#variant' => 'highlighted', 'quote' => 'You must do the things you think you cannot do.', 'attribution' => 'Eleanor Roosevelt', 'url' => 'https://example.org' ] ``` ```php [ '#type' => 'component', '#component' => 'provider:blockquote', '#slots' => [ 'quote' => 'You must do the things you think you cannot do.', 'attribution' => 'Eleanor Roosevelt', ], '#props' => [ 'url' => 'https://example.org', 'variant' => 'highlighted' ] ] ``` -------------------------------- ### YAML Slot Definition without Drupal Render API Prefix Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library The equivalent of the previous example, but using the standard 'type', 'tag', and 'attributes' keys for defining slot content. This is the more common and preferred method. ```yaml slots: picture: type: html_tag tag: img attributes: src: http://example.org/path/to/media.png ``` -------------------------------- ### Automatic UI Patterns Component Migration (Drush) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/3-migration-from-UIP1 This command initiates the automatic migration of UI Patterns components from version 1.x to 2.x. It requires the `ui_patterns_legacy` submodule to be active and processes components within a specified theme, saving converted components in the 'components/' directory. ```bash drush ui-patterns:migrate my_theme ``` ```bash drush upm my_theme ``` -------------------------------- ### Lint and Fix Twig Templates with Twig CS Fixer Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Uses the installed twig-cs-fixer to lint and automatically fix Twig templates. This helps maintain code consistency and reduce diff sizes in commits. ```bash vendor/bin/twig-cs-fixer lint --fix /path/to/code ``` -------------------------------- ### Integer with Min/Max Values Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Specifies an integer type with defined minimum and maximum allowable values. This is useful for constraining numerical input within a specific range. ```json type: integer minimum: 1789 maximum: 2025 ``` -------------------------------- ### UI Patterns 2: Enum Prop Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Demonstrates defining a single prop value that is restricted to a fixed set of string or numeric options, using the `ui-patterns://enum` reference or direct schema definition. ```yaml type: ["string", "number", "integer"] enum: [] ``` -------------------------------- ### Wysiwyg Widget Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML snippet illustrates a 'wysiwyg' source plugin for a widget in UI Patterns 2. It defines the source with a value and its format. This is used when building forms for components, allowing rich text content to be handled. ```yaml source_id: wysiwyg source: value: value: "Lorem ipsum dolor sit amet" format: basic_html ``` -------------------------------- ### Selects Widget Source Plugin Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML snippet demonstrates a 'selects' source plugin for a widget in UI Patterns 2. It configures the source value as an array of strings. This is used for multi-select inputs within the component form building process. ```yaml source_id: selects source: value: - "12" - "12" ``` -------------------------------- ### UI Patterns 2: Enum List Prop Schema Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Shows how to define an array prop where each item must be one of a specific set of values, using the `ui-patterns://enum_list` reference or direct schema definition. ```yaml type: array items: type: - string - number - integer enum: [] ``` -------------------------------- ### Process pattern_preview() Twig Function Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/3-migration-from-UIP1 Details the runtime processing of the `pattern_preview()` Twig function, converting it into a 'component' render array with specific props. This is used for displaying pattern previews within the Drupal environment. ```twig {{ pattern_preview('modal', 'highlighted') }} ``` ```php [ '#type' => 'component', '#component' => 'provider:modal', '#story' => 'preview', '#props' => [ 'variant' => 'variant_id' ] ] ``` -------------------------------- ### Apply Variant Class Conditionally (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices This Twig example shows how to conditionally apply a variant class, ensuring that a specific variant ('vertical' in this case) is handled correctly while allowing other variants to apply their respective classes. ```twig {% set attributes = variant and (variant != 'vertical') ? attributes.addClass('btn-group--' ~ variant) : variant %} ``` -------------------------------- ### String Pattern for HTML ID Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Defines a string pattern suitable for HTML IDs, allowing a restricted set of characters. This pattern ensures compatibility with HTML ID requirements. ```json type: string pattern: '(?:--|-?[A-Za-z_\x{00A0}-\x{10FFFF}])[A-Za-z0-9-_x{00A0}-\x{10FFFF}\.]*' ``` -------------------------------- ### Entity Reference Context Switcher Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML demonstrates an 'entity_reference' context switcher in UI Patterns 2. It uses 'derivable_context' to specify a node reference and includes configuration for its value sources. This is used for dynamically referencing entities. ```yaml source_id: entity_reference source: derivable_context: entity_reference:node:article:uid:user:user entity_reference:node:article:uid:user:user: value: sources: [] ``` -------------------------------- ### Define Enum Set with Values Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/0-authoring-a-component Sets a list of unique predefined string or number items. The enum values must be explicitly set using the `enum` property within the schema. ```yaml ui-patterns://enum_set items: enum: ["Foo", "Bar", "Baz"] ``` -------------------------------- ### Entity Link Data Source Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML snippet defines an 'entity_link' data source in UI Patterns 2. It includes a template and an absolute flag. This represents a 'normal' data source used for linking to entities. ```yaml source_id: entity_link source: template: devel-load absolute: 0 ``` -------------------------------- ### Process pattern() Twig Function Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/3-migration-from-UIP1 Shows how the `pattern()` Twig function, used for rendering components with slots and properties, is processed into a Drupal render array. This conversion facilitates dynamic component rendering within Twig templates. ```twig pattern( "card", {slot_1: foo, slot_2: bar, prop_1: baz, prop_2: true}, variant_id ) ``` ```php [ '#type' => 'component', '#component' => 'bootstrap:card', '#slots' => [ 'slot_1' => foo, 'slot_2' => bar, ], '#props' => [ 'prop_1' => 'baz', 'prop_2' => true, 'variant' => 'variant_id' ] ] ``` -------------------------------- ### Entity Field Context Switcher Example (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This YAML snippet shows an 'entity_field' context switcher in UI Patterns 2. It specifies a 'derivable_context' for a user picture field and its value sources. This allows for contextual referencing of entity fields. ```yaml source_id: entity_field source: derivable_context: field:user:user:user_picture field:user:user:user_picture: value: sources: [] ``` -------------------------------- ### Define Component Variants and Descriptions (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices This snippet demonstrates how to define variants for a component in YAML, including labels, titles, and descriptions. Variants allow for different visual presentations of the same component. ```yaml label: Button Group variants: default: title: Default vertical: title: Vertical description: Make a set of buttons appear vertically stacked rather than horizontally. Split button dropdowns are not supported here. lg: title: Large sm: title: Small ``` -------------------------------- ### Alternative Props-based Image Rendering (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Renders an image using separate 'url' and 'label' props, demonstrating the prop drilling anti-pattern. ```twig {{ label }} ``` -------------------------------- ### Twig: Include Components Safely with `include()` Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Demonstrates the correct method for including one component from another using the `include()` function in Twig. It specifies using the component ID and `with_context = false` to prevent context bleeding and avoid direct template path calls, which can lead to dependency issues. ```twig include( "bootstrap:card", {slot_1: foo, slot_2: bar, prop_1: baz, prop_2: true}, with_context = false ) ``` ```twig include( "/templates/includes/other-events.html.twig')", {slot_1: foo, slot_2: bar, prop_1: baz, prop_2: true}, with_context = false ) ``` -------------------------------- ### Alternative Props-based Image Rendering (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices An alternative, less preferred method of defining image properties using separate props ('label' and 'url') instead of a slot. This approach leads to prop drilling. ```yaml props: type: object properties: label: type: "string" label: "Label" url: type: "url" label: "URL" ``` -------------------------------- ### Simplified Flat Slot Structure (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices An even more simplified approach to flat slot structures, where a single component type is directly assigned to the slot. ```yaml slots: type: component component: "material:card" ``` -------------------------------- ### Menu Links Rendering Macro (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices A Twig macro for recursively rendering nested menu links. It handles different levels of menus and applies attributes. It utilizes the 'link' and 'menus' functions. ```twig {{ menus.menu_links(items, attributes, 0) }} {% macro menu_links(items, attributes, menu_level) %} {% if items %} {% if menu_level == 0 %} {% else %}
    {% endif %} {% for item in items %} {{ link(item.title, item.url) }} {% if item.below %} {{ _self.menu_links(item.below, attributes, menu_level + 1) }} {% endif %} {% endfor %}
{% endif %} {% endmacro %} ``` -------------------------------- ### Component Props for Size and Modifiers (YAML) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Defines component props with an enum for size and boolean flags for modifiers, helping to avoid prop paradoxes and ensure compatible prop combinations. ```yaml props: type: object properties: size: type: string enum: [xs, sm, md, lg] wide: type: boolean _block: type: boolean ``` -------------------------------- ### YAML Story with Multiple Renderables in a Slot Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library Shows how to define a slot that can contain multiple renderable items, such as a list of buttons. This is achieved by providing a YAML list under the slot key. ```yaml slots: actions: - type: component component: "material:button" slots: label: Bookmark - type: component component: "material:button" slots: label: Bookmark ``` -------------------------------- ### YAML Story with Library Wrapper Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library Defines a story that includes a library wrapper, allowing for custom HTML structure around the rendered component. The `_story` variable is used to inject the component's output. ```yaml name: In latin library_wrapper: '
{{ _story }}
' slots: title: "Dapibus ac facilisis" subtitle: "Cras justo odio" props: heading_level: 5 ``` -------------------------------- ### YAML Story Definition for a Component Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library Defines a story for a component, specifying its name, targeting component, slots, and properties. This is useful for documenting specific states or variations of a component. ```yaml name: In latin component: "bootstrap:card" slots: title: "Dapibus ac facilisis" subtitle: "Cras justo odio" props: heading_level: 5 ``` -------------------------------- ### Schema Compatibility Checking (PHP) Source: https://project.pages.drupalcode.org/ui_patterns/3-devs/3-internals This snippet outlines the file structure for schema compatibility checking in UI Patterns 2. It includes classes for canonicalization, checking compatibility, resolving references, and handling scalar types. These components are crucial for ensuring that UI Patterns works with various SDC components by inferring prop types. ```php src/SchemaManager/ ├── Canonicalizer.php ├── CompatibilityChecker.php ├── ReferencesResolver.php ├── ScalarCompatibilityTrait.php └── StreamWrapper.php ``` -------------------------------- ### YAML Slot Definition with Drupal Render API Prefix Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library Illustrates using the Drupal render API prefix ('#') for defining slot content, such as an HTML image tag. This is an alternative to the 'type', 'tag', and 'attributes' keys. ```yaml slots: picture: "#type": html_tag "#tag": img "#attributes": src: http://example.org/path/to/media.png ``` -------------------------------- ### YAML Nested Component Story Definition Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/1-stories-and-library Demonstrates how to define nested components within the slots of a story. This allows for complex component structures and interactions to be documented. ```yaml slots: bottom: type: component component: "my_project:transcription" slots: text: "When they got to the road he stopped the boy with his hand." actions: type: component component: "material:button" slots: label: Bookmark ``` -------------------------------- ### Twig: Generate Dynamic IDs for Components Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Provides methods for generating unique IDs for HTML elements within components to ensure they are repeatable and nestable. It shows how to use the Twig `random()` function or inject an ID via a prop, contrasting with the invalid practice of hardcoding IDs. ```twig Lorem ipsum ``` ```twig {{ attributes.setAttribute("id", id) }} ``` -------------------------------- ### Split Variant Modifier Classes in Twig (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices This Twig snippet illustrates how to split a variant string and apply multiple modifier classes to an element, useful when a variant requires several distinct CSS classes. ```twig {% set variants = variant|split('__')|map(v => v|replace({(v): 'progress-bar-' ~ v})) %} {% set attributes = attributes.addClass(variants) %} ``` -------------------------------- ### Apply Variant Modifier Class in Twig Template (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices This Twig code shows how to dynamically add a modifier class to a component's root element based on the selected variant. This allows for styling variations. ```twig {% set attributes = variant ? attributes.addClass('btn-group--' ~ variant) : attributes %} ``` -------------------------------- ### Safe Looping in Twig Templates Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Demonstrates a safe way to loop through slides in Twig templates by ensuring the 'slides' variable is treated as a sequence, even if it contains a single item. This prevents errors when 'slides' is not a list. ```twig {{ title}} ``` -------------------------------- ### Set Default Variant with Conditional Class in Twig (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices This Twig code ensures a component renders correctly with a default appearance when no variant is specified or when the 'default' variant is selected. It conditionally applies a modifier class. ```twig {% set attributes = variant and (variant != 'default') ? attributes.addClass('btn-group--' ~ variant) : variant %} ``` -------------------------------- ### Applying Drupal Attributes to Nested Elements Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Illustrates using the Drupal `attributes` object to add classes to nested elements within a component, such as the text container in a quote component. This ensures proper styling and structure. ```twig
{{ value }}
``` -------------------------------- ### SDC Component Status Enum Definition Source: https://project.pages.drupalcode.org/ui_patterns/faq This YAML snippet shows the definition of the 'status' enum for SDC components. This property is used to indicate the development status of a component (experimental, stable, deprecated, obsolete) and is displayed in the component library. ```yaml status: type: string title: Development Status description: Use this property to identify if the component can be safely used in production. enum: - experimental - stable - deprecated - obsolete ``` -------------------------------- ### Using Drupal Attributes for Component Wrappers Source: https://project.pages.drupalcode.org/ui_patterns/2-authors/2-best-practices Shows how to correctly use the Drupal `attributes` object within a Twig template to add classes and attributes to the component's wrapper element. This is essential for integration with various Drupal modules. ```twig {% if icon_attributes %}{% endif %} {{ label }} ``` -------------------------------- ### Include Drupal Component in Presenter Template (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/1-users/5-presenter-templates Demonstrates how a presenter template uses Twig's include() function to render a menu component, passing transformed data and attributes. This approach separates logic from markup, keeping presenter templates clean. ```twig {{ include('my_theme:menu', { 'items': items, 'attributes': attributes.addClass('bg-primary'), }, with_context=false)}} ``` -------------------------------- ### Clean Drupal Node Template (Twig) Source: https://project.pages.drupalcode.org/ui_patterns/1-users/5-presenter-templates Shows a simplified Drupal node template that primarily renders the 'content' variable within optional wrapper attributes. This approach removes legacy markup and promotes cleaner template files, pushing complexity to other plugins. ```twig {% if attributes.storage() %} {% endif %} {{ content }} {% if attributes.storage() %} {% endif %} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.