Table with empty cell for children value type form element

Created on 20 February 2019, about 6 years ago
Updated 31 January 2023, about 2 years ago

When a table form has children form elements of type value, it causes that empty cells be rendered, breaking the table layout.

A similar issue was reported (also mentioned in a TODO in code of Table form element) here: https://www.drupal.org/node/1248940

I attach a patch to be reviewed.

Example code, and screenshots of the output generated Before and After of the fix

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['items'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Phrase'),
        $this
          ->t('Book'),
      ],
    ];

    foreach ($this->phrasesManager->getAllPhrases() as $tid => $phrase) {
      $form['items'][$tid]['text'] = [
        '#type' => 'textfield',
        '#title' => 'Phrase',
        '#title_display' => 'invisible',
        '#default_value' => $phrase['name'],
      ];

      $form['items'][$tid]['id'] = [
        '#type' => 'value',
        '#value' => $tid
      ];

      $form['items'][$tid]['is_phrase'] = [
        '#type' => 'value',
        '#value' => 1
      ];

      $form['items'][$tid]['some_value'] = [
        '#type' => 'value',
        '#value' => 200486
      ];

      $form['items'][$tid]['author'] = [
        '#type' => 'textfield',
        '#title' => 'Author',
        '#title_display' => 'invisible',
        '#default_value' => $phrase['author'],
      ];
    }

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save')
    ];
    
    return $form;
  }
🐛 Bug report
Status

Needs work

Version

9.5

Component
Theme 

Last updated 1 day ago

Created by

🇦🇷Argentina matiasmiranda Yerba Buena

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.

Production build 0.71.5 2024