PHP code example of digitalwerk / content_element_registry

1. Go to this page and download the library: Download digitalwerk/content_element_registry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

digitalwerk / content_element_registry example snippets



declare(strict_types=1);
namespace Vendor\Extension\EventListeners;

class ContentElementRegistryListener
{
    /**
     * @param RegisterContentElementRegistryClassEvent $event
     */
    public function __invoke(RegisterContentElementRegistryClassEvent $event): void
    {
        $contentElementRegistry = $event->getContentElementRegistry();
        $contentElementsClassMap = \Composer\Autoload\ClassMapGenerator::createMap(
            \TYPO3\CMS\Core\Core\Environment::getPublicPath() .
            '/typo3conf/ext/your_extension/Classes/ContentElement/'
        );
        foreach ($contentElementsClassMap as $elementClass => $elementClassPath) {
            $contentElementRegistry->registerContentElement(
                \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($elementClass)
            );
        }
    }
}


namespace \YourVendor\YourExtension\ContentElement;

use Digitalwerk\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{

}


namespace \YourVendor\YourExtension\ContentElement;

use Digitalwerk\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{

    /**
     * YourNewContentElement constructor.
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->addPalette(
            'default',
            'header, --linebreak--, bodytext'
        );
    }
}


    /**
     * @return array
     */
    public function getColumnsOverrides()
    {
        return [
            'bodytext' => [
                'config' => [
                    'enableRichtext' => true,
                ],
            ],
        ];
    }


declare(strict_types=1);
namespace \YourVendor\YourExtension\Domain\Model\ContentElement;

use DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement;

/**
 * Class YourNewContentElement
 * @package YourVendor\YourExtension\Domain\Model\ContentElement
 */
class YourNewContentElement extends ContentElement
{

}


defined('TYPO3') or die();

$ttContentNewColumns = [
    'new_field' => [
        'label' => 'New field',
        'config' => [
            'type' => 'input',
            'eval' => 'trim',
         ],
    ],
];

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('tt_content', $ttContentNewColumns);


namespace \YourVendor\YourExtension\ContentElement;

use YourVendor\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{

    /**
     * YourNewContentElement constructor.
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->addPalette(
            'default',
            'new_field'
        );
    }
}


$tempTca = [
    'ctrl' => [
        'typeicon_classes' => [
            'yourextension_yournewcontentelement_yournewrelation' => 'yourextension_yournewcontentelement_yournewrelation',
        ],
    ],
    'types' => [
        'yourextension_yournewcontentelement_yournewrelation' => [
                    'showitem' => '--palette--;;mediaPalette,',
                ],
    ],
    'palettes' => [
        'mediaPalette' => [
            'showitem' => 'title, media',
        ],
    ],
    'columns' => [
        'title' => [
            'label' => 'Title',
            'config' => [
                'type' => 'text',
            ],
        ],

    ],
];

$GLOBALS['TCA']['tx_contentelementregistry_domain_model_relation'] = array_replace_recursive($GLOBALS['TCA']['tx_contentelementregistry_domain_model_relation'], $tempTca);



namespace \YourVendor\YourExtension\ContentElement;

use YourVendor\ContentElementRegistry\ContentElement\AbstractContentElementRegistryItem;

class YourNewContentElement extends AbstractContentElementRegistryItem
{
  public function __construct()
    {
        parent::__construct();
        $this->addPalette(
            'default',
            'tx_contentelementregistry_relations'
        );
    }
}


namespace \YourVendor\YourExtension\Domain\Model\ContentElement;

use DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement;

/**
 * Class YourNewContentElement
 * @package YourVendor\YourExtension\Domain\Model\ContentElement
 */
class YourNewContentElement extends ContentElement
{
    /**
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement\YourNewRelation>
     */
    protected $relations = null;

    /**
     * @return ObjectStorage|null
     */
    public function getRelations(): ? ObjectStorage
    {
        return $this->relations;
    }

}


namespace \YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement;

use Digitalwerk\ContentElementRegistry\Domain\Model\Relation;

/**
 * Class YourNewRelation
 * @package YourVendor\YourExtension\Domain\Model\ContentElement\YourNewContentElement
 */
class YourNewRelation extends Relation
{
    /**
     * @var string
     */
    protected $type = '';

    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

}

namespace \YourVendor\YourExtension\Domain\Model\ContentElement;

use DigitalWerk\ContentElementRegistry\Domain\Model\ContentElement;

/**
 * Class YourNewContentElement
 * @package YourVendor\YourExtension\Domain\Model\ContentElement
 */
class YourNewContentElement extends ContentElement
{
   /**
    * @var array
    */
   protected $columnsMapping = [
       'tx_contentelementregistry_relations' => 'relations',
   ];

}