Add a class to a Drupal table cell containing ['data'] In Drupal 8,  rendering Tables is still a lot like Drupal 7.  You build out  multi-dimensional arrays of rows and columns in PHP that Drupal transforms into a &lt;tr&gt; and &lt;td&gt;s respectively.  There is still this confusing Drupalism known as 'data' that lets you add render array elements as cell data (not to be confused with data attributes).

I've been given a site where the developer chose to use 'data' to render the contents of the cell, but I cannot figure out how to add a class to the &lt;td&gt; around the data.

I've read the source code and documentation for Table.php and I'm aware of the new #wrapper_attributes  but I cannot crack this.

I've tried at least four ways to add the class, and none work.

$table['row-' . $row_id] = [

  // Option 1: Class appears on &lt;tr&gt; tag
  '#attributes' =&gt; [
    'class' =&gt; ['option-1-row-attributes'],
    'id' =&gt; 'row-' . $row_id,
    'no_striping' =&gt; TRUE,
  ],

  // Option 2: Class appears on &lt;td&gt; tag of first column. 
  'item' =&gt; [
    '#markup' =&gt; $row['my_item']-&gt;label(),
    '#wrapper_attributes' =&gt; [   
      'class' =&gt; ['option-2-markup-wrapper-attributes'],
    ],
  ],

  // In the following section, the only item that works is
  // the class on the &lt;a&gt; tag.
  'edit_operation' =&gt; [
    'data' =&gt; [
      '#type' =&gt; 'link',
      '#url' =&gt; Url::fromRoute('my_module.my_route', ['item' =&gt; $row_id]),
      '#title' =&gt; $this-&gt;t('Edit'),
      '#attributes' =&gt; [
        // Option 3: Class appears on the anchor tag
        'class' =&gt; ['use-ajax', 'option-3-link-attributes'],
        'data-dialog-type' =&gt; 'modal',
        'data-dialog-options' =&gt; Json::encode([
          'width' =&gt; 700,
        ]),
      ],
      // Option 4: Has no effect.
      '#wrapper_attributes' =&gt; [
        'class' =&gt; ['option-4-data-wrapper-attributes'],
      ],
    ],
    // Option 5: Update: This appears to be the correct solution! 
    // Class appears on the &lt;td&gt;.
    '#wrapper_attributes' =&gt; [
      'class' =&gt; ['option-5-wrapper-attributes'],
    ],
    // Option 6: Has no effect.
    '#attributes' =&gt; [
      'class' =&gt; ['option-6-attributes'],
    ],
    // Option 7: Has no effect.
    'class' =&gt; ['option-7-attributes'],
  ],
];
After writing out the question in general terms, I've gone back to test again, and have determined that Option 5 in the OP with '#wrapper_attributes' at the same level of the 'data' element does work.  I believe Drupal 8 was aggressively caching the table, because my changes were not showing up even after a drush cr.  

The rules for adding classes to tables via backend PHP is:


Table class requires #attributes.
TR row class inside TBODY requires #attributes.
TD cell class inside TBODY requires #wrapper_attributes.
TR row class inside THEAD/TFOOT requires 'class' and 'data' containers.  Neither #attributes nor #wrapper_attributes work here.
TH/TD cell class inside THEAD/TFOOT requires 'class' and 'data' containers.  Neither #attributes nor #wrapper_attributes work here. 
There is no way to add a class directly to a &lt;thead&gt; or &lt;tfoot&gt; tag without overriding a twig template.


Here is the most common example for adding classes to the &lt;tr&gt; &amp; &lt;td&gt; tags inside the main &lt;tbody&gt;, as well as to the main &lt;table&gt; tag itself:

$table = [
  '#type' =&gt; 'table',
  '#attributes' =&gt; [
    'class' =&gt; ['table-class'],
  ],
  'row1' =&gt; [
    '#attributes' =&gt; [
      'class' =&gt; ['tr-class'],
    ],
    // Table data cell using 'data' for renderable elements.
    'column1' =&gt; [
      'data' =&gt; [
        '#type' =&gt; 'link', // Or any other renderable thing.
        '#attributes' =&gt; [
          'class' =&gt; ['link-class'],
        ],
        // Other elements required to render the link go here...
      ],
      '#wrapper_attributes' =&gt; [ // Watch out!
        'class' =&gt; ['td-class'],
      ],
    ],
    // Table data cell using '#markup'.
    'column2' =&gt; [
      '#markup' =&gt; '&lt;span&gt;' . $this-&gt;t('text') . '&lt;/span&gt;',
      '#wrapper_attributes' =&gt; [   
        'class' =&gt; ['td-class'],
      ],
    ],
  ],
];


Note that the 'class' container will accept either a string or an array, but I suggest always using an array. 

From here, the story gets more complicated.  If you need to add classes to the TR or TD/TH tags inside a THEAD/TFOOT area, the rules change completely.  Neither #attributes nor #wrapper_attributes work inside #header and #footer sections and trying to use them produces very strange effects. 

The bare minimum structure for tables with header/footer data columns in Drupal 8 is this:

$table = [
  '#type' =&gt; 'table',
  // Produces &lt;thead&gt; &lt;tr&gt; &lt;th&gt;
  '#header' =&gt; [
    'Header 1',
    'Header 2',
    'Header 3',
  ],
  // Produces &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;
  'row1' =&gt; [
    'Body 1',
    'Body 2',
    'Body 3',
  ],
  // Produces &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;
  '#footer' =&gt; [
    'Footer 1',
    'Footer 2',
    'Footer 3',
  ],
];


You must change the actual structure of the data and introduce two levels of additional multi-dimensional arrays, in order to leverage the 'class' array index which requires then also introducing the 'data' array index.  This applies both to the row element and the data cell elements, as seen in the following example:

$table = [
  '#type' =&gt; 'table',
  // This example works the same way for '#footer'.
  '#header' =&gt; [
    // First, introduce an extra level to the array to provide a
    // place to store the class attribute on the &lt;tr&gt; element inside
    // the &lt;thead&gt;.
    [
      'class' =&gt; 'thead-tr-class',
      // Next place the columns inside a 'data' container, so that
      // the 'class' can be used.  '#wrapper_attributes' will not
      // work here.
      'data' =&gt; [
        // The following line produces data inside a &lt;th&gt;
        // without any class.
        'Header 1',

        // The following lines produce data inside a &lt;th&gt;
        // with a class: th-class.
        [
           'class' =&gt; 'th-class',
           'data' =&gt; 'Header 2',
           'colspan' =&gt; 2
        ],
      ],
    ],
  ],
];


The above example #header example produces:

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr class="thead-tr-class"&gt;
      &lt;th&gt;Header 1&lt;/th&gt;
      &lt;th class="th-class" colspan="2"&gt;Header 2&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
&lt;/table&gt;