API page: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Ta...

Here's how to do it:

      $row['name'] = [
        '#plain_text' => $field_name,
        '#wrapper_attributes' => ['colspan' => 2],
      ];
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim created an issue. See original summary.

gianani’s picture

Assigned: Unassigned » gianani
gianani’s picture

Status: Active » Needs review
FileSize
689 bytes

Adding Patch.

Added row attributes and cell attributes example usage.

for ($i=1; $i<=4; $i++) {
    $form['contacts'][$i]['#attributes'] = array('class' => array('foo', 'baz'));
    $form['contacts'][$i]['name'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
      '#title_display' => 'invisible',
      '#wrapper_attributes' => array('colspan' => 2, 'class' => array('foo', 'bar')),
    );
 
    $form['contacts'][$i]['phone'] = array(
      '#type' => 'tel',
      '#title' => $this->t('Phone'),
      '#title_display' => 'invisible',
    );
  }
shashikant_chauhan’s picture

Status: Needs review » Needs work

After applying the patch, name cell will cover both columns ('colspan' => 2) and phone cell will be shown outside.
This can be improved by adding more appropriate usage example.

gianani’s picture

Status: Needs work » Needs review
FileSize
928 bytes

Adding new patch with more suitable example.

for ($i = 1; $i <= 4; $i++) {
  $form['contacts'][$i]['#attributes'] = array('class' => array('foo', 'baz'));
  $form['contacts'][$i]['name'] = array(
    '#type' => 'textfield',
    '#title' => $this->t('Name'),
    '#title_display' => 'invisible',
  );

  $form['contacts'][$i]['phone'] = array(
    '#type' => 'tel',
    '#title' => $this->t('Phone'),
    '#title_display' => 'invisible',
  );
}

$form['contacts'][]['colspan_example'] = array(
  '#plain_text' => 'Colspan Example',
  '#wrapper_attributes' => array('colspan' => 2, 'class' => array('foo', 'bar')),
);
shashikant_chauhan’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
22.44 KB

Table example
Looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed af89f80 to 8.3.x and 9f1e31d to 8.2.x. Thanks!

  • alexpott committed af89f80 on 8.3.x
    Issue #2803923 by gianani, shashikant_chauhan, joachim: Table render...

  • alexpott committed 9f1e31d on 8.2.x
    Issue #2803923 by gianani, shashikant_chauhan, joachim: Table render...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.