Many times people want to display an item list in multiple columns. This is not terribly difficult, but why should we always re-invent the wheel? Hopefully someone can roll this as a proper patch.

/**
 * Returns HTML for a list as a multi-column table.
 * 
 * @param $variables
 *   An associative array containing:
 *   - columns: The number of columns desired.
 *   - items: an array of items. Generally a simple list, but may be any data
 *      recognized by theme_table.
 *   - empty: The message to display if list does not have any rows.
 *
 * Example:
 * @code
 *    $list = array_map('filter_xss', array_values(module_list(FALSE, FALSE, TRUE)));
 *    echo theme('list_columns', array('items' => $list, 'columns' => 5, 'empty' => t('The list is empty')));
 * @endcode
 */
function theme_list_columns($variables) {
  $cols = $variables['columns'];
  $list = $variables['items'];
  $addvals = count($list) % $cols;
  for ($i = 1; $i < $addvals; ++$i) {
    $list[] = " ";
  }
  $count = count($list);
  $len = $count / $cols;
  $rows = array();
  for ($i = 0; $i < $len; ++$i) {
    $row = array();
    for ($j = $i; $j < $count; $j += $len + 1) {
      $row[] = $list[$j];
    }
    $rows[] = $row;
  }
  return theme('table', array('rows' => $rows, 'empty' => $variables['empty']));
}

Since this is new function, I don't see why it can't be at least back-ported to 7.x.

Comments

droplet’s picture

-1

item list and using table ??

you can do theme_table directly...

NancyDru’s picture

If you use theme_table, then any overrides the user has would be skipped.

droplet’s picture

Sorry. can you explain more about "any overrides the user has would be skipped."

NancyDru’s picture

Any user can implement their own function to replace theme('table'...) but using theme_table would skip that functionality.

joelpittet’s picture

Status: Active » Closed (won't fix)

I think table template suggestions may do the trick here for both concerns.

'table__list_columns' with a table--list-columns.html.twig template suggestion override.

Since we are in RC this is unlikely to make it into D8. Though if you think this really has some strong use-cases maybe worth creating a contrib or sandbox module with help RenderElement/theme implementations and see if it get's the uptake and at least use it in your own projects and can share with others that way.

Triaging remaining theme system issues since we are in RC.

NancyDru’s picture

Status: Closed (won't fix) » Active

As a new function, it does not alter interfaces. There is no reason it can't be considered for D8.

joelpittet’s picture

Version: 8.0.x-dev » 8.1.x-dev
Issue tags: +Twig

Reason I closed it was because there was no work done on it in the last 4 years. There doesn't seem to be a need for this.

This will not get in 8.0.x so bumping but I'd rather not keep feature requests open if there is not a solid use-case. Like I said this could be done in contrib if you feel it could get some traction.

joelpittet’s picture

@NancyDru Regarding your assertions:

I don't see why it can't be at least back-ported to 7.x.

As a new function, it does not alter interfaces. There is no reason it can't be considered for D8.

  1. What is the use-case and rational for including this in core?
  2. Do you use this often?
  3. Have other users asked you for having this theme function?
  4. Are there any modules in D7 that are using this now that would benefit from this being in core?

It doesn't seem pragmatic to include this in core unless it will have a wide benefit. And contrib may be better playground for including extra stuff otherwise it just becomes more maintenance cruft.

joelpittet’s picture

Title: Theme_list_columns » Add theme_list_columns()
Status: Active » Postponed (maintainer needs more info)

And I should set the status until we can get answers for #8

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

Closing since no response after 7 months. Feel free to re-open if you can answer the questions in #8