Add additional source to Image for further actions

115 views Asked by At

im trying to add an additional source to the dce-image for further actions e.g. an alternate source to perform some JavaScript actions.

The wanted output should look like this:

<img src="path/to/foo.png" data-altsrc="path/to/bar.png">

The problem is the dce i'm using - its iterating through the "images" like this:

<f:for each="{dce:fal(field:'image', contentObject:contentObject)}" as="fileReference">
    <f:image src="{fileReference.uid}" treatIdAsReference="1" />
</f:for>

So if I would insert multiple images to this I have no real relation between the images where i know which one is the normal-source and which one is the alternate source.

So there was the possibility to create a section and add two fields for images which we can restrict to one image per field. But there again is the for-loop which doesn't allow me to access the source of the second image for the first image.

It should be a visible relation between these images for the user which is working with the dce.

Im trying to achieve something like this:

<f:for each="{field.images}" as="images">
    <!-- want to achieve something like this -->
    <f:image image="{images.foo.src}" data-altsrc="{images.bar.src}">

    <!-- thats the normal way iterating through images -->
    <f:for each="{images.foo}" as="image">
        <f:image image="{image}" />
    </f:for>
</f:for>

Another idea would be to iterate first through the alternate images and store them into an array and on the main-images to access them but I have no idea if this is even possible also this will restrict the usability of the dce for the user.

Is there any way to achieve this with dce-fluid ?

Thanks in advance

2

There are 2 answers

1
Philipp Wrann On BEST ANSWER

Well, i guess you could extend the sys_file_reference table to add a relation from there. So you would have nested relations (never done that, so you would have to try).

You would also have to add the field to the tca type in the place you need it, that could be a little tricky. Have a look at Tca types overrides.

You could extend the sys_file_reference table with a field "alternative_reference" and add the neccessary TCA setup. Then you would have to retrieve the FileReferences via the FileRepository and use sys_file_reference as foreign table (and of course the sys_file_reference uid as identifier).

FileRepository::findByRelation(
    'sys_file_reference',
    'alternative_reference',
    $uidOfActualSysFileReferenceRecord
);

The other possibility would be a completely new record with 2 different sys_file_reference relations. That record (eg: tx_ext_domain_model_imageset) would have an image_default and image_alternate field, both would be configured as file_relations. That would work for sure.

$defaultImages = FileRepository::findByRelation(
    'tx_ext_domain_model_imageset',
    'image_default',
    $uidOfRecord
);
$alternativeImages = FileRepository::findByRelation(
    'tx_ext_domain_model_imageset',
    'image_alternative',
    $uidOfRecord
);

I assume you do have knowledge about creating records, models, tca and so on.

I personally would prefer the second way, it is more clean and does not change the core-table structures.

Also there is a second image generation viewhelper, that might suite your needs better

<img src="{f:uri.image()}" data-altsrc="{f:uri.image()}" />

But the best way to go might be to write your own ViewHelper for that purpose. You could pass your Object (ImageSet) as parameter and handle all logic there. So your template would be simpler and easier to read/work on.

1
StatiX On

You have to use data attribute of fluid viewhelper and then get the uri of the image with inline call. Here is how to achieve :

 <f:image src="{fileReference.uid}" data="{altsrc: '{f:uri.image(src: \'{fileReference.uid}\', treatIdAsReference: 1}" treatIdAsReference="1"/>

This should do the work.