When creating a view of (core) media entities, you'd expect the alt text to be available as a field. However it's not.

Not sure if I'm missing something, but simply can't find it.

Comments

johnzzon created an issue. See original summary.

seanb’s picture

I think you can create a relation from the media entity to the source field and fetch the alt text from there since it's a regular entity reference. Could you try that?

chr.fritsch’s picture

No, the alt and title field is not available.

But following workaround should work:

  • Add the image field to your view
  • Check "Override the output of this field with custom text"
  • Then you should be able to use a token like {{ field_media_image__alt }}
piotr pakulski’s picture

#3 @chr.fritsch works for me. Thank you.

andrewmacpherson’s picture

Issue tags: +Accessibility

Version: 8.5.1 » 8.5.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.5.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

phenaproxima’s picture

andrewmacpherson’s picture

#3 certainly works. I'm using it on a custom admin listing of my own.

Confusingly, there's a views field plugin called "Thumbnail (alt)" but I can't tell how it differs from the "Thumbnail" plugin. Both use the image formatter to output an IMG with an alt attribute.

abhaysaraf’s picture

Assigned: Unassigned » abhaysaraf
Issue tags: +DIACWNov2020
mrpauldriver’s picture

Based on the suggestion at #3. How to use this in a contextual filter, to return media images that do not have alt text?

johnpitcairn’s picture

Version: 8.9.x-dev » 9.3.x-dev
Assigned: abhaysaraf » Unassigned

Similarly to #11, there is no way to use the image alt or title in a filter. It would be tremendously useful for my editors to be able to filter a view by alt text content (for example, "red").

happy047’s picture

Reference to #11 and #12, image alt field can be added in both contextual filter and filter criteria and we can filter images based on the alt text provided in the filter criteria. Tried this by creating a view of an article and exposing image alt field in filters. Are you not getting the alt field in your view?

johnpitcairn’s picture

Sorry, I should clarify: there appears to be no way to use the image alt in a "combine fields" filter in a view of media entities, because it isn't available as a field.

happy047’s picture

Yes, you are right. Image alt cannot be added in combine fields filter in a view. You can alter the view query for a specific project though. For that, you will have to add the image field in combine filter and alter the query in hook_views_query_alter. You can try the following code in your module file:

<?php
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;

/**
 * Implements hook_views_query_alter().
 */
function MODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  // fetch the specific view and the display id
  if ($view->id() == 'your_view_id' && $view->getDisplay()->display['id'] == 'display_id') {
    foreach ($query->where as &$condition_group) {
      foreach($condition_group['conditions'] as &$condition){ 
        //find if the string exists
        if(strpos($condition['field'], 'CONCAT_WS') !== false){
          $find = 'field_image_target_id';
          $replace = 'field_image_alt';
          //replace target_id with alt
          $field = str_replace($find, $replace, $condition['field']);
          $condition = [
            'field' => $field,
            'value' => $condition['value'],
            'operator' => $condition['operator'],
          ];
        } 
      } 
    }
  }

}
?>

Although, this is not the best approach, it will work for the mentioned feature. Hope this helps for your specific project. Will update if I find any alternate approach.
Note: In your combine filter, you will need to set your operator as "contains" instead of "equal to"

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jwilson3’s picture

On Drupal 10, I was able to add both a field for the Alt text using the approach in #3, as well as an exposed filter using a grouped dropdown of the "Image (alt)" field using different potential values in the dropdown:

  • Empty
  • NULL (has a special meaning in the database, but essentially the same as "Empty").
  • Possibly too short (alt text less than 10 characters long).
  • Possibly too long (alt text more than 100 characters long).

It is indeed odd that "Media image (image:alt)" is available in the filter criteria, but not in the view's field list.

anybody’s picture

Yeah this is indeed uncool and would be very helpful. I didn't expect that... quite sure we had it in Drupal 7.

mgifford’s picture

Category: Feature request » Bug report
Issue tags: +wcag111

This is a bug.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

anybody’s picture

Just ran into this once again and I think it's really unexpected. Views covers so much, it should also offer these two values!

anybody’s picture

PS: One more reason to have this is being able to sort by alt / title in tables. This isn't possible using the workaround.

As filter alt / title are already available!