Inheritance of TYPO3 models - typo3

I have a tricky question about the inheritance of TYPO3 models:
I want to extend the extension powermail with two seperate/independed (!) extensions. The first extend powermail TCA-forms and needs the new fields in the fluid template. The second extends also the TCA-forms, but not need an output in the frontend.
Now I use the mixed-ins hack from Franz Koch http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2010-September/006497.html which using the delete field as record type. But this solution works only for one extension. According to the loading of the TS persistence settings either the first or the second model of the extension will be loaded.
If I use an own field for the record type – eg. for the field record of powermail – I can use only one record type for the one extension, but can't access to the other.
Is there a way to "merge" ALL models related to the base model?
Next current TS config:
config.tx_extbase {
persistence {
classes {
# Using mixed-ins hack from Franz Koch to make map table extendable.
# #see http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2010-September/006497.html
Tx_Powermail_Domain_Model_Fields {
mapping.recordType = 0
subclasses.0 = Vendor\MyExtension\Domain\Model\Powermail\Fields
}
Vendor\MyExtension\Domain\Model\Powermail\Fields {
mapping {
tableName = tx_powermail_domain_model_fields
recordType = 0
columns {
tx_myfirstextension_field_one.mapOnProperty = FieldOne
}
}
}
}
}
}
ext_tables.php:
// Using mixed-ins hack from Franz Koch to make map table extendable.
// #see http://lists.typo3.org/pipermail/typo3-project-typo3v4mvc/2010-September/006497.html
$TCA['tx_powermail_domain_model_forms']['ctrl']['type'] = 'deleted';
$TCA['tx_powermail_domain_model_pages']['ctrl']['type'] = 'deleted';
$TCA['tx_powermail_domain_model_fields']['ctrl']['type'] = 'deleted';

Related

TYPO3 Extension: How to make a relation to another extension’s model optional?

I have an events extension (for TYPO3 9 LTS and 10 LTS), say MyVendor\MyEvents and a Locations extension, say MyVendor\MyLocations.
The Model MyVendor\MyEvents\Domain\Model\Events has a property eventLocation which is defined to be an object of MyVendor\MyLocations\Domain\Model\Locations.
Now I want to make the relation to MyVendor\MyLocations\Domain\Model\Locations optional. I have found a way for the TCA to show a different form field in the backend depending on the MyLocations extension being installed. But I have no idea how to make all the type definitions in the Events model conditional. They are crucial for the extension to work:
namespace MyVendor\MyEvents\Domain\Model
class Events extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* #var \MyVendor\MyLocations\Domain\Model\Locations
*/
protected $eventLocation = NULL;
/**
* #return \MyVendor\MyLocations\Domain\Model\Locations $eventLocation
*/
public function getEventLocation()
{
return $this->eventLocation;
}
/**
* #param \MyVendor\MyLocations\Domain\Model\Locations $eventLocation
* #return void
*/
public function setEventLocation(\MyVendor\MyLocations\Domain\Model\Locations $eventLocation)
{
$this->eventLocation = $eventLocation;
}
}
In case MyVendor\MyLocations is loaded it needs to be defined as above, in case it isn’t loaded it should be just an integer.
In the TCA I am using if (TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('my_locations')) for showing a different field in the backend form for an event.
The Locations Model is in a separate extension because I am using it in a third extension as well.
In your events extension you could setup a repository for locations. Then you can map this repository to your location extensions model via TypoScript.

How to give a fixed Uid to my Action

