1

I'm making a table form

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

In getOutput() I have 'vehicleName' => "<a href=\"/vehicle?id=" . $vehicle->vehicleId . "\">" . $vehicle->vehicleName . "</a>", for one of the fields. And it doesn't print out as normal html. It goes through htmlspecialchars() and I don't have clickable link at the end. How can I disable this?

1

You can use FormattableMarkup

Here's a simple example to get you started

use Drupal\Component\Render\FormattableMarkup; // place this at top of file

$options = [
  1 => ['foo' => 'Foo'],
  2 => ['foo' => 'Bar'],
  3 => ['foo' => new FormattableMarkup('<a href="http://example.com">I am a link</a>', [])],
];

$header = [
  'foo' => t('Foo bar')
];

$form['table'] = [
  '#type' => 'tableselect',
  '#title' => t('Foo bar table'),
  '#header' => $header,
  '#options' => $options,
  '#empty' => t('No foos found.'),
];

So in your case $this->getOutput() may return something like (this is untested):

['vehicleName' =>  new FormattableMarkup(
  '<a href="/vehicle?id=@id">@name</a>', 
  ['@id' => $vehicle->vehicleId, '@name' => $vehicle->vehicleName]
)]

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.