Clean way to programmatically add attributes to a view field entity label link I have a View that lists entities. One of the fields is the entity label field, which is set to output a link to the entity itself. I would like to add attributes to the &lt;a&gt; tag to grab later with a jQuery plugin. 

I can use template_preprocess_views_view_field() and hook_views_pre_render() to get the rendered markup and alter it there, but it's very dirty and not something I'll consider. I could also create a specific theme suggestion and field template and do it that way, but I don't want to rely on something that's potentially editable or changeable. It needs to be as generic as possible.

Is there a clean solution for this?
Generate the scaffolding of a Views field plugin with Drupal Console

drupal generate:plugin:views:field


and put the link in the render() method generated by the command above:

  public function render(ResultRow $values) {
    if ($entity = $values-&gt;_entity) {
      return [
        '#type' =&gt; 'link',
        '#title' =&gt; $entity-&gt;label(),
        '#url' =&gt; $entity-&gt;toUrl(),
        '#attributes' =&gt; [
          'class' =&gt; ['foo'],
        ],
      ];
    }
  }


After clearing the cache you'll find the new field plugin when adding a field to the FIELDS list.