Hy,
I'm trying to call my action with allways a fixed Uid (configured by TS) so I could put a plugin on my page to register for a specific Event. And don't have to go over a Event List click the Event click register.
I tried the following which did not work out:
public function newAction(
\XYZ\xyz\Domain\Model\Registration $newRegistration = NULL,
\XYZ\xyz\Domain\Model\Event $event = 'DD8B2164290B40DA240D843095A29904'
)
The next didn't one work either!
public function newAction(
\XYZ\xyz\Domain\Model\Registration $newRegistration = NULL,
\XYZ\xyz\Domain\Model\Event $event = Null
) {
$myinstance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
'XYZ\\xyz\\Domain\\Model\\Event'
);
$event = $myinstance->findByUid('DD8B2164290B40DA240D843095A29904');
.......
}
So I was woundering is there a way to give my fixed Uid to the action?
In TYPO3 calling Extbase actions is done in the routing and dispatching components - to pass anything from the outside that is different from a numeric uid value a custom property TypeConverter would have to be implemented that transforms a particular string pattern into a value domain object of type Event.
However, there's a simpler approach by using configuration:
1) Provide configuration in TypoScript
Extbase uses a strong naming convention based on the extension name and optionally the plugin name. Thus, either tx_myextension or tx_myextension_someplugin can be used - latter is more specific for for according somePlugin. Besides that settings are automatically forwarded and provided in an Extbase controller context - accessible by $this->settings.
plugin.tx_xyz {
settings {
newActionEventIdentifier = DD8B2164290B40DA240D843095A29904
}
}
2) Retrieve data via repository
\XYZ\xyz\Domain\Repository\EventRepository
Use a dedicated EventRepository::findByIdentifier(string) method to retrieve the data. The property names are just assumptions since there are no explicit mentions how exactly the event data is persisted and whether it is persisted in a relational DBMS at all.
<?php
namespace XYZ\xyz\Domain\Repository;
class EventRepository
{
public function findByIdentifier($identifier)
{
$query = $this->createQuery();
$query->matching(
$query->equals('event_id', $identifier)
);
return $query->execute();
}
}
3) Putting all together in the according controller
The $event property was removed from the action since that entity is pre-defined and cannot be submitted from the outside (and to support the string to Event entity transformation a custom TypeConverter would be required as mentioned earlier).
public function newAction(
\XYZ\xyz\Domain\Model\Registration $newRegistration = null
) {
$event = $this->eventRepository->findByIdentifier(
$this->settings['newActionEventIdentifier']
);
if ($event === null) {
throw new \RuntimeException('No event found', 1522070079);
}
// the regular controller tasks
$this->view->assign(...);
}

TYPO3: get all FE users

I am trying to create a user management module. I would like to get all FE users.
This is my Controller:
/**
* #var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
* #inject
*/
protected $feUserRepository;
then I use:
$users = $this->feUserRepository->findAll();
$this->view->assign('users', $users);
but all I get is an empty object.
EDIT:
for some reason
$this->feUserRepository->findByUId(1);
does work but findAll() not...
This is because extbase will silently disable the respectStoragePage setting on the querySettings for a findByUid($uid) call.
So, you have two options:
Provide the correct storage pid in the TypoScript configuration of your plugin (plugin.tx_myextension.persistence.storagePid). This way, you will find every frontenduser that is stored on the given page.
You could implement your own FrontendUserRepository that extends the repository from extbase but disables the respectStoragePage for all calls (this way you'll get every frontendUser regardless of the page the record is stored on). Here is how you do it:
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository as ExtbaseFrontendUserRepository;
class FrontendUserRepository extends ExtbaseFrontendUserRepository
{
/**
* Disable respecting of a storage pid within queries globally.
*/
public function initializeObject()
{
$defaultQuerySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings:class);
$defaultQuerySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($defaultQuerySettings);
}
}
In your Controller you then inject your FrontendUserRepository. Then you should do the same for the FrontendUser Model and tell extbase afterwards that you are using the fe_users table for your Model:
config.tx_extbase {
persistence {
classes {
Vendor\MyExtension\Domain\Model\FrontendUser {
mapping {
tableName = fe_users
}
}
}
}
}

Problems with Symfony embedded forms

