0

I have a module which presents a table of contents from a twig template of examples on to how to use the module. This listing is located at /flot-examples. I would like additional modules to be able to add links to this list. The main module, flot_examples has a block, plugins defined like this:

<ul>
{% block plugins %}
{% endblock %}
</ul>

This way, any plugin modules can add <li><a href='flot-examples/[plugin_name]'>plugin example</a> to the list

If I have only one plugin, I think I could override the route to /flot-examples by the plugin, call {{ parent() }} and inject content into {% block plugins %} However, if I have multiple plugins, they all can't do that.

I also see that there is a way for the parent to identify child scripts as 'appendable':

{% use [child_module].html.twig with appendable as appendableContent %}

This would work, but the parent module would have to be given a list of the child modules. If this is the best way to accomplish this, I imagine I would have to:

1)add an array to the parent module, flot_examples, (where?) so that...

2)the child modules would append their names to (where?) so that ...

3)when this array is passed to the twig template (flot_examples.module and somewhere else?), it can loop over the array (in the twig temple) to generate the {% use .... %} statement.

I guess I can see another strategy would be to move this directory listing out of a twig template, and just use render arrays. Then would the child module use preRender functions to append additional list items to the render array, somehow?

Are any of these on the right track? Which would be "the best" or "the easiest" way?

2
  • 1
    I think that is a good strategy is going to Hooks. You define a hook_alter in a module that provide the block and others modules implement this hook_alter, that is it.
    – Vagner
    Commented May 11, 2016 at 23:47
  • If you could explain more details on how to create a custom hook in Drupal 8, i'd appreciate it. Commented May 13, 2016 at 2:15

1 Answer 1

1

I went with a hook instead. There must not be a way to do this in twig.

This section resides in a controller in the parent module:

$plugin_items = [];
// Call modules that implement the hook, and let them add items.
\Drupal::moduleHandler()->alter('flot_examples_toc', $plugin_items);
if (count($plugin_items > 0)) {
  $output['plugins'] = [
    '#title' => 'Plugins',
    '#theme' => 'item_list',
    '#items' => $plugin_items,
  ];
}

And this resides in the child [module].module file.

<?php
use Drupal\Core\Url;
function flot_spider_examples_flot_examples_toc_alter(&$item_list) {
 $options = [
      ':one' => Url::fromRoute('flot_spider_examples.example')->toString(),
    ];
 $item_list[] = t('<a href=":one">Spider Chart</a> (with spider plugin)', $options);

}

The parent creates an array and passes it to the children by reference. They can alter the array by adding elements to it. The parent then adds it to the render array.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.