1

I have some nodes in my site

Day         Color   Type    Value
01-01-2018  blue    A       4
02-01-2018  yellow  B       3
02-01-2018  orange  C       2
03-01-2018  red     B       8

I need to show this data in the following way (NOT USING VIEWS):

enter image description here

Does anyone knows if it's possible using the table theme in Drupal? I don't think it's possible to have the double header either.

Is there any other way to achieve this?

1

You'll have to flatten the data yourself, as you're grouping by date (you'd have to regardless of the double header and colspans), but yes, theme_table can accommodate that.

I haven't tested this but it should be at least near to working. The key points to note are colspan, which does what you'd think, and header, which will mark a cell to be rendered as <th> rather than <td>. Meaning you can stuff the second row of headers into the body. Not ideal, as they'll be inside a <tbody>, but I suspect good enough.

$header = [
  [
    'data' => '',
    'colspan' => 2,
  ],
  [
    'data' => 'Type',
    'colspan' => 3,
  ],
];

$rows = [
  [
    [
      'data' => 'Day',
      'header' => TRUE,
    ],
    // Other header rows here, with 'header' => TRUE
  ]
];

// Loop through your grouped data
foreach ($data as $item) {
  $rows[] = [
    $item->date,
    $item->colours,
    $item->type_a,
    $item->type_b,
    $item->type_c,
  ];
}

$build = [
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.