4

I have render array like this.

$array = [
  '#theme' => 'theme_wrap',
  '#content' => $data,
  '#contextual_links' => [
    'my_module' => [
      'my_module.router.name', $pid
    ]
  ]

and theme-wrap.html.twig

{%
set classes = [
'contextual-region'
]
%}
<div {{ attributes.addClass(classes) }}>
  {{ title_prefix }}
  {{ title_suffix }}
  {{ content }}
</div>

and file mymodule.links.contextual.yml

mymodule.admin.edit:
  title: 'Edit'
  route_name: my_module.router.name
  group: my_module

and route name my_module.router.name exited and working. Render data array working but don't see contextual link.

Update: I have a form to user can enter input. After submit i'll show data from user. And simple controller return array above. - I have insert {{ title_suffix }},{{ title_prefix }} class contextual-region but don't working.

I was doing something wrong? Please help!

0

2 Answers 2

4

First of, usually you want to handle the contextual links in your preprocess theme function so you don't have to handle it every time you need to render the thing.

The actual contual links is added to title_suffix, so you need to do {{ title_suffix }} somewhere in your template to actual render the contextual links. You also need to render the attributes or make sure your wrapper html tag has the contextual-region class.

In Drupal 8, most of contexual links are used with relationship to an entity. If you have something you can view and edit most likely it should be an entity, meaning you should use the entity view builder to render it etc. Might also make it easier for you to do contextual links, as there are many examples of how this is done in core and contrib, like the node module, user module taxonomy module etc.

1
  • I've updated my question.
    – user62538
    Oct 13, 2016 at 9:21
2

After debug function contextual_preprocess(). I've found the answer. My problem is module contextual will set variable title_suffix to first variable in define function themes. In my case: i have function theme

<?php
mymodule_theme($existing, $type, $theme, $path) {
  $theme = [];
  $theme['my_theme'] = [
    'path' => $path . '/templates',
    'variables' => [
      'content' => '',
    ]
  ];
  return $theme;
}

when module contextual will be set variable title_suffix in variable content. Debug in twig data like this:

content[
  $data,
  '#contextual_links'= > [...]
]

So want adding the contextual link render element i need add context_link to variable data like this:

//have $data and add new contextua link
$data['#contextual_links'] => [
  'my_module' => [
    'my_module.router.name', $pid
  ]
]

$array = [
  '#theme' => 'theme_wrap',
  '#content' => $data,
]

and it't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.