I'm sure this is something simple, but I'm starting to transition our D7 custom apps, mostly CRUD, to D8.4 and am having trouble grasping how to pass a Link object correctly in a table theme.

Given that I have a route, I'm opting to use the Link::createFromRoute() method. In many of my modules, I'm trying to get an array of rows themed so that we see the data formatted and an operations link to edit that record. At this moment, I only have a Controller for the Index and a Form for the Create and Edit. Looking on the web, I started using this form: https://valuebound.com/resources/blog/how-to-create-custom-form-crud-cre...

and checked out the comments about the deprecated l() function. Searching some more I'm seeing https://www.drupal.org/node/2346779 

OK, I've tried all of these with poor results. The one closest to working is this:

$rows=array();
foreach ($result as $row) {
$edit = Link::createFromRoute('Edit', 'my_feature.my_feature_form', array('id' => $row->id));
$rows[] = ['data' => array($row->name, $edit->toString())];

}

$build['tablesort_table'] = [
'#markup' => "Add new feature",
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
return $build;

I get back a themed table that returns the html for the Edit link 

<a href="/my_feature/form?id=1">Edit</a>

instead of the actual link.

Looking at the Link class at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Link.php/... It seems I could use

$edit->toRenderable() instead. But there is no output of that method.

At this point, there is no .twig file because I'm 90 percent there on this simple page template. In D7 I would return theme($build) and be done. What am I overlooking?, sam

Comments

Jaypan’s picture

You'll need to use FormattableMarkup:

use Drupal\Component\Render\FormattableMarkup;

$link = new FormattableMarkup('<a href=":url">Edit</a>', [':url' => '/my_feature/form?id=1');
$row[] = $link;