XF 2.1 How to update $resource object

AndyB

Well-known member
The Resource manager by default shows a Download button like this:

pic001.webp

My goal is to change the Download button so it shows like this:

pic002.webp

In the xfrm_resource_wrapper_macros template the following phrase will be updated using Template modifications:

icon="download">{{ phrase('download') }}</xf:button>

I assume I will use $resource.filename instead of the phrase. My question is how can I update the $resource object to include the filename?

Thank you.
 
I tried extending the XFRM\Pub\Controller\ResourceItem.php like this, but it's not working.

The template code is:

icon="download">{$resource.filename}</xf:button>

The PHP file:

PHP:
<?php

namespace Andy\DownloadByFilename\XFRM\Pub\Controller;

use XF\Mvc\ParameterBag;

class ResourceItem extends XFCP_ResourceItem
{
    public function actionView(ParameterBag $params)
    {
        // get parent
        $parent = parent::actionView($params);
      
        // get resourceId
        $resourceId = $params->resource_id;
    
        // get result
        $finder = \XF::finder('XFRM:ResourceItem');
        $result = $finder
            ->where('resource_id', $resourceId)
            ->fetchOne();
      
        // get currentVersionId
        $currentVersionId = $result['current_version_id'];
      
        // get result
        $finder = \XF::finder('XF:Attachment');
        $result = $finder
            ->where('content_type', 'resource_version')
            ->where('content_id', $currentVersionId)
            ->fetchOne();
      
        // get dataId
        $dataId = $result['data_id'];
      
        // get result
        $finder = \XF::finder('XF:AttachmentData');
        $result = $finder
            ->where('data_id', $dataId)
            ->fetchOne();
      
        // get filename
        $filename = $result['filename'];
      
        // set parent
        $parent->setParams([
           'filename' => $filename
        ]);  

        // return parent
        return $parent;
    }
}

I get this error when I view the Resource item.

1576087595134.webp
 
Last edited:
I'd take a completely different approach:
  • (Optional but recommended to save a query) Extend XFRM\Pub\Controller\ResourceItem::getResourceViewExraWith() to include relation CurrentVersion.Attachments
    CurrentVersion.Attachments is TO_MANY so this won't work
  • Access $resource.CurrentVersion.Attachments.first().Data.filename in the template
 
Last edited:
Thank you, Kirby. I really appreciate your help.

I've hacked this into the entity so there's a relation. I realize this will ultimately need to be done properly, but for testing I added this:

1576091318293.png

How do I do this?

Extend XFRM\Pub\Controller\ResourceItem::getResourceViewExtraWith() to include the relation
 
Last edited:
See my revised comment ;)

It is (currently) not possible to JOIN a TO_MANY relation.

You could define your one relation and make it TO_ONE; this should theoretically work as there currently is usually only one file per version.
But this is not guaranteed, theoretically there could be multiple (this is a setting), the data structures and code do allow for that.
 
Last edited:
It's working!!!

This template code works.

$resource.CurrentVersion.Attachments.first().Data.filename

So with the entity hack I show in post #4 and your template code it works.

Now I have to figure out how to extend the entity relation the proper way.

Thank you, Kirby.
 
You don't need to (nor can't) modify the entity, as said before it's a TO_MANY relation.

The template code is all you need.
 
Apparently the add-on will require showing all the attachment filenames while viewing a resource item.

Kirby has indicated "It is (currently) not possible to JOIN a TO_MANY relation." So does that mean it's simply impossible to do what I would like to do?
 
Top Bottom