A
A
asantat2018-01-29 10:25:39
PHP
asantat, 2018-01-29 10:25:39

How to themize link field in drupal8?

Initial data: php 7.1, drupal 8.4.4, custom theme inherited from the uikit theme, which in turn is from stable.
What you need: add a class to the html link tag inside a custom custom block.
What has been done: redefined the twig-template of the field by entity type, bundle and field name. It contained a variety of options, ranging from

<a href="{{ url }}"{{ attributes.addClass(button_class) }}>{{ title }}</a>

I tried other options: complex constructions, methods, filters. Outcome:
<a href="" class="uk-button uk-button-default"></a>

spoiler
Пробовал проверить, что есть в контексте, через
<ol>
    {% for key, value in _context  %}
      <li>{{ key }}</li>
    {% endfor %}
  </ol>

Показал вот что:
element
theme_hook_original
attributes
title_attributes
content_attributes
title_prefix
title_suffix
db_is_active
is_admin
logged_in
user
directory
entity_type
field_name
field_type
label_display
label_hidden
label
multiple
items
#cache
theme_hook_suggestions
theme_hook_suggestion
button_class
_parent
потом проверял содержимое переменных через {{ kint() }} в файле шаблона. Показал, что нет ни атрибутов, ни uri, ни значений. Переменная items содержала content, в которой хранились значения, но использовать их оттуда не получилось.
5a6eca5506272137685407.jpeg

How can I themize a field using twig methods, why didn’t it work for me, and what can I do to provide the necessary variables to the template engine?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
antiden, 2018-02-05
@donda

Bamboo twig might help

A
asantat, 2018-02-07
@asantat

This problem slowed down the work on the template, but thanks to this article, which described such a scheme:
and thanks to these examples, a solution was found that helped to themize the link. But I think this approach is wrong. The template developer should not, in trivial tasks, get to those objects that need to remain under the hood. The only option that worked solely with twig resources, i.e. without preprocessors, extensions, formatters and modules, was

{% if items %}
  <a href="{{ element[0]['#url'] }}"{{ attributes.addClass(button_class) }}>{{ element[0]['#title'] }}</a>
{% endif %}

I wish the drupal developers to improve the workflow for theming compound fields, in particular links, in the next releases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question