3

I can't get form-table of '#type' => 'tableselect' to work. I've lifted the example from 8.3.x core/lib/Drupal/Core/Render/Element/Tableselect.php Tableselect. The table works and there are no log messages.

What happens is the page reports that there are no users found (ie. the table is empty) even though the $form array has the row data in it.

If I change the type to '#type' => 'table' the table is populated as expected.

This is the code I am using.

namespace Drupal\simpleForm\Controller;
use Drupal\Core\Controller\ControllerBase;

class DisplayTable_1 extends ControllerBase
{

    public function description_one()
    {
        $header = [
            'first_name' => $this->t('First Name'),
            'last_name' => $this->t('Last Name'),
        ];

        $options = [
            1 => ['first_name' => 'Indy', 'last_name' => 'Jones'],
            2 => ['first_name' => 'Darth', 'last_name' => 'Vader'],
            3 => ['first_name' => 'Super', 'last_name' => 'Man'],
        ];

        $form['table'] = array(
            '#type' => 'tableselect',
            '#header' => $header,
            '#options' => $options,
            '#empty' => $this->t('No users found!!!'),
        );

        return $form;
    }

}

Any help would be appreciated.

1
  • Clearing cache makes no difference.
    – Mike
    Commented Mar 1, 2017 at 20:56

2 Answers 2

2

Found the issue: the #type' => 'tableselect has to be built inside a class that extends FormBase.

This Works:

  class DisplayTable_1 extends FormBase {
    public function buildForm(array $form, FormStateInterface $form_state)
    {
        $options = [
            1 => ['first_name' => 'Indy', 'last_name' => 'Jones'],
            2 => ['first_name' => 'Darth', 'last_name' => 'Vader'],
            3 => ['first_name' => 'Super', 'last_name' => 'Man'],
        ];

        $header = [
            'first_name' => t('First Name'),
            'last_name' => t('Last Name'),
        ];

        $form['table'] = [
            '#type' => 'tableselect',
            '#title' => $this->t('Users'),
            '#header' => $header,
            '#options' => $options,
            '#empty' => t('No users found'),
        ];

        // Add a submit button that handles the submission of the form.
        $form['actions']['submit'] = [
            '#type' => 'submit',
            '#value' => $this->t('Submit'),
            '#description' => $this->t('Submit, #type = submit'),
        ];
        return $form;
    }

    public function getFormId() {
        return 'fapi_example_input_demo_form';
    }

    public function submitForm(array &$form, FormStateInterface $form_state) {
        // Find out what was submitted.
        $values = $form_state->getValues();
        foreach ($values as $key => $value) {
            $label = isset($form[$key]['#title']) ? $form[$key]['#title'] : $key;

            // Many arrays return 0 for unselected values so lets filter that out.
            if (is_array($value)) {
                $value = array_filter($value);
            }

            // Only display for controls that have titles and values.
            if ($value && $label) {
                $display_value = is_array($value) ? preg_replace('/[\n\r\s]+/', ' ', print_r($value, 1)) : $value;
                $message = $this->t('Value for %title: %value', array('%title' => $label, '%value' => $display_value));
                drupal_set_message($message);
            }
        }
    }
}
2
  • Yes, ControllerBase would not provide any form building capabilities.
    – Kevin
    Commented Mar 3, 2017 at 20:34
  • Any way to use tableselect outside the form?
    – MiSHuTka
    Commented May 29, 2017 at 13:35
0

try

$header = [
  'first_name' => ['data' => $this->t('First Name')],
  'last_name' => ['data' => $this->t('Last Name')],
   ...
 ];

$options = [
    1 => ['first_name' => ['data' => ['#title' => 'Indy']], 'last_name' => ['data' => ['#title' => 'Jones']],
    ...
];

I checked the code over here.

1
  • Remy, I'm afraid that did not work.
    – Mike
    Commented Mar 2, 2017 at 13:23

Your Answer

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

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