0

I have a simple view block with exposed the taxonomy term linked to the taxonomy page, but I have noticed that the attribute is missing, I tried to rewrite the term but every attempt went wrong.

What I need is simple, this is the view generated field:

<a href="/category/events" tabindex="-1">Events</a>

I want to add title="Events", like this:

<a href="/category/events" tabindex="-1" title="Events">Events</a>

But I can't find any solutions in Drupal 8 view!

  • I'm not sure why you're adding the title attribute, but I don't think the example you've given is the intended way to use for it. – sonfd Apr 20 '18 at 13:48
  • Hi sonfd, I want to add the title for accessibility, a requirement that WCAG requires. w3.org/TR/WCAG20-TECHS/H33.html – Luca Pagani Apr 23 '18 at 7:28
  • Is your TITLE coming from VIEW or Its the default taxonomy page with TITLE? – Ajay Reddy Apr 23 '18 at 11:00
  • From the page you linked to "Because of the extensive user agent limitations in supporting access to the title attribute, authors should use caution in applying this technique. For this reason, it is preferred that the author use technique C7: Using CSS to hide a portion of the link text (CSS) or H30: Providing link text that describes the purpose of a link for anchor elements." – sonfd Apr 23 '18 at 13:14
  • Hi Ajay Reddy, the title i coming from VIEW, view generated field! – Luca Pagani Apr 24 '18 at 12:28
0

You should be able to use template_preprocess_views_view_field to accomplish this. Create that hook in your custom theme like:

/**
 * Alter field_MY_TAX_FIELD link to add title attribute.
 *
 * Implements template_preprocess_views_view_field().
 */
function MY_THEME_preprocess_views_view_field(&$vars) {
  if ($vars['field']->field == 'field_MY_TAX_FIELD') {
    // Add title attribute to link.
  }
}

Alternatively you could create a new field formatter that extends Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter. Override the viewElements method. First call $elements = parent::viewElements($items, $langcode);, then traverse $elements and add your title attribute when appropriate. After you create the formatter, clear cache and then go back to your view and select your new formatter as formatter for the taxonomy field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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