Cold_Class
Cold_Class

Reputation: 3484

How to realize inheritance in Typo3 6.2 Extension?

My goal is being able to:

  1. Create Expertise Entries in the backend (already accomplished)
  2. Create SubExpertise Entries in the backend
    • (same props as Expertise but they belong to one or many Expertise)
  3. Create AdditionalInfoTitles Entries in the backend
    • (they can belong to one or many Expertise OR SubExpertise)
    • I want to be able to choose Objects from all Expertise AND SubExpertise when creating a new entry

Right now I can only choose between all Expertise-Entries: enter image description here

That's why I thought about inheritance since then SubExpertise would be of the same type as Expertise and therefore automatically displayed in the Expertise list in a AdditionalInfoTitles entry. But that's just my theory and I'm kinda stuck in reality with typo3 TCA and other knowledge that I'm lacking...

In my extension builder I made following (don't mind the subExpertises property)enter image description here
Then I added expertise to the Overrides folder, because I'm trying to extend it with subexpertise:

<?php
if (!defined('TYPO3_MODE')) {
        die ('Access denied.');
}

$temporaryColumns = array (
        'expertise' => array(
        'exclude' => 1,
        'label' => 'LLL:EXT:appoints/Resources/Private/Language/locallang_db.xlf:tx_appoints_domain_model_subexpertise.expertise',
        'config' => array(
            'type' => 'select',
            'foreign_table' => 'tx_appoints_domain_model_subexpertise',
            'MM' => 'tx_appoints_subexpertise_expertise_mm',
            'size' => 10,
            'autoSizeMax' => 30,
            'maxitems' => 9999,
            'multiple' => 0,
            'wizards' => array(
                '_PADDING' => 1,
                '_VERTICAL' => 1,
                'edit' => array(
                    'module' => array(
                        'name' => 'wizard_edit',
                    ),
                    'type' => 'popup',
                    'title' => 'Edit',
                    'icon' => 'edit2.gif',
                    'popup_onlyOpenIfSelected' => 1,
                    'JSopenParams' => 'height=350,width=580,status=0,menubar=0,scrollbars=1',
                    ),
                'add' => Array(
                    'module' => array(
                        'name' => 'wizard_add',
                    ),
                    'type' => 'script',
                    'title' => 'Create new',
                    'icon' => 'add.gif',
                    'params' => array(
                        'table' => 'tx_appoints_domain_model_expertise',
                        'pid' => '###CURRENT_PID###',
                        'setValue' => 'prepend'
                    ),
                ),
            ),
        ),
    ),
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
        'tx_appoints_domain_model_expertise',
        $temporaryColumns
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
        'tx_appoints_domain_model_expertise',
        'expertise'
);

But I don't think I'm going into the right direction with this - Because I think this way I'm not gonna be able to add a SubExpertise in the backend separately from an Expertise - I already have the same problem with my Objects that extend fe_user because when creating them I usually have to go through a new User and then set the extension type - but this way I don't have separate listings of the different entities that extend fe_user.

Upvotes: 0

Views: 178

Answers (2)

Oliver Hader
Oliver Hader

Reputation: 4203

If the entity SubExpertise does not have a meaning in your domain model, Jigal's answer is perfect for your scenario. If it does have a meaning, you can achieve that using single table inheritance in Extbase.

class Expertise extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    // all common properties
}

class SubExpertise extends Expertise
{
    /**
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\[YourVendorName]\Appoints\Domain\Model\Expertise>
     */
    protected $expertises;

    public function __construct()
    {
      $this->expertises = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }

    public function getExpertises() {}
    public function setExpertises($expertises) {}
}

Via TypoScript then you have to define mapping rules, since both Expertise and SubExpertise would be stored in the same table tx_appoints_domain_model_subexpertise.

You'll find more details on single table inheritance in the Extbase book.

Upvotes: 1

Jigal van Hemert
Jigal van Hemert

Reputation: 722

I would get rid of the separation between Expertise and SubExpertise for the most part. According to your description a SubExpertise cannot have another SubExpertise as its parent, so you can adapt the select field that it only lists Expertises which have an empty parent field. By removing the difference the problem of selecting (Sub)Expertise's in AdditionalInfoTitles is removed; it's just one and the same type of objects.

If you need to differentiate in the presentation in the BE forms there are plenty of options to adjust the labels of the listed items, use a function of your own to build the list or even a custom form element.

In Extbase you can simply write a few functions in your repository to fetch Expertise's, SubExpertise's or both.

Upvotes: 1

Related Questions