在Drupal 8中,渲染Tables仍然很像Drupal7。您在PHP中构建了行和列的多维数组,然后将Drupal 分别转换为a <tr>和<td>s。仍然存在令人困惑的Drupalism,'data'它使您可以将渲染数组元素添加为单元格数据(不要与数据属性相混淆)。
我得到了一个站点,开发人员选择使用“数据”来渲染单元格的内容,但是我无法弄清楚如何<td>在数据周围添加类。
我已经阅读了Table.php的源代码和文档,并且知道新版本,#wrapper_attributes  但是我无法破解。
我尝试了至少四种添加类的方法,但均无效果。
$table['row-' . $row_id] = [
  // Option 1: Class appears on <tr> tag
  '#attributes' => [
    'class' => ['option-1-row-attributes'],
    'id' => 'row-' . $row_id,
    'no_striping' => TRUE,
  ],
  // Option 2: Class appears on <td> tag of first column. 
  'item' => [
    '#markup' => $row['my_item']->label(),
    '#wrapper_attributes' => [   
      'class' => ['option-2-markup-wrapper-attributes'],
    ],
  ],
  // In the following section, the only item that works is
  // the class on the <a> tag.
  'edit_operation' => [
    'data' => [
      '#type' => 'link',
      '#url' => Url::fromRoute('my_module.my_route', ['item' => $row_id]),
      '#title' => $this->t('Edit'),
      '#attributes' => [
        // Option 3: Class appears on the anchor tag
        'class' => ['use-ajax', 'option-3-link-attributes'],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
      // Option 4: Has no effect.
      '#wrapper_attributes' => [
        'class' => ['option-4-data-wrapper-attributes'],
      ],
    ],
    // Option 5: Update: This appears to be the correct solution! 
    // Class appears on the <td>.
    '#wrapper_attributes' => [
      'class' => ['option-5-wrapper-attributes'],
    ],
    // Option 6: Has no effect.
    '#attributes' => [
      'class' => ['option-6-attributes'],
    ],
    // Option 7: Has no effect.
    'class' => ['option-7-attributes'],
  ],
];