I am trying to achieve the following scenario:
Auction and Category entities (many-to-one). The Category entity has a one-to-many relationship to CategoryAttribute entity which allows an unlimited number of attributes of various types to be added to the category. Let's say a Cars category will have Make and Year attributes. The CategoryAttribute entity has widgetType property which defines how to render the attribute (input, select etc.), attributeValues which is the data for the attribute (populating the select etc.) and isRequired property to tell whether a property is required or not. So far so good. Managing the attributes was piece of cake BUT:
On the Auction side of things I want when the user selects a given category from the list to render all the attributes for that category to be filled in. This is translated to an related AuctionAttribute entity (many-to-one to the auction in attributes property in the class). The AuctionAttribute has reference to the CategoryAttribute, a attributeValue to hold the input or selected value.
Well, the whole AJAX request and fill in of the attributes for a selected category was not a problem. The problem(s) arise when I submit the form. Basically there are two issues.
How do we bind the attributes part of the form to the actual form for validation. Let's say we have Car category selected and Make attribute is required, how do we validate that this attribute?
How do we bind the attributes input to AuctionAttribute entity in that form?
I know that for embedded forms I need to hook up to the FormEvents::PRE_SUBMIT event but I am not sure how to transform the attribute to an Entity in there.
In terms of code, I have the following:
When getting the attributes for a category, I create a AuctionAttributeFormType and render it into a twig form helper and return the HTML back in the AJAX request:
$form = $this->createForm(new Type\AuctionAttributeFormType(), null, array('csrf_protection' => false));
foreach ($categoryAttributes as $attribute) {
$form->add('attribute_'.$attribute->getId(), $attribute->getWidgetType(), array('label' => $attribute->getName(), 'required' => $attribute->isRequired());
}
When the Auction form is submitted, I hook to the PRE_SUBMIT event and when whether there is a attribute submitted and it belongs to the set of attributes of the category but this is as far as I went before I got stuck:
$builder->addEventListener(
Form\FormEvents::PRE_SUBMIT, function (Form\FormEvent $event) {
$auction = $event->getData();
if (null !== $auction['category']) {
$categoryAttributes = $this
->repository
->findAttributesForCategory($auction['category'])
->getResult();
if (count($categoryAttributes) > 0) {
$attribute_values = array();
foreach ($categoryAttributes as $attribute) {
if (isset($auction['attribute_' . $attribute->getId()])) {
$attribute_values[$attribute->getId()] = $auction['attribute_' . $attribute->getId()];
}
}
}
}
}
);
I need to get the values from attribute_values array into AuctionAttribute entities bound to the Auction entity. Any idea how this could be achieved. I think it should be done through some kind of data transformer but I am not sure to what to transform that data - should it be a form->add field, or directly touch the Auction entity which is filled in with data.
Any suggestions?
EDIT:
I made it work with the use of Model transformer but now there is another problem, when editing the record, if there is more than one attribute, only the first one is populated with data. Here is a sample gist of the code:
https://gist.github.com/SvetlinStaev/86e066a865478e40718c
My suggestion is NOT to convert the submitted data via Event Listeners, but to use a Data Transformer, which you attach to a form field like so:
$formBuilder->add(
$formBuilder
->create('FIELD_NAME', 'FIELD_TYPE', [
... FIELD_OPTIONS ...
])
->addModelTransformer(new SomeModelTransformer())
)
And the "SomeModelTransformer" class should look like this:
class SeatingToNumberTransformer implements DataTransformerInterface
{
/**
* Transforms the object from the norm data to model data
* The norm data is the field value. Say you have an integer field, $normDataObject would be an int.
* In your case: you need to instantiate several new AuctionAttribute objects and persist them maybe
*/
public function transform($normDataObject)
{
$transformedObject = $this->someTransformAction($normDataObject);
return $transformedObject;
}
/**
* Reverts the transform
* in your case: from AuctionAttribute to int
*/
public function reverseTransform($modelDataObject)
{
$transformedObject = $this->someOtherTransformAction($modelDataObject);
return $transformedObject;
}
}
More info can be found here
If you need more help, just let me know.

Doctrine_Table Vs class That extends from BaseClass

I am a little confused in this Doctrine model concept , lets say we a table called "article"
Doctrine will generate class called
i am using Zend framework and Doctrine 1.2
models/generated/BaseArticle.php
models/ArticleTable.php
models/Article.php
Is it true to call the ArticleTable in the controller in this way
$tableArticle = Doctrine::getTable('Article');
then to save it in the Article Object like this
$article = new Article();
$fArticles = $tableArticle->getFeaturedArticles();
foreach ($fArticles as $fArticle) {
$article->fromArray($fArticle);
echo $article->title
}
Or I have to let the Article.php to call the ArticleTable ?
then to initiate an Article.php object in the controller ?
class Article extends BaseArticle
{
public function getFArticles()
{
$tableArticle = Doctrine::getTable('Article');
$obj = $tableArticle->getFeaturedArticles();
return $obj;
}
Article.php should not call ArticleTable.php unless really, really needed. In table classes you will only hold queries called by controller like:
$featuredArticles = ArticleTable::getInstance()->getFeatured() ;
Above code is simpler and you will have autocompletion in any IDE.
The reason not to call queries in Article.php is that you will have easier transition to Doctrine2 one day.
For a table call tbl_article or just article, doctrine will generate Article.php and BaseArticle.php. Base classes must not be changed manually.
Article class is where your logic goes. For example, you fetch list of ALL articles in database. When you display them, you want feature articles to have a star (just an example):
controller:
$allArticles = ArticleTable::getInstance()->findAll() ;
template (Smarty version here):
{foreach $allArticles as $article}
{if $article->isFeatured()} <img src=.. some image ..>{/if}
<h5>{$article->title}
{/foreach}
and the model class
class Article extends BaseArticle
{
const STATUS_FEATURED = 1 ;
public function isFeatured()
{
return $this->status == self::STATUS_FEATURED ;
}
}
All these are just some examples, in real life it is much more usefull.
And what are you actually trying to do with this fromArray($fArticle)? I don't see any point of that code.