1

I have a field widget that has multiple values per field and some are values or other form types that do not render any visible data. I want to use the table element to encapsulate these fields into proper structure.

The issue I have is that for each field within the "field"(ie. table row) a table cell is created, even for values, which resutls in empty <td></td>. Sure, I can use the colspan on headers but that also requires adding some css styling to hide those empty cells to avoid padding/margin offsets from those empty cells.

So I am wondering if it is possible to somehow tell the row which fields to put together into one cell or maybe render them outside of the table itself. I could use a custom template but I need to use the tabledrag so I don't want to reinvent the wheel.

I also don't want to alter the structure of those fields since it is very important the structure stays the same. although I could probably fiddle with array parents, I'd rather not.

So ss there a simple way I can get rid of those empty cells that hold values and hidden fields?

-- I have found an old Drupal issue when I was going through the Table code https://www.drupal.org/node/1248940

1 Answer 1

0

I've just implemented it using a theme preprocessor to alter the tag from <td> to <span> and I've added the hidden and aria-hidden attributes. Hidden by itself works but it isn't supported by < IE10.

So as to prevent the loops on every table I've added a class to the table as well so I'm checking for 'form-table' first.

/* Implements MODULE_preprocess_HOOK */
function mymodule_preprocess_table(&$variables) {
  if ( ! in_array('form-table', $variables['attributes']['class']) )
    return;

  // Iterate the variables looking for form elements that have the type hidden
  foreach ($variables['rows'] as $key => &$row ) {
    foreach ( $row['cells'] as &$cell ) {
      if ( $cell['content']['#type'] == 'hidden' ) {
        $cell['attributes']['hidden'] = TRUE;
        $cell['attributes']['aria-hidden'] = 'true';
        $cell['tag'] = 'span';
      }
    }
  }
}
2
  • Yeah, I guess a preprocessor is needed in this case. Even though it's not ideal.
    – user21641
    Commented Oct 27, 2015 at 7:20
  • It's a lighter approach in terms of lines of code than rewriting the twig file. Actually it would be difficult in twig as attributes is all wrapped up by then. I couldn't see a simpler way. Commented Oct 27, 2015 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.