media_library element inside webform_multiple error

Created on 29 August 2024, 4 months ago

Problem/Motivation

When media_library element is added inside webform_multiple, it throughs error.
Warning: Array to string conversion in /var/www/web/modules/contrib/media_library_form_element/src/Plugin/WebformElement/MediaLibrary.php on line 112

Steps to reproduce

Creates a custom @WebformElement extends WebformElementBase
under public function form, add

$form['images'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Image options'),
    ];

    $form['images']['media'] = [
      '#type' => 'webform_multiple',
      '#element' => [
        'image_label' => [
          '#type' => 'textfield',
          '#title' => $this->t('Label'),
        ],
        'media_list' => [
          '#type' => 'media_library',
          '#allowed_bundles' => ['image'],
          '#title' => $this->t('Image'),
          '#cardinality' => 1,
        ],
      ],
      '#add_more_button_label' => $this->t('Add more'),
      '#add_more_input' => FALSE,
    ];

Proposed resolution

Replace line with

$element['#default_value'] = $element['#default_value']['media_library_selection'] ?? implode(',', $element['#default_value']);

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇵🇰Pakistan monymirza Islamabad

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

Comments & Activities

Production build 0.71.5 2024