1

I have a dynamically created Views listing page where I display fields from "Collection Items" node type in this order:

  1. Item Title
  2. Item Status
  3. Date Stamp
  4. Document (uploaded file. I'm only getting the filename here)

My goal right now is to take the path of the uploaded document and use it as a link in the title. Like so

<a href="path/to/file"> Item Title </a>

I'm trying to achieve this with hook_views_pre_render(), but am stuck just testing out some code I found from here.

Here is my code:

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\collections\Entity\CollectionsEntityInterface;
use Drupal\collections\Entity\CollectionsEntityType;

function collections_views_pre_render(&$view){
  $results = $view->result;
  $view_id = $view->storage->id();
  $current_display = $view->current_display;

  if(strpos($view_id,"collection__" ) > -1 && $current_display == "default_page"){
  foreach ($results as $key => $result){

    //prints 0123456
      echo $key;

      //breaks the site. Gets message "The website encountered an unexpected error. Please try again later."
      $parent_id_value =  $result->_entity->parent_id->getValue()[0]['value'];
      $parent_type_value =  $result->_entity->parent_type->getValue()[0]['value'];
      $media_field= $result->_entity->field_media_field->getValue()[0]['value'];

    }
  }
}

Once I got the file's path, I would then be trying the solution offered here: https://www.drupal.org/forum/support/module-development-and-code-questions/2020-01-12/use-hook_views_pre_render-to-change

$link = Drupal\Core\Link::createFromRoute($parent_id_value, 'entity.node.canonical', ['node' => $parent_id_value]);
2
  • Isn't there an option in the Views UI, for your Document field, to link the document name to the document? Also, what type of field is your Document field?
    – sonfd
    Aug 10, 2020 at 11:50
  • Yes, but I have to achieve this programmatically as the view was created dynamically. A colleague started this project and I was in charge of completing it when they left the company. We have a custom module with a File upload field. The view displays the contents of this module.
    – kcsting
    Aug 10, 2020 at 19:10

0

Your Answer

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

Browse other questions tagged or ask your own question.