Extending sys_file_reference (FAL)
Asked Answered
C

3

5

I want to extend sys_file_reference with a own field. So i created the field and the TCA. In the backend the field is usable, but i cant refer to the field in my fluid template.

ext_tables.php:

CREATE TABLE sys_file_reference (
 nofollow int(11) DEFAULT '0' NOT NULL,
);

Configuration/TCA/Overrides/sys_file_reference.php:

$tempColumns = array(
    'nofollow' => array(
        'exclude' => 1,
        'l10n_mode' => 'mergeIfNotBlank',
        'label' => 'Link als Nofollow?',
        'config' => array(
            'type' => 'check',
            'default' => '0'
        )
    )
);
TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('sys_file_reference',$tempColumns,1);
TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToPalette('sys_file_reference', 'imageoverlayPalette','--linebreak--,nofollow','after:description');

As far it works in the backend.

Classes/Domain/Model/MyFileReference.php

<?php
namespace LISARDO\Foerderland\Domain\Model;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;

class MyFileReference extends FileReference {

    /**
     * nofollow
     *
     * @var integer
     */
    protected $nofollow;


    /**
     * Returns the nofollow
     *
     * @return integer $nofollow
     */
    public function getNofollow() {
        return $this->nofollow;
    }

    /**
     * Sets the nofollow
     *
     * @param integer $nofollow
     * @return void
     */
    public function setNofollow($nofollow) {
        $this->nofollow = $nofollow;
    }
}

in my setup:

config.tx_extbase.persistence.classes {
    LISARDO\Foerderland\Domain\Model\MyFileReference {
        mapping {
            tableName = sys_file_reference
        }
    }
}

In Fluid i get image.uid oder image.link but image.nofollow is always empty. What did i wrong? I presume the mapping is not correct ...


Well i got the right answer and noticed that i made a mistake and explained it wrong in some ways. First: it is not a normal extbase extension but simply a own content element. So i do not have a own model for my extension where i can inject the implementation as Georg and Victor proposed. I simply had to change the syntax in fluid: {image.properties.nofollow} does the job.

And i recognized that i dont need most of my code:

  • Classes/Domain/Model/MyFileReference.php is not necessary
  • config.tx_extbase.persistence.classes is not necessary too

Only the TCA-Code is needed and a different syntax in fluid.

But i cant figure out why this syntax works and the normal syntax dont.

Thanks for all answers!

Cafeteria answered 26/9, 2016 at 16:23 Comment(0)
P
4

As I known you can access your own properties in fluid with {image.properties.nofollow}.

Phony answered 27/9, 2016 at 6:52 Comment(2)
Thats weird ... you are right. Why this syntax? Is there a manual where i can find more infos? Anywhere: many thanks!Cafeteria
I don't know the "why" but I think the simple setters are defined in the orgin FileModel and the properties getter fetch all database fields. To use the "simple" getter you will need to do what @georg-ringer has wrote.Counterproposal
P
2

Are you referencing your sys_file_reference implementation as well?

So, that could look like that

/**
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\LISARDO\Foerderland\Domain\Model\MyFileReference>
 * @lazy
 */
protected $media;
Paragraph answered 27/9, 2016 at 6:9 Comment(1)
I edited my question - i explained it wrong i presume.Cafeteria
N
1

You need to refer to your custom implementation.

Do it either, like Georg Ringer suggests in his answer, or you can substitute the class on TS level, like this:

plugin.tx_yourext {
    objects {
        TYPO3\CMS\Extbase\Domain\Model\FileReference {
            className = LISARDO\Foerderland\Domain\Model\MyFileReference
        }
    }
}

This will automatically instantiate MyFileReference instead of core's FileReference in all properties, that refer to it, also includes @inject and ObjectManager->get() calls.

If you want to do it on global level (for all plugins, including core), you can just change tx_yourext to tx_extbase in TS above.

Niklaus answered 27/9, 2016 at 7:58 Comment(1)
This might lead to errors with other extensions which don't expect this class but the original one. Therefore I suggest to the minimal variant. Of course it is working this way as well.Paragraph

© 2022 - 2024 — McMap. All rights reserved.