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.

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();
  • 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(). – user670809 Apr 1 '17 at 12:45
  • fix it like this, $link = $link->toRenderable(); return render($link); that works now, thanks. – user670809 Apr 1 '17 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. – Eyal Apr 1 '17 at 13:49

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.