Skip to main content
added 1 character in body; edited tags
Source Link
berramou
  • 7.3k
  • 2
  • 13
  • 31

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).

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).

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).

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).

Source Link

How to get parent node from a attachment in drupal8 search api results

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).