DEV Community

Anders Grendstadbakk
Anders Grendstadbakk

Posted on • Originally published at andeers.com

Disable row in Drupal tableselect

When you use the tableselect element there may be reasons you want to make a row not selectable:

  • Permissions
  • Missing properties on the element
  • Or any other reason to not make a row selectable

I had to do this recently and thought it should be a easy thing to do, since all form elements seems to support the #disabled property.

A tableselect element looks something like this:

$form['table'] = [
  '#type' => 'tableselect',
  '#multiple' => FALSE,
  '#header' => [
    'name' => 'Name',
    'age' => 'Age',
  ],
  '#options' => $options,
  '#empty' => 'No items available',
];

And then each entry (in $options) would look like:

$options['anders'] = [ // knowing the key is important for disabling.
  'name' => [
    '#markup' => 'Anders',
  ],
  'age' => [
    '#markup' => 30,
  ],
];

You would think the way to disable a row is to just add '#disabled' => TRUE; to the option entry, but no, nothing happens.

To actually disable a row you would need to target the same key outside the #options array. Like this:

$form['table']['anders']['#disabled'] = TRUE; // Added directly to tableselect element.

In my opinion this is quite confusing.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay