Advanced Usage

Last updated on
20 February 2019

Retrieve one or more page-s attached to a Template Whisperer suggestion

Twig

Template Whisperer exposes a twig function name tw_suggestion_entities. This function will allow you to get every entity attached to a specific suggestions.

It's commonly used when you have to create a link from a page to a specific Template Whisperer one.
For example, you have a Basic Page "Projects" with a Template Whisperer Suggestion "projects_collection" - this page list all your projects.
Then you want to create a link to this page from your Homepage template. You will then use tw_suggestion_entities('projects_collection').

{% set suggestions = tw_suggestion_entities('projects_collection') %}
{% if suggestions %}
  <a href="{{ path('entity.node.canonical', {'node': suggestions|first.id}) }}">all my projects</a>
{% endif %}

PHP

Sometimes, the previous Twig function is not necessary, you just need to retrieve which entities are linked to a given Template Whisperer Suggestion. You can simply use the provided services \Drupal\template_whisperer\TemplateWhispererManager & \Drupal\template_whisperer\TemplateWhispererSuggestionUsage.

$twManager = \Drupal::service('plugin.manager.template_whisperer');
$twSuggestionUsage = \Drupal::service('template_whisperer.suggestion.usage');

// Get entities from suggestion, if exists.
$suggestion = $twManager->getOneBySuggestion('projects_collection');
if($suggestion) {
  $entities = $twSuggestionUsage->listUsage($suggestion);
}

Help improve this page

Page status: No known problems

You can: