function FileReferenceResolver::isFileReferencedByField

Returns whether the file is referenced by the given field.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity to check.

\Drupal\file\FileInterface $file: The file to look for.

string $field_name: The field name.

string $field_column: The field column/property.

Return value

bool True if the file is referenced, false if not.

1 call to FileReferenceResolver::isFileReferencedByField()
FileReferenceResolver::getReferences in core/modules/file/src/FileReferenceResolver.php
Retrieves a list of references to a file.

File

core/modules/file/src/FileReferenceResolver.php, line 264

Class

FileReferenceResolver
Retrieves file references.

Namespace

Drupal\file

Code

protected function isFileReferencedByField(FieldableEntityInterface $entity, FileInterface $file, string $field_name, string $field_column) : bool {
  // Iterate over the field items to find the referenced file and
  // field name. We also iterate over all translations because a file
  // can be linked to a language other than the default.
  foreach ($entity->getTranslationLanguages() as $langcode => $language) {
    foreach ($entity->getTranslation($langcode)
      ->get($field_name) as $item) {
      if ($file->id() == $item->{$field_column}) {
        return TRUE;
      }
    }
  }
  return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.