Add first item to collection on create (Slider with first Slide)

(Martin Ficzel) #1

Often it helps editors to directly add items to a collection to show how it should be used, or it makes sense to directly add some items since the empty-collection is not usefull.

An common example is a slider-content that should have the first slide already attached directly on create.

To achive this create a class that handles the afterNodeCreate-signal checke the type of the created node and acts as needed.

namespace Vendor\Site\CR\Signals\;

use TYPO3\Flow\Annotations as Flow;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TYPO3CR\Domain\Service\NodeTypeManager;
use TYPO3\TYPO3CR\Domain\Service\NodeServiceInterface;

 * @Flow\Scope("singleton")
class CreateDefaultContent

	 * @Flow\Inject
	 * @var NodeTypeManager
	protected $nodeTypeManager;

	 * @Flow\Inject
	 * @var NodeServiceInterface
	protected $nodeService;
	 * Callback to handle the afterNodeCreate signal
	 * @param NodeServiceInterface $node
	public function handleAfterNodeCreateSignal (NodeInterface $node) 
        // add a slide to new slider nodes
        if ($node->getNodeType()->isOfType('Vendor.Site:Content.Slider')) {

After that add a Classes\Vendor\Site\Package.php file to register the callback-method for the signal.

namespace Vendor\Site;

use TYPO3\Flow\Package\Package as BasePackage;
use TYPO3\TYPO3CR\Domain\Model\Node;
use Vendor\Site\CR\Signals\CreateDefaultContent;

class Package extends BasePackage {

    public function boot(\TYPO3\Flow\Core\Bootstrap $bootstrap)
        $dispatcher = $bootstrap->getSignalSlotDispatcher();
            Node::class, 'afterNodeCreate',
            CreateDefaultContent::class, 'handleAfterNodeCreateSignal'

NOTE: It is possible to use named/autocreated child nodes for this but this will lead to trouble as soon as node:repair is trying to fix the html-structure.

(Florian Grell) #2

Why is this leading to trouble? I have used that approach before and it seems to work just fine.

    'TYPO3.Neos:Content': TRUE
    'TYPO3.Neos:ContentCollection': TRUE
    label: 'Tab'`

    'TYPO3.Neos:Content': TRUE
    label: 'Tabs'
    inlineEditable: TRUE
      'My.Namespace:TabItem': TRUE
      '*': FALSE
      type: 'My.Namespace:TabItem'

Is there something I might have done incorrectly?

(Martin Ficzel) #3

If you do a node:repair the autocreated child nodes will be moved before the other nodes … so you risk uncontrolled node reordering.

(Florian Grell) #4

That’s a bug, isn’t it?

(Marv M) #5

Please add this to the NEOS How to’s.

That would be great =)

(Martin Ficzel) #6

It’s more an unspecified behavior. Another problem would be that if you later decide to delete the autocreated item it will appear again after node:repair.

(Martin Ficzel) #7

Sure … I see the FAQ as an incubator for how-tos

(Florian Grell) #8

I thought you can’t delete autocreated nodes?

(Martin Ficzel) #9

Hmm … might be … but that would also be an unexpected behavior. I would definitely avoid mixing of autocreated and normal child nodes for such a purpose.