-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Property Mapper from Extbase Fluid Book (#2074)
* Move Property Mapper from Extbase Fluid Book Releases: main, 11.5 * Update Documentation/ExtensionArchitecture/Extbase/Reference/Controller/PropertyMapping.rst Co-authored-by: Chris Müller <[email protected]> * Include real code Remove Example "Allow mapping of sub-properties" here is no method propertyMappingConfigurationBuilder in the Abstract Controller anymore and the PropertyMappingConfigurationBuilder is internal. Therefore removing the section for now Co-authored-by: lina.wolf <[email protected]> Co-authored-by: Chris Müller <[email protected]>
- Loading branch information
1 parent
9b909d3
commit 3c74441
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Documentation/CodeSnippets/Extbase/PropertyManager/IntegerMapping.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets | ||
.. Extracted from EXT:blog_example/Classes/Controller/PostController.php | ||
.. code-block:: php | ||
:caption: EXT:blog_example/Classes/Controller/PostController.php | ||
class PostController extends \FriendsOfTYPO3\BlogExample\Controller\AbstractController | ||
{ | ||
/** | ||
* This method demonstrates property mapping to an integer | ||
* @throws \TYPO3\CMS\Extbase\Property\Exception | ||
*/ | ||
protected function mapIntegerFromString(string $numberString = '42'): int { | ||
return $output = $this->propertyMapper->convert($numberString, 'integer'); | ||
} | ||
} | ||
25 changes: 25 additions & 0 deletions
25
Documentation/CodeSnippets/Extbase/PropertyManager/ObjectMapping.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets | ||
.. Extracted from EXT:blog_example/Classes/Controller/PostController.php | ||
.. code-block:: php | ||
:caption: EXT:blog_example/Classes/Controller/PostController.php | ||
use FriendsOfTYPO3\BlogExample\Domain\Model\Tag; | ||
class PostController extends \FriendsOfTYPO3\BlogExample\Controller\AbstractController | ||
{ | ||
/** | ||
* This method demonstrates property mapping to an object | ||
* @throws \TYPO3\CMS\Extbase\Property\Exception | ||
*/ | ||
protected function mapTagFromString(string $tagString = 'some tag'): Tag { | ||
$input = [ | ||
'name' => $tagString, | ||
]; | ||
return $this->propertyMapper->convert ( | ||
$input, | ||
Tag::class | ||
); | ||
} | ||
} | ||
27 changes: 27 additions & 0 deletions
27
Documentation/CodeSnippets/Extbase/PropertyManager/PropertyMapperInjection.rst.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.. Generated by https://github.com/TYPO3-Documentation/t3docs-codesnippets | ||
.. Extracted from EXT:blog_example/Classes/Controller/PostController.php | ||
.. code-block:: php | ||
:caption: EXT:blog_example/Classes/Controller/PostController.php | ||
use FriendsOfTYPO3\BlogExample\Domain\Repository\BlogRepository; | ||
use FriendsOfTYPO3\BlogExample\Domain\Repository\PersonRepository; | ||
use FriendsOfTYPO3\BlogExample\Domain\Repository\PostRepository; | ||
use TYPO3\CMS\Extbase\Property\PropertyMapper; | ||
class PostController extends \FriendsOfTYPO3\BlogExample\Controller\AbstractController | ||
{ | ||
/** | ||
* PostController constructor. | ||
* | ||
* Takes care of dependency injection | ||
*/ | ||
public function __construct( | ||
protected readonly BlogRepository $blogRepository, | ||
protected readonly PersonRepository $personRepository, | ||
protected readonly PostRepository $postRepository, | ||
protected readonly PropertyMapper $propertyMapper | ||
) { | ||
} | ||
} | ||
38 changes: 38 additions & 0 deletions
38
...entation/ExtensionArchitecture/Extbase/Reference/Controller/PropertyMapping.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. include:: /Includes.rst.txt | ||
.. index:: | ||
Extbase; PropertyMapper | ||
.. _extbase_property_mapping: | ||
|
||
=================== | ||
Property mapping | ||
=================== | ||
|
||
Extbase provides a property mapper to convert different values, like integers | ||
or arrays, to other types, like strings or objects. | ||
|
||
In this example, we provide a string that will be converted to an integer: | ||
|
||
.. include:: /CodeSnippets/Extbase/PropertyManager/IntegerMapping.rst.txt | ||
|
||
Conversion is done by using the :php:`TYPO3\CMS\Extbase\Property\PropertyMapper::convert()` | ||
method. | ||
|
||
.. note:: | ||
The :php:`PropertyMapper` has to be injected before it can be used: | ||
|
||
.. include:: /CodeSnippets/Extbase/PropertyManager/PropertyMapperInjection.rst.txt | ||
|
||
|
||
How to use property mappers | ||
=========================== | ||
|
||
This example shows a simple conversion of a string into a model: | ||
|
||
.. include:: /CodeSnippets/Extbase/PropertyManager/ObjectMapping.rst.txt | ||
|
||
The result is a new instance of :php:`FriendsOfTYPO3\BlogExample\Domain\Model\Tag` | ||
with defined property `name`. | ||
|
||
.. note:: | ||
The property mapper will not check the validation rules. The result will be | ||
whatever the input is. |