Property plugin sets value to index of selected option instead of value

Created on 21 February 2025, 5 months ago

Problem/Motivation

When I choose a style option of the plugin type "property", the template variable includes the index of my chosen option instead of the value of that option.

Instead, I expect the template variable to be set to the value of the chosen option.

Steps to reproduce

Configure style options:

options:
  hide_title:
    plugin: property
    label: Hide title
    options:
      - label: 'No - always show the title'
      - label: 'Yes - show the title for screen readers only'
        value: true

contexts:
  paragraphs:
    card_grid:
      options:
        hide_title: true

Create a page with a paragraph with a type with this style option and set it to 'Yes'.

The result is my template has a variable

$variables['elements']['#hide_title'] = 1

I expect instead for the template variable to have the value true

Proposed resolution

Property::build() has:

    $value = $this->getValue('property') ?? NULL;
    $option_definition = $this->getConfiguration('options');
    if (is_array($value)) {
      $property = implode(' ',
        array_map(function ($index) use ($option_definition) {
          return $option_definition[$index]['value'] ?? NULL;
        }, $value)
      );
    }
    else {
      $property = $value ?? NULL;
    }

Since there is no example for the property plugin ( πŸ’¬ Add property plugin example usage Active ), it isn't clear when we should expect $value to be an array. In my example, $value is set to the index of my chosen option.

In order for the template variable to contain the value of my option, the else statement should be:

    else {
      $property = $option_definition[$value]['value'] ?? NULL;
    }

Is this a bug, or is there another structure I should use in my definition?

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

1.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States calebtr

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @calebtr
  • Merge request !301.1.x β†’ (Open) created by calebtr
  • πŸ‡ΊπŸ‡ΈUnited States calebtr

    I suppose the issue might actually be that Property::getValue() is returning the index instead of actual value. This would make πŸ› values are not being set Active a related issue also. It seems like all of the plugins could need refactoring if getValue() was returning an actual value.

    I appreciate some direction from module maintainers.

  • Status changed to Needs review 17 days ago
  • πŸ‡¨πŸ‡·Costa Rica alemadlei

    The MR works for me.

Production build 0.71.5 2024