I have a dynamically created Views listing page where I display fields from "Collection Items" node type in this order:
- Item Title
- Item Status
- Date Stamp
- 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]);