Change record status: 
Project: 
Introduced in branch: 
main, 12.x, 11.x
Introduced in version: 
12.0.0, 11.6.0
Description: 

Single-directory components (SDC) can replace another component with the replaces key in their *.component.yml file. The replacing component must have a props schema that is compatible with the original component's schema.

Until now, both components had to declare props. If the original component did not declare any props, it could not be replaced by a component that does. The only way around this was to edit the original component to add an empty props object.

Now, a component without props is treated as having an empty object schema:

props:
type: object
properties: []

This means it can be replaced by a component that declares optional props. The usual compatibility rules still apply, so a replacing component cannot add required props.

Example

Original component, my_theme:card, has no props:

# my_theme/components/card/card.component.yml
name: Card
status: stable
slots:
  content:
    title: Content

A replacing component can now add optional props:

# my_module/components/card/card.component.yml
name: Card
status: stable
replaces: my_theme:card
props:
  type: object
  properties:
    heading:
      type: string
      title: Heading
slots:
  content:
    title: Content

The template of the replacing component should handle a missing value, because existing render arrays and templates that use my_theme:card do not pass the new prop:

{% if heading %}<h2>{{ heading }}</h2>{% endif %}
{{ content }}

This replacement is still rejected with \Drupal\Core\Render\Component\Exception\IncompatibleComponentSchema, because heading is required:

replaces: my_theme:card
props:
  type: object
  required:
    - heading
  properties:
    heading:
      type: string

Replacing components must still declare props

The replacing component must still declare props. When it doesn't, the exception message has changed:

- Before: Component "X" is attempting to replace "Y", however component replacement requires both components to have schema definitions.
- After: Component "X" is attempting to replace "Y", however component replacement requires the replacing component to have a schema definition.

Code or tests that check for the old message need to be updated.

Impacts: 
Module developers
Themers