denis
denis

Reputation: 1473

TYPO3: How to get a domain property field with type ObjectStorage<SomeSubType> mapped from a fluid form in extbase?

Does someone know how to get a domain property field with type ObjectStorage mapped from a fluid form in extbase on TYPO3 10LTS?

I've got the following property in the domain object:

    /**
     * @var ObjectStorage<Employee>|null
     */
    protected ?ObjectStorage $companyEmployees = null;

And this is one field of the fluid template:

<f:form.textfield property="companyEmployees.0.firstName" id="companyEmployees.0.firstName" additionalAttributes="{required: 'required'}"/>

firstName is one property of the Employee domain object.

I would expect to get an ObjectStorage filled with one Employee object filled with the firstName. But I'm getting the following exception:

Core: Exception handler (WEB): Uncaught TYPO3 Exception: Return value of TYPO3\CMS\Extbase\Property\TypeConverter\ObjectStorageConverter::getTypeOfChildProperty() must be of the type string, null returned 

Any ideas on how to do it correctly?

Upvotes: 0

Views: 700

Answers (1)

denis
denis

Reputation: 1473

Ok the answer to that question is pretty simple. The object mapper of extbase does try to find out the correct type for each property of the object. The first way of doing that is by trying to find the setter method of the property. In my case this would be the setCompanyEmployees.

And this one has to be build in the following way:

    /**
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\MyVendor\Ext\Domain\Model\Employee> $companyEmployees
     */
    public function setCompanyEmployees($companyEmployees): void
    {
        $this->companyEmployees = $companyEmployees;
    }

It is important to note that you have to use the FQN in the PHPdoc @param notation. In other parts of extbase it is no longer necessary to use the FQN since 10LTS but here it is!

Secondly it is important to note that you have to omit the type of the parameter. So in my case I had public function setCompanyEmployees(ObjectStorage $companyEmployees) which did not work since you can't have a subtype in PHP (there is nothing like ObjectStorage<Employee>) and extbase prefers the parameter type over the phpdoc @param notation. And that leads to the error mentioned in the question since the parameter type would be ObjectStorage without the subtype.

So to sum up use public function setCompanyEmployees($companyEmployees) and the FQN in the @param notation and the mapping does work perfectly in Typo3 10LTS.

Upvotes: 2

Related Questions