4

In my taxonomy-term--NAME.html.twig I use a reference field to load a 'block_content'.

{% for item in term.field_blocks %}
  {% set uuid =  item.entity.uuid.value %}
  {{ drupal_block('block_content:' ~ uuid) }}
{% endfor %}

The block is rendered correctly but without the 'contextual links' (quick edit on front-end).

When I look into the twig_tweak module, I see you can add an array with configuration options.

public function drupalBlock($id, array $configuration = [], $wrapper = TRUE) {

    $configuration += ['label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE];

    /** @var \Drupal\Core\Block\BlockPluginInterface $block_plugin */
    $block_plugin = \Drupal::service('plugin.manager.block')
      ->createInstance($id, $configuration);
    ...
}

Does anyone know how I can change the twig-rule to something like this:

{{ drupal_block('block_content:' ~ uuid, {show_contextual_links:true}) }}

I found this question, but that is not correct: Programatically render block with contextual links (in php)

2 Answers 2

1

I was able to do it like this:

<div data-quickedit-entity-id="block_content/1008" class="contextual-region" data-once="quickedit" data-quickedit-entity-instance-id="0">
  {{ drupal_contextual_links('block_content:block_content=1008:langcode:') }}
  {{ drupal_entity('block_content', 1008) }}
</div>
1

When you use {{ drupal_entity('block_content', 'content_block_id') }}, Drupal skips the block template, which contains the code for contextual links. If you use {{ drupal_block('plugin_id') }}, it will apply the block template, including title, links and other attributes.

If you look at the default block template in Classy (block.html.twig):

{%
  set classes = [
    'block',
    'block-' ~ configuration.provider|clean_class,
    'block-' ~ plugin_id|clean_class,
  ]
%}
<div{{ attributes.addClass(classes) }}>
  {{ title_prefix }}
  {% if label %}
    <h2{{ title_attributes }}>{{ label }}</h2>
  {% endif %}
  {{ title_suffix }}
  {% block content %}
    {{ content }}
  {% endblock %}
</div>

The {{ title_suffix }} part contains the code that renders the contextual links.

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.