template_preprocess_field breaks DS field layout

Created on 10 December 2020, about 4 years ago
Updated 26 December 2023, 12 months ago

As soon I add a template_preprocess_field function to mytheme.theme, DS field settings are removed/ignored.

I added two wrappers to my field + enabled link to content:
- Link to the content
- Field items -> Classes : "my-btn-container"
- Field item -> Classes : "btn my-btn"

Output:

<div class="my-btn-container">
  <div class="btn my-btn">
    <a href="/de/my-page" hreflang="de">mehr lesen</a>
  </div>
</div>

Now in mytheme.theme I added the following function

function mytheme_preprocess_field__expert__node__field_teaser_li(&$variables) {

  $element = $variables['element'];

  $nodeTitle = t('Read more about') . ' ' . $element['#object']->getTitle();

  // Iterate through the field items.
  foreach($variables['items'] as $key => $item) {
    $variables['items'][$key]['content']['#options']['attributes']['title'] = $nodeTitle;
  }

}

Afterward, the output contains the link title attribute - but the DS wrappers are gone:
Output:

<div class="field field--name-field-teaser-li field--type-string field--label-hidden field--item">
  <a href="/de/my-page" title="Mehr erfahren ΓΌber Testpage" hreflang="de">mehr lesen</a>
</div>

What is going on here? How can I preprocess the link title without breaking DS layout?

πŸ› Bug report
Status

Closed: duplicate

Version

3.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany vistree

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024