[TYPO3-dev] FAL in an Extbase 6.0 extension... but how?!

Anders Gissel invadercyg at gmail.com
Thu Jan 31 18:37:22 CET 2013


Hello Henjo,

thanks to some helpful tips from a fellow TYPO3-freak, I was able to get 
things up and running. Mind you, these are extremely dirty hacks (albeit 
inside your own extension only), and once Extbase starts properly 
supporting FAL (which, currently, it doesn't), you will have to change a 
lot of things, at the very least in your frontend rendering. You'd be 
wise to plan accordingly. But without further ado, here's how to use FAL 
to attach a single image to a domain model object, and then retrieve the 
raw data in frontend.


TCA configuration:
         'file' => array(
             'exclude' => 0,
             'label' => 'Some label',
             'config' => 
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig('file', 
array(
                 'appearance' => array(
                     'createNewRelationLinkTitle' => 
'LLL:EXT:cms/locallang_ttc.xlf:images.addFileReference',
                     'collapseAll' => TRUE,
                 ),
                 'maxitems' => 1,
                 'minitems' => 1
             ), $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'])
         ),

Notice that the first argument to getFileFieldTCAConfig() is "file" - 
this needs to be named after your field, so if the block above was for 
the field "attachedImage" instead of "file", you would then set 
"attachedImage" as the first argument instead. In this example, though, 
it's called "file". Deal with it!

Here's where things get tricky. Your regular domain model should 
probably look something like this:


class AbstractImage extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

     // Lots of code removed here. This is for demonstration purposes only.

     /**
      * file
      *
      * @var \TYPO3\CMS\Core\Resource\FileReference
      */
     protected $file;

     /**
      * Returns the file
      *
      * @return \TYPO3\CMS\Core\Resource\FileReference $file
      */
     public function getFile() {
         return $this->file;
     }

     /**
      * Sets the file
      *
      * @param \TYPO3\CMS\Core\Resource\FileReference $file
      * @return void
      */
     public function setFile(\TYPO3\CMS\Core\Resource\FileReference $file) {
         $this->file = $file;
     }
}


Notice how I call it AbstractImage and not just Image. I can then 
override the regular functions with this (this is lifted verbatim from 
my model object, and it works):


class Image extends AbstractImage {

     /**
      * @var \TYPO3\CMS\Core\Resource\FileRepository
      * @inject
      */
     protected $typo3FALRepository;

     /**
      * file
      *
      * @var int
      */
     protected $file;

     /**
      * Returns the file USING HAXX0RED METHODS! PLEASE DON'T TRY THIS 
AT HOME, KIDS!
      *
      * @return array $file
      */
     public function getFile() {

         // Get the UID from the current image object.
         $objectUid = $this->getUid();

         // Use the UID to search sys_file_reference
         $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid', 
'sys_file_reference', "uid_foreign={$objectUid} AND fieldname = 'file' 
AND tablenames = 'tx_myext_domain_model_image' AND deleted=0 AND hidden=0");
         if (is_array($row)) {
             $fileObject = 
$this->typo3FALRepository->findFileReferenceByUid($row['uid']);
             $fileObjectData = $fileObject->toArray();
             return $fileObjectData;
         }
         return FALSE;
     }
}


Firstly, I inject the FAL repository. I then tell Extbase NOT to expect 
a filereference-object, but merely an integer. Anything else, and it'll 
kill itself trying to set up the relations. Trust me on that one.
Then, in getFile(), I use the uid from the object itself (not "$file") 
to make a manual lookup in the sys_file_reference table. Notice how you 
should (must) include "tablenames" and "fieldname", just to make sure 
you don't get any errant data from other tables.
Finally, if an array is returned, meaning the query got a usable result, 
I use the FAL repository to fetch the actual file reference, and then 
get all the data from inside it by using toArray(). You can then do 
something like this:

<f:for each="{gallery.images}" as="imageRecord">
     <f:debug>{imageRecord.file}</f:debug> - this will contain the 
result of all of the above.
     <f:image src="{imageRecord.file.url}" alt="{imageRecord.title}" />
</f:for>


Come the day when Extbase properly supports FAL, you can simply empty 
out the contents of your Image-class, and let it inherit everything from 
AbstractImage. You will still have to change your frontend rendering 
(and I have no idea how that'll work), but at least the backend changes 
will be small.



Some post this was. I hope it'll be helpful.

Best regards,
Anders




On 31-01-2013 14:36, Henjo Hoeksma | Stylence wrote:
> Hi Anders,
>
> I am more than eager to see an example / bestpractice solution for this as
> well. I have found there's some basic implementation in Extbase atm.  See
> the functional test and the domain models that are there.
>
> Some insights, or a small docu on how the architecture of FAL is setup
> would be great.
>
> Kind regards,
>
> Henjo
>
> Problems are small because we learned how to deal with them.
> Problems are big because we need to learn how to deal with them.
>
>
> On Thu, Jan 31, 2013 at 10:23 AM, Anders Gissel <invadercyg at gmail.com>wrote:
>
>> Hey guys,
>>
>>
>> I have been trying to figure out how to integrate the fancy new File
>> Abstraction Layer in a new Extbase-extension I'm working on, since I want
>> the extension itself to be as modern as possible, and since
>> t3lib_basicFileFunctions is marked as deprecated, I seem to have little
>> choice but to either backport the file functions I need myself into a
>> utility in my extension, and stick with the good old /uploads/tx_whatever/
>> format, or to make the switch to FAL. And obviously the latter would be
>> preferable and vaguely more future proof.
>> However, what little documentation exists on FAL is sorely lacking, at
>> best. There seem to be no coding examples, outside of the basic "this is
>> how you display all files in a filemount"-example found in the original
>> FAL-presentation slides, and they don't cover the integration of FAL in a
>> domain model object at all.
>>
>> So. Basically, I have a model object that needs a single reference to a
>> file (an image, if you must know). I spent hours trying every permutation
>> of \TYPO3\CMS\Core\Resource\* for my file object I could think of, but at
>> best I got nothing, at worst I got exceptions from the SQL-layer. And while
>> I got the TCA to sort-of work (I got it to present me with a buggy
>> imitation of the file selector so prominently on display in the tt_content
>> editor), I couldn't for the life of me get the stored data out of the model
>> object in the frontend. Again, exceptions all around, with little
>> indication of what actually went wrong.
>>
>> So I'm pleading with you – because surely someone smarter than I (that's
>> practically most of the planet) must have made an extension with a working
>> FAL-integration, right? Can anyone point me to a project on TER or GitHub
>> that I can dissect and learn how to go about this? Or, if such a project is
>> in the works but hasn't been released yet, help me the hard way by
>> answering these questions:
>>
>>
>> 1) How on earth does one define a field in the domain model, so that it
>> accepts, stores and returns a proper file reference?
>>
>> 2) How should the TCA for such a field be defined, so that it can be
>> safely used in the backend? The extension builder's solution results in...
>> well, nothing.
>>
>> 3) Bonus question: how does one go about storing a new file in a FAL
>> storage through the frontend? Let's assume I have all the file data readily
>> at hand, and I just want to store it in a pre-defined file storage. But how?
>>
>>
>> Once I've figured these things out, I'll happily write proper code
>> examples and publish them online – if nothing else on my own website, but
>> I'll be happy to add it to an official Wiki or... whatever. Google is of no
>> help; I'm probably not the first to run into this problem, and I certainly
>> won't be the last.
>>
>> Here's hoping. Because surely it's just the documentation that's lacking
>> and me being stupid, and not, as some evil tongues will have me believe,
>> that the TCA is currently not able to handle FAL references inside an
>> extension? Right?
>>
>>
>> Best regards,
>> Anders Gissel
>> Denmarkistan
>>
>> ______________________________**_________________
>> TYPO3-dev mailing list
>> TYPO3-dev at lists.typo3.org
>> http://lists.typo3.org/cgi-**bin/mailman/listinfo/typo3-dev<http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-dev>
>>
> _______________________________________________
> TYPO3-dev mailing list
> TYPO3-dev at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-dev




More information about the TYPO3-dev mailing list