0

I want to build popups in Drupal 8. This is the code I am using.

$attr = array( 'attributes' => [ 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 700, ]), ] ); $url = Url::fromRoute('resume.grade_detail', ['uid' => $id, 'courseid' => $courseid]); $internal_link = \Drupal::l($average, $url, $attr); return array('#markup => $internal_link); 

In the rendered output, i don't find the CSS classes I added; as result, the popup doesn't work.

I need help.

1 Answer 1

3

The markup element strips some HTML tags from the output. It is much better to just use the following code.

$attr = [ 'attributes' => [ 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 700, ]), ] ]; $link = Link::createFromRoute($average, 'resume.grade_detail', ['uid' => $id, 'courseid' => $courseid], $attr); return $link->toRenderable(); 
3
  • ok, thanks. but i get error like below. Fatal error: Method Drupal\Core\Template\Attribute::__toString() must not throw an exception, caught Error: Call to undefined method Drupal\Core\Url::render(). Commented Apr 1, 2017 at 12:45
  • fix it like this, $link = $link->toRenderable(); return render($link); that works now, thanks. Commented Apr 1, 2017 at 13:19
  • I don't know the context for your question, but it is not recommended to render render-elements manually. I.E> you should let the twig template engine render them. Commented Apr 1, 2017 at 13:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.