I have a Model for a Products table with this code to get the images references:
/**
* Returns the images
*
* #return \TYPO3\CMS\Core\Resource\FileReference $images
*/
public function getImages() {
$fileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\FileRepository::class);
$this->images = $fileRepository->findByRelation('tx_myext_domain_model_products', 'images', $this->uid);
return $this->images;
}
But it triggers an error:
ERROR => 'Table 'database.tx_core_resource_filereference' doesn't exist'
Don't sure why this table name is used. sys_file_reference is the name of the real table in the database.
If this is in the domain as you suggested, then all you should need is:
.../Classes/Domain/Model/Product.php
...
/**
* images
*
* #var \TYPO3\CMS\Extbase\Domain\Model\FileReference
*/
protected $images = null;
/**
* Returns the images
*
* #return \TYPO3\CMS\Extbase\Domain\Model\FileReference images
*/
public function getImages()
{
return $this->images;
}
/**
* Sets the images
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $images
* #return void
*/
public function setImages(\TYPO3\CMS\Extbase\Domain\Model\FileReference $images)
{
$this->images = $images;
}
...
Then the TCA for products would require config for images... for example:
.../Configuration/TCA/tx_yourext_domain_model_product.php
...
'images' => [
'label' => 'FAL Image',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'images',
[
'appearance' => [
'createNewRelationLinkTitle' => 'LLL:EXT:cms/locallang_ttc.xlf:images.addFileReference'
],
// custom configuration for displaying fields in the overlay/reference table
// to use the image overlay palette instead of the basic overlay palette
'overrideChildTca' => [
'types' => [
'0' => [
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
],
\TYPO3\CMS\Core\Resource\File::FILETYPE_TEXT => [
'showitem' => '
--palette--;LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.imageoverlayPalette;imageoverlayPalette,
--palette--;;filePalette'
],
],
],
],
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
),
],
...
Seems like you use a wrong FileReference. Check if you import the correct one in your Model:
Wrong:
use TYPO3\CMS\Core\Resource\FileReference;
Correct:
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
Related
i would like to show the user who created an element. The TCA looks like this:
'ctrl' => [
'title' => 'Title',
'label' => 'title',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'cruser_id' => 'cruser_id',
With getter i can get the tstamp and the crdate. But with the cruser_id this doesn't work. How can i get the user name of the user with the cruser_id?
The following example, works on TYPO3 11.5.8 and should work with TYPO3 10 as well.
First you need your getters und setters on your model (\YourVendor\YourExtension\Domain\Model\YourModel)
/**
* #var int
*/
protected int $cruserId = 0;
/**
* #return int
*/
public function getCruserId(): int
{
return $this->cruserId;
}
/**
* #param int $cruserId
*/
public function setCruserId(int $cruserId): void
{
$this->cruserId = $cruserId;
}
Then you need to add the TCA configuration for this specific field. You do not need to add it on types or anywhere else. TYPO3 just needs the configuration so it knows what to do with it.
'cruser_id' => [
'config' => [
'type' => 'input',
'size' => 10,
'max' => 255,
],
],
Last and not least, you need to map it on the right column. So under your your_extension/Configuration/Extbase/Persistence/Classes.php you need to add your Model and the property:
\YourVendor\YourExtension\Domain\Model\YourModel::class => [
'columns' => [
'cruserId' => [
'mapOnProperty' => 'cruser_id'
]
]
]
Here is a reference: TYPO3 v10 How do i get an object's crdate or the tstamp in FrontEnd
Backend User Object
If you want to get the Backend user information, the steps are the same but some changes need to be made.
Model
use TYPO3\CMS\Beuser\Domain\Model\BackendUser
/**
* #var BackendUser|null
*/
protected ?BackendUser $cruserId = null;
/**
* #return BackendUser|null
*/
public function getCruserId(): ?BackendUser
{
return $this->cruserId;
}
/**
* #param ?BackendUser $cruserId
*/
public function setCruserId(?BackendUser $cruserId): void
{
$this->cruserId = $cruserId;
}
TCA
'cruser_id' => [
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'be_users',
'foreign_table_where' => 'ORDER BY username',
],
],
This will give you the following:
Hope it helped
I am trying to extend a new field with fe varient. I have added below code:
ext_tables.sql
#
# Table structure for table 'tx_cartproducts_domain_model_product_fevariant'
#
CREATE TABLE tx_cartproducts_domain_model_product_fevariant (
note text,
);
/myext/Configuration/TCA/Overrides/tx_cartproducts_domain_model_product_fevariant.php
<?php
defined('TYPO3_MODE') or die();
$temporaryColumns = [
'note' => [
'exclude' => 1,
'label' => 'Note',
'config' => [
'type' => 'text',
'eval' => 'trim',
],
],
];
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'tx_cartproducts_domain_model_product_fevariant',
$temporaryColumns
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'tx_cartproducts_domain_model_product_fevariant',
'note',
'',
'after:title'
);
Now, backend working as expected, to render value in frontend. I added model like below:
myext/Classes/Domain/Model/Product/FeVariant.php
<?php
namespace Vendor\myext\Domain\Model\Product;
class FeVariant extends \Extcode\CartProducts\Domain\Model\Product\FeVariant
{
/**
* note
*
* #var string
*/
protected $note = '';
/**
* Returns note
*
* #return string
*/
public function getNote()
{
return $this->note;
}
/**
* Sets note
*
* #param string $note
*/
public function setNote($note)
{
$this->note = $note;
}
}
And added class mapping like below
myext/Configuration/Extbase/Persistence/Classes.php
<?php
declare(strict_types=1);
return [
\Vendor\myext\Domain\Model\Product\FeVariant::class => [
'tableName' => 'tx_cartproducts_domain_model_product_fevariant',
],
];
I also found reference here but it also not working for fe_variant.
I think you have to add some lines to your ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Extcode\CartProducts\Domain\Model\Product\FeVariant::class] = [
'className' => \Vendor\myext\Domain\Model\Product\FeVariant::class
];
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class)
->registerImplementation(
\Extcode\CartProducts\Domain\Model\Product\FeVariant::class,
\Vendor\myext\Domain\Model\Product\FeVariant::class
);
I have a typo3 extension which allows to create Content Elements within the extension - in typo3 v9 it works fine but on typo3 v10 it doesn’t, when I debug in the template i got this result:
contentElements => protected TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorageprototypeobject (empty)
The Content Element is created and in the backend also correctly visible within the Dataset
I also tried to have a look at tx_news which implements a simular behaiver but I doesn’t have a clue, for me it seems that I done right but yeah … , following some code snippets I used, may you have an Idea:
In the setup.typoscript:
lib.contenttabs_fecontenttabs.contentElementRendering = RECORDS
lib.contenttabs_fecontenttabs.contentElementRendering {
tables = tt_content
source.current = 1
dontCheckPid = 1
}
In the Model (I tried to use the sam as news):
/**
* Get content elements
*
* #return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
public function getContentElements()
{
return $this->contentElements;
}
/**
* Set content element list
*
* #param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $contentElements content elements
*/
public function setContentElements($contentElements)
{
$this->contentElements = $contentElements;
}
/**
* Adds a content element to the record
*
* #param \Moc\Contenttabs\Domain\Model\TtContent $contentElement
*/
public function addContentElement(\Moc\Contenttabs\Domain\Model\TtContent $contentElement)
{
if ($this->getContentElements() === null) {
$this->contentElements = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
$this->contentElements->attach($contentElement);
}
/**
* Get id list of content elements
*
* #return string
*/
public function getContentElementIdList()
{
return $this->getIdOfContentElements();
}
/**
* Get translated id list of content elements
*
* #return string
*/
public function getTranslatedContentElementIdList()
{
return $this->getIdOfContentElements(false);
}
/**
* Collect id list
*
* #param bool $original
* #return string
*/
protected function getIdOfContentElements($original = true)
{
$idList = [];
$contentElements = $this->getContentElements();
if ($contentElements) {
foreach ($this->getContentElements() as $contentElement) {
if ($contentElement->getColPos() >= 0) {
$idList[] = $original ? $contentElement->getUid() : $contentElement->_getProperty('_localizedUid');
}
}
}
return implode(',', $idList);
}
In the Template:
<f:cObject typoscriptObjectPath="lib.contenttabs_fecontenttabs.contentElementRendering">
{tab.contentElementIdList}
</f:cObject>
Some of the TCA Code (which seems to works fine):
'content_elements' => [
'exclude' => 1,
'l10n_mode' => 'mergeIfNotBlank',
'label' => 'Content Elements',
'config' => [
'type' => 'inline',
'allowed' => 'tt_content',
'foreign_table' => 'tt_content',
'foreign_sortby' => 'sorting',
'foreign_field' => 'tx_tabs_content_elements',
'minitems' => 0,
'maxitems' => 99,
'overrideChildTca' => [
'columns' => [
'colPos' => [
'config' => [
'default' => 99
]
],
'CType' => [
'config' => [
'default' => 'textmedia'
]
]
]
],
'appearance' => [
'collapseAll' => 1,
'expandSingle' => 1,
'levelLinksPosition' => 'bottom',
'useSortable' => 1,
'showPossibleLocalizationRecords' => 1,
'showRemovedLocalizationRecords' => 1,
'showAllLocalizationLink' => 1,
'showSynchronizationLink' => 1,
'enabledControls' => [
'info' => false,
]
]
]
],
Thanks in advance!
I have in my TYPO3 an object called Location where I store informations about a city, including a pdf file.
In the backend I can upload a pdf without problem, but when I try to display it, I get NULL. The value of 'pdf' in the database is not NULL, but when I debug <f:debug>{location}</f:debug> I get pdf => NULL !!!
Here is my TCA/LOCATION :
$GLOBALS['TCA']['tx_locations_domain_model_location'] = array(
'ctrl' => $GLOBALS['TCA']['tx_locations_domain_model_location']['ctrl'],
....
'columns' => array(
...
'pdf' => array(
'exclude' => 1,
'label' => 'LLL:EXT:locations/Resources/Private/Language/locallang_db.xlf:tx_locations_domain_model_location.pdf',
'config' => array (
'type' => 'group',
'internal_type' => 'file',
'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
'max_size' => $GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize'],
'uploadfolder' => 'uploads/pics',
'show_thumbs' => 1,
'size' => 1,
'minitems' => 0,
'maxitems' => 1
)
),
...
The Getter and Setter should be fine in typo3conf/ext/locations/Classes/Domain/Model/Location.php :
/**
* Returns the pdf
*
* #return \TYPO3\CMS\Extbase\Domain\Model\FileReference $pdf
*/
public function getPdf() {
return $this->pdf;
}
/**
* Sets the pdf
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $pdf
* #return void
*/
public function setPdf(\TYPO3\CMS\Extbase\Domain\Model\FileReference $pdf) {
$this->pdf = $pdf;
}
You can see the debug gives pdf=> NULL from below:
My pdf column is not empty :
Did I miss something ??
UPDATE :
After the answer of Pavin, I can see something in PDF, but I can't get the name of the file in href tag , maybe I need to change something after the new answer.
I can see things changed in the Database, the pdf field now is boolean. that's why maybe I get pdf0.originalResource NULL, but in the backend I can upload files and see then after save and refresh !!!:
<p><f:translate key="tx_locations_domain_model_location.downloadpdf" /> <a class="download" target="_blank" title="Initiates file download" href="{location.pdf.originalResource.publicUrl}"><f:translate key="tx_locations_domain_model_location.here" />..</a></p>
UPDATE 2
my new Model/Location.php
/**
* pdf
*
* #var string
*/
protected $pdf = NULL;
...
/**
* Returns the pdf
*
* #return string $pdf
*/
public function getPdf() {
return $this->pdf;
}
/**
* Sets the pdf
*
* #param string $pdf
* #return void
*/
public function setPdf($pdf) {
$this->pdf = $pdf;
}
My TCA/Location.php
'pdf' => array(
'exclude' => 1,
'label' => 'LLL:EXT:locations/Resources/Private/Language/locallang_db.xlf:tx_locations_domain_model_location.pdf',
'config' => array (
'type' => 'group',
'internal_type' => 'file',
'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
'max_size' => $GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize'],
'uploadfolder' => 'uploads/pics',
'show_thumbs' => 1,
'size' => 1,
'minitems' => 0,
'maxitems' => 1
)
),
You can use below TCA configuration for Doc types records. also add Getter and Setter methods in modle file.
'pdf' => array(
'exclude' => 0,
'label' => 'LLL:EXT:ext_key/Resources/Private/Language/locallang_db.xlf:label_key.files',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'pdf',
array('minitems' => 0,'maxitems' => 10),
'pdf,doc,docx'
),
),
For Model.php files.
/**
* pdf
*
*#var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
*/
protected $pdf;
initStorageObjects Methods :
protected function initStorageObjects() {
$this->files = new ObjectStorage();
parent::initializeObject();
}
Getter And Setter Method:
/**
* Returns the pdf
*
* #return ObjectStorage
*/
public function getPdf() {
return $this->pdf;
}
/**
* Sets the pdf
*
*/
public function setPdf($pdf) {
$this->pdf = $pdf;
}
See attached image for pdf name. Please add pdf title. For Pdf title you can use {location.pdf.title}
You use group as the type for saving the file in the TCA, so the property $pdf in your model must be string and not \TYPO3\CMS\Extbase\Domain\Model\FileReference
FileReference only work for FAL (File Abstraction Layer)
I create a extbase Plugin in TYPO3 6.2. In one table i have a field "fuid" where i want to store the fe_users uid, to know which user can edit this record.
I set the "fuid" in createAction:
$newLocation->setFuID((int) $GLOBALS['TSFE']->fe_user->user['uid']);
This work. In the Database is the right UID.
But in the editAction:
$location->getFuID()
returns null
Why?
TCA:
fu_i_d' => array(
'exclude' => 1,
'label' => 'LLL:EXT:pitss24/Resources/Private/Language/locallang_db.xlf:tx_pitss24_domain_model_location.fu_i_d',
'config' => array(
'type' => 'select',
'items' => array (
array('',0),
),
'foreign_table' => 'fe_users',
'foreign_class' => '\TYPO3\CMS\Extbase\Domain\Model\FrontendUser',
'minitems' => 0,
'maxitems' => 1,
'size' => 10,
'appearance' => array(
'collapseAll' => 0,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'showAllLocalizationLink' => 1
),
),
),
In the Backend / TYPO3 is all OK!
In Model File.
/**
* feuseruid
*
* #var \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
*/
protected $feuseruid;
// GET and SET Methods
/**
* Returns the feuseruid
*
* #return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser feuseruid
*/
public function getFeuseruid() {
return $this->feuseruid;
}
/*
* Sets the feuseruid
*
* #param string $feuseruid
* #return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser feuseruid
*/
public function setFeuseruid(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser $feuseruid) {
$this->feuseruid = $feuseruid;
}
In TCA File
'feuseruid' => array(
'exclude' => 1,
'label' => 'Feuser Id',
'config' => array(
'type' => 'inline',
'foreign_table' => 'fe_users',
'minitems' => 0,
'maxitems' => 1,
'appearance' => array(
'collapseAll' => 0,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'showAllLocalizationLink' => 1
),
),
),
In Sql.php
feuseruid int(11) unsigned DEFAULT '0' NOT NULL,
In Controller
/**
* User Repository
*
* #var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
* #inject
*/
protected $userRepository;
$userObject = $this->userRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
$orders->setFeuseruid($userObject);
$this->yourRepository->add($orders);
i found the Error! The extensionBuilder writes wrong Data in the Model:
the right values are:
Model:
/*
*
* #var TYPO3\CMS\Extbase\Domain\Model\FrontendUser
*/
protected $fuID = NULL;
...
...
...
/**
* Returns the fuID
*
* #return TYPO3\CMS\Extbase\Domain\Model\FrontendUser fuID
*/
public function getFuID() {
return $this->fuID;
}
/*
* Sets the fuID
*
* #param string $fuID
* #return TYPO3\CMS\Extbase\Domain\Model\FrontendUser fuID
*/
public function setFuID(TYPO3\CMS\Extbase\Domain\Model\FrontendUser $fuID) {
$this->fuID = $fuID;
}
Controller:
/**
* User Repository
*
* #var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
* #inject
*/
protected $userRepository;
/**
* Den aktuell angemeldeten User auslesen
*
* #return \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
*/
public function getCurrentUser() {
if ($this->currentUser == NULL && $GLOBALS['TSFE']->fe_user->user['uid'] > 0) {
$this->currentUser = $this->userRepository->findByUid($GLOBALS['TSFE']->fe_user->user['uid']);
}
return $this->currentUser;
}
Hard to say without the appropriate view of your model. It's very likely, that you have a mismatch between your model and your TCA: On the one hand you are using an integer (setFuID()), on the other hand you have an object (foreign_table/foreign_class). Maybe it's working after you have adjusted this to be the same.