Problem/Motivation

We tried to expose the "Alt attribute" grouped filter, so editors can find media items with missing or present alt text. The filter uses the core string Views plugin with the empty / not empty operators.

Those operators are handled by \Drupal\views\Plugin\views\filter\StringFilter::opEmpty(), which generates only IS NULL / IS NOT NULL SQL conditions:

 protected function opEmpty($field) {
   $operator = ($this->operator == 'empty') ? "IS NULL" : "IS NOT NULL";
   $this->query->addWhere($this->options['group'], $field, NULL, $operator);
 }

However, \Drupal\image\Plugin\Field\FieldType\ImageItem::schema() declares the alt column as a plain nullable varchar with no 'not null' key and no 'default' key:

\Drupal\image\Plugin\Field\FieldType\ImageItem::schema

        ],
        'alt' => [
          'description' => "Alternative image text, for the image's 'alt' attribute.",
          'type' => 'varchar',
          'length' => 512,
        ],

Because there is no 'not null' => TRUE, the Drupal database layer creates a nullable column with no default value. In this
situation, blank alt values are stored differently depending on the origin of the data:

  • Form submission with blank alt field stores '' (empty string). This is the common case for any editor who uploads an image and leaves the alt field blank.
  • Media::updateThumbnail() with no alt metadata explicitly sets
    $this->thumbnail->alt =
     ''

    (line 187 of core/modules/media/src/Entity/Media.php), so media thumbnails are always stored as '', never as NULL.

  • Programmatic save or migration that omits the alt property stores NULL.

The practical consequence is:

  • The "Is empty" filter option (IS NULL) misses every record whose alt was saved as '', which is the overwhelming majority of real-world blank alts coming from form submissions.
  • The "Is not empty" filter option (IS NOT NULL) incorrectly includes those same records, falsely reporting them as having alt text.

The root mismatch is an architectural gap in Drupal core: \Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase::isEmpty() correctly treats both NULL and '' as empty at the PHP/field-API layer, but code>StringFilter::opEmpty() operates at the SQL query layer and never consults the field type's own isEmpty() logic. There is no bridge between the two layers.
A core fix to StringFilter::opEmpty() would be the architecturally ideal solution, but it is a broad change that could have
unintended side-effects across all Views using string filters. The correct and safe fix for this module is a custom Views filter plugin.

Steps to reproduce

  1. Install the module on a Drupal 10 or 11 site with at least one Image media type or any content type with an image field.
  2. Upload an image and intentionally leave the alt text field blank. Save the entity.
  3. Inspect the database row directly, e.g.:
    SELECT thumbnail__alt FROM media_field_data;
    Observe that the value is '' (empty string), not NULL.

Issue fork drupal-3590666

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hoxton’s picture

I found a relevent comment in core/modules/image/src/Hook/ImageThemeHooks.php@39 which doesn't appreciate alt being left null.

While in the case of you requirement, the view should be using 'Is equal to' filter and set the value to empty string. This change would let you find all images with missing alt field (as they are set to empty string by deault). Using 'Is Null' would not be a valid choice as we know alt field default behavious and other concerns.

hoxton’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Active
Issue tags: +Needs issue summary update, +Needs tests

Don't see anything to review so putting back to active.

ishfaqafridi85 made their first commit to this issue’s fork.

ishfaqafridi85’s picture

Status: Active » Needs review

Created MR !17238.

The change treats both NULL and empty strings as empty, updates the operator labels, and adds kernel coverage for empty-string values and the "not empty" operator. Local validation completed successfully: PHP syntax checks pass for both modified files, and git diff --check reports no issues.

smustgrave’s picture

Going to leave for others for now this one is reading like a lot of AI (summary and MR), just fyi AI disclosure needs to be made.

ishfaqafridi85’s picture

Thanks @smustgrave for letting me know.
Yes, I used Open AI to help with the code and tests.
I reviewed the changes myself.
I should have mentioned this earlier, and I will disclose AI use next time.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.76 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.