2

The following code is returning html in plain text instead of link.for example its printing Edit slides intead of converting in to link.

$editslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/editslides');
      $editslides_link = (Link::fromTextAndUrl('Edit slides', $editslides)->toString());
      $exportslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/export');
      $exportslides_link =  (Link::fromTextAndUrl('Export slides', $exportslides)->toString());
      $deleteslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/delete');
      $deleteslides_link =  (Link::fromTextAndUrl('Delete', $deleteslides)->toString());
      $tmp[] =  $editslides_link .' ,' .$exportslides_link .' ,' .$deleteslides_link;

Though if i don't append string in $tmp[] code render the links. For example

  $tmp[] =  $editslides_link;

The complete code is as below

  public function dexp_layerslider_list() {
    $header = [
      'ID',
      'Name',
      'Action',
    ];
    $results = \Drupal::database->select('dexp_layerslider', 'd')->fields('d', [
      'id',
      'name',
    ])->execute();
    foreach ($results as $row) {
      $url = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/edit');
      $row->name = (Link::fromTextAndUrl($row->name, $url)->toString());
      $tmp = (array) $row;
      $editslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/editslides');
      $editslides_link = (Link::fromTextAndUrl('Edit slides', $editslides)->toString());
      $exportslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/export');
      $exportslides_link =  (Link::fromTextAndUrl('Export slides', $exportslides)->toString());
      $deleteslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/delete');
      $deleteslides_link =  (Link::fromTextAndUrl('Delete', $deleteslides)->toString());
      $tmp[] =  $editslides_link .' ,' .$exportslides_link .' ,' .$deleteslides_link;
      $rows[] = $tmp;
    }
    $table = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => [],
      '#caption' => '',
      '#colgroups' => [],
      '#sticky' => FALSE,
      '#empty' => t('No slider created yet.'),
       );
     return  $table;
  }

screenshot of the table generated

3
  • Are you sure that $editslides_link is a string and not a render element? Mar 8 '17 at 16:20
  • @Neograph734 $editslides_link is string. even i typecast it as string the result remain same. One interesting this if i assign $tmp[] = $editslides_link .' ,'; code above render link as text but if i assign $tmp[] = $editslides_link ; the code render the link .
    – Aditya
    Mar 8 '17 at 17:22
  • @clive any insight
    – Aditya
    Mar 8 '17 at 20:14
3

The toString method of Link returns the HTML string for the link object.

HTML strings in the table render array are wrapped in a markup element which escapes many HTML tags for security concerns.

You can, however, pass your own render array in the row as follows:

$table['#rows'][0]['item']['data'] = [
  '#type' => 'inline_template',
  '#template' => '{{ link1 }}, {{ link2 }}, {{ link3 }}',
  '#context' => [
    'link1' => $link1->toRenderable(),
    'link2' => $link2->toRenderable(),
    'link3' => $link3->toRenderable(),
  ]
];
8
  • the problem is with $table array. if i print the $rows it output the links in html markup. $table = array( '#type' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => [], '#caption' => '', '#colgroups' => [], '#sticky' => FALSE, '#empty' => t('No slider created yet.'), );
    – Aditya
    Mar 9 '17 at 20:30
  • The problem is that the table render array wraps your string with a markup element. I'll update my answer with how to fix it ,later.
    – Eyal
    Mar 9 '17 at 21:23
  • in the above solution work but how to integrate it with table.
    – Aditya
    Mar 13 '17 at 19:03
  • I don't understand your question.
    – Eyal
    Mar 13 '17 at 19:05
  • the above snippet is one row ,how to display the header and column of the table. i mean how to render in table format.i do agree i am little unclear in explaining the question.
    – Aditya
    Mar 13 '17 at 19:15
0

This is the working code , if it helps any one. public function dexp_layerslider_list() { $header = [ 'ID', 'Name', 'Action', ] ; $results = db_select('{dexp_layerslider}', 'd')->fields('d', [ 'id', 'name', ])->execute(); foreach ($results as $row) { $url = Url::fromUserInput("/admin/dexp_layerslider/". $row->id . "/edit"); $row->name = (Link::fromTextAndUrl($row->name, $url)); $editslides = Url::fromUserInput('/admin/dexp_layerslider/'.$row->id.'/editslides'); $editslides_link = (Link::fromTextAndUrl('Edit slides', $editslides))->toString(); $exportslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/export'); $exportslides_link = (Link::fromTextAndUrl('Export slides', $exportslides))->toString(); $deleteslides = Url::fromUserInput('/admin/dexp_layerslider/'. $row->id . '/delete'); $deleteslides_link = (Link::fromTextAndUrl('Delete', $deleteslides))->toString(); $tmp = [ '#type' => 'inline_template', '#template' => '{{ link1 |raw }}, {{ link2 | raw }}, {{ link3 | raw}}', '#context' => [ 'link1' => $editslides_link->getGeneratedLink(), 'link2' => $exportslides_link->getGeneratedLink(), 'link3' => $deleteslides_link->getGeneratedLink(), ] ]; // Row with attributes on the row and some of its cells. $rows[] = array( 'data' => array($row->id,$row->name,$tmp ) ); } $table = array( '#type' => 'table', '#header' => $header, '#rows' => $rows, '#sticky' => FALSE, '#empty' => t('No slider created yet.'), ); for($i= 0; $i $table['#rows'][$i]['data'][2] = $tmp; } return $table; }

0

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.