Handle default values of nested elements

Created on 18 February 2021, over 3 years ago
Updated 24 April 2024, 2 months ago

Problem/Motivation

Drupal\multivalue_form_element\Element\MultiValue::setDefaultValue does not support nested elements, eg. a container element with other elements inside.

Steps to reproduce

Example form structure:

$form['custom'] = [
    '#type' => 'multivalue',
    '#title' => 'Custom sources',
    '#description' => 'A space-separated list of additional sources to allow.',
    '#default_value' => array_map(
        static function (array $source) { return ['container' => $source]; },
        $this->service->getSources($directive)
    ),
];
$form['custom']['container'] = [
    '#type' => 'container',
    '#attributes' => ['class' => ['form-items-inline']],
    '#tree' => false,
];
$form['custom']['container']['source'] = [
    '#type' => 'textfield',
    '#title' => 'Source to allow',
    '#size' => 50,
];
$form['custom']['container']['comment'] = [
    '#type' => 'textfield',
    '#title' => 'Comment',
    '#size' => 50,
];
🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇧🇪Belgium DieterHolvoet Brussels

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to RTBC over 1 year ago
  • 🇩🇪Germany Atropoides Hamburg

    Tested with version 1.0.0-beta5. Worked great for me, thanks for the solution.

  • 🇧🇷Brazil adinancenci

    So, patch #5 have fixed some occurrences in one of our sites but in other forms it had the exact opposite effect, causing the bug to occur where once it was working just fine.

    I tweaked patch #5, it worked for us. ( tested with 1.0.0-beta4 ).

  • 🇧🇷Brazil adinancenci

    Damn... can someone please rename the patch above to reflect the comment ?

  • 🇪🇸Spain psf_ Huelva

    #9 tested with version 1.0.0-beta6. Work for me.

    In buildForm() I have:

    $form['legal_links'] = [
          '#type' => 'multivalue',
          '#title' => $this->t('Legal links'),
          '#description' => $this->t('Links to legal documents in the enrollment form. Leave empty a row to remove it.'),
          '#cardinality' => MultiValue::CARDINALITY_UNLIMITED,
          '#default_value' => $links,
          'link_title' => [
            '#type' => 'textfield',
            '#title' => $this->t('Title'),
            '#description' => $this->t('To include a link, please provide a title as well.'),
          ],
          'link' => [
            '#type' => 'textfield',
            '#title' => $this->t('Link'),
            '#maxlength' => 2048,
            '#element_validate' => [[static::class, 'validateUriElement']],
            '#link_type' => LinkItemInterface::LINK_GENERIC,
            '#description' => $this->t(
              'You can enter an internal path such as %add-node or an external URL such as %url. Enter %front to link to the front page. Enter %nolink to display link text only. Enter %button to display keyboard-accessible link text only.',
              [
                '%front' => '<front>',
                '%add-node' => '/node/add',
                '%url' => 'http://example.com',
                '%nolink' => '<nolink>',
                '%button' => '<button>',
              ]
            ),
          ],
        ];
    

    And to save, in submitForm():

    $this->config('mymodule.settings')
          ->set('legal_links', $form_state->getValue('legal_links'))
          ->save();
    
  • Status changed to Needs work 7 months ago
  • 🇮🇹Italy sardara

    Test coverage has to be added.
    Off the top of my head, I think I didn't add support for nested elements because of the possibility of having #tree set to true or false, making it hard to handle complex cases. Please test that scenario when adding tests.

  • Status changed to Needs review 7 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 7 months ago
    12 pass
  • 🇵🇰Pakistan monymirza Islamabad

    text_format elements added along this.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    12 pass
  • Status changed to Needs work 2 months ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Setting to NW as maintainer has asked for tests.

Production build 0.69.0 2024