I have apachesolr index search with Search API attachments. In the results I get a link to the file but I would like to get a link to the parent node. I tried in the view but there is no such field with relationship to file usage. I wrote the following in a module to override the field fid in the view but it returns an error
function hook_preprocess_views_view_field(&$variables) {
$view = $variables['view'];
$field = $variables['field'];
if ($view->storage->id() == 'saa' &&
$view->current_display == 'page_1' &&
$field->field == 'fid') {
$fid = $variables['output'];
$file = \Drupal\file\Entity\File::load($fid);
$file_usage = \Drupal::service('file.usage')->listUsage($file);
$variables['results'] = $file_usage;
}
}
It returns
The website encountered an unexpected error. Please try again later. TypeError: Argument 1 passed to Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage() must implement interface Drupal\file\FileInterface, null given, called in /home/rebecca/public_html/new5/modules/pdf_app/pdf_app.module on line 262 in Drupal\file\FileUsage\DatabaseFileUsageBackend->listUsage() (line 123 of core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php).
modules/pdf_app/pdf_app.module
or take a look at this line to see the issue$file
should be instance ofDrupal\file\FileInterface
but in this case$file
is null so you should check first on$file
something likeif ($file && $file instanceof Drupal\file\FileInterface) { $file_usage = \Drupal::service('file.usage')->listUsage($file); }