0

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

9
  • the pdf_app is a custom module ?
    – berramou
    Jan 7, 2020 at 20:40
  • yes it is a custom module Jan 7, 2020 at 21:06
  • So add the line 262 of modules/pdf_app/pdf_app.module or take a look at this line to see the issue
    – berramou
    Jan 7, 2020 at 21:09
  • line 262 is in the code it is $file_usage = \Drupal::service('file.usage')->listUsage($file); Jan 7, 2020 at 21:11
  • $file should be instance of Drupal\file\FileInterface but in this case $file is null so you should check first on $file something like if ($file && $file instanceof Drupal\file\FileInterface) { $file_usage = \Drupal::service('file.usage')->listUsage($file); }
    – berramou
    Jan 7, 2020 at 21:16

2 Answers 2

0

$file should be instance of Drupal\file\FileInterface but in this case $file is null so you should check first on $file something

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']->__toString();
    $file = \Drupal\file\Entity\File::load($fid);
    if ($file && $file instanceof Drupal\file\FileInterface) {
      $file_usage = \Drupal::service('file.usage')->listUsage($file);
      $variables['output'] = $file_usage;
    }
  }
}
14
  • still getting Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 266 of core/lib/Drupal/Core/Entity/EntityStorageBase.php). Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array) (Line: 250) Drupal\Core\Entity\EntityStorageBase->load(Object) (Line: 532) Drupal\Core\Entity\EntityBase::load(Object) (Line: 261) pdf_app_preprocess_views_view_field(Array, 'views_view_field__fid', Array) (Line: 287) and no link to the node is showing... Jan 7, 2020 at 21:30
  • check the $fid value var_dump($fid); for example
    – berramou
    Jan 7, 2020 at 21:33
  • I am getting object(Drupal\Core\Render\Markup)#4062 (1) { ["string":protected]=> string(2) "24" } object(Drupal\Core\Render\Markup)#4118 (1) { ["string":protected]=> string(2) "41" }} The website encountered an unexpected error. Please try again later. RuntimeException: Failed to start the session because headers have already been sent by but if i write $variables['output'] = $fid; it gives me the id of the file Jan 7, 2020 at 21:43
  • ok try to dump var_dump($fid->__toString());
    – berramou
    Jan 7, 2020 at 21:46
  • I am getting The website encountered an unexpected error. Please try again later. ParseError: syntax error, unexpected 'var_dump' (T_STRING) in Drupal\Core\Extension\Extension->load() (line 262 of modules/pdf_app/pdf_app.module). but if i replace $fid with the id of the field i still get the original error Jan 7, 2020 at 21:47
0

Thanks to the help I got I manage to get it working

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']; 
if (!is_string($fid)){ 
$fid = $variables['output']->__toString();
}

$file = \Drupal\file\Entity\File::load($fid);
if ($file && $file instanceof Drupal\file\FileInterface) {
  $file_usage = \Drupal::service('file.usage')->listUsage($file);
  //var_dump($file_usage);
 if (array_key_exists("node",$file_usage['file'])){
  $essai = $file_usage['file']['node'];
}
 elseif (array_key_exists("media",$file_usage['file'])){
$essai = $file_usage['file']['media'];
}
  $news = (array_keys($essai));
  $new2 = (array_values($news));
  $nef = array_values($new2)[0];
  $event = \Drupal::entityTypeManager()->getStorage('node')->load($nef);
if (!$event)
 return false;

// add ->toString() or other if needed
$abs_link = $event->toLink(NULL, 'canonical', ['absolute' => true]);
  $variables['output'] = $abs_link;
  }
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.