Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to copy (child) nodes from another node #60

Open
jonnitto opened this issue Jun 14, 2023 · 3 comments
Open

Possibility to copy (child) nodes from another node #60

jonnitto opened this issue Jun 14, 2023 · 3 comments

Comments

@jonnitto
Copy link
Member

With NodeTemplates version 1.2 I've created the possibility to create template sites where you can add content elements to have a fast quick start (e.g. for blog posts).

For this, I've created following mixin

'Base.Templates:Mixin.CreationDialog':
  ui:
    creationDialog:
      elements:
        templateNodeIdentifier:
          type: reference
          ui:
            label: Template
            editorOptions:
              nodeTypes: ['Base.Templates:Mixin.Template']
  options:
    template:
      childNodes:
        mainContentCollection:
          when: '${data.templateNodeIdentifier}'
          name: main
          options:
            childNodesToCopy: "${q(node).find('#' + data.templateNodeIdentifier).children('main').children().get()}"

And following Package.php

<?php

namespace Base\Templates;

use Base\Templates\Service\ChildNodeCopyService;
use Flowpack\NodeTemplates\Template;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Package\Package as BasePackage;

/**
 * The Node Templates Magic Package
 */
class Package extends BasePackage
{
    /**
     * @param Bootstrap $bootstrap The current bootstrap
     * @return void
     */
    public function boot(Bootstrap $bootstrap)
    {
        $dispatcher = $bootstrap->getSignalSlotDispatcher();

        $dispatcher->connect(
            Template::class,
            "nodeTemplateApplied",
            ChildNodeCopyService::class,
            "copyChildNodesAfterTemplateApplication"
        );
    }
}

The ChildNodeCopyService.php looks like that

<?php

namespace Base\Templates\Service;

use Flowpack\NodeTemplates\Service\EelEvaluationService;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Eel\Package as EelPackage;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Service\NodeOperations;

/**
 */
class ChildNodeCopyService
{
    /**
     * @var EelEvaluationService
     * @Flow\Inject
     */
    protected $eelEvaluationService;

    /**
     * @var NodeOperations
     * @Flow\Inject
     */
    protected $nodeOperations;

    /**
     * @param NodeInterface $node
     * @param array $context
     * @param array $options
     * @return void
     */
    public function copyChildNodesAfterTemplateApplication(
        NodeInterface $node,
        array $context,
        array $options
    ): void {
        // Copy child nodes from template
        if (
            isset($options["childNodesToCopy"]) &&
            preg_match(
                EelPackage::EelExpressionRecognizer,
                $options["childNodesToCopy"]
            )
        ) {
            $childNodes = $this->eelEvaluationService->evaluateEelExpression(
                $options["childNodesToCopy"],
                $context
            );
            /** @var NodeInterface $childNode */
            foreach ($childNodes as $childNode) {
                $this->nodeOperations->copy($childNode, $node, "into");
            }
        }
    }
}

With the new version, it is not possible to create this helpful feature. Should we create an API for that, to make this possible?

@mhsdesign
Copy link
Contributor

Funnily we just discussed this but dindt think of this perfectly legit and cool usecase - thanks for reminding us.
#50

Our options to solve this usecase are:

1 - use a custom NodeCreationHandler yourself -> but i dislike this, as the api will change with 9.0 (one could extract it into an own package then)
2 - introduce this as a feature with support for neos8 and neos9

@theilm
Copy link
Collaborator

theilm commented Jun 14, 2023

Yes that's the use case I was mentioning in #50 - I introduced the signal exactly for that (cool that you are using it :)). It will be more complex to solve this for neos9, but I think it should be possible - and I agree, it should be part of the NodeTemplates package.

@bweinzierl
Copy link

We are using it too for every customer. We have a template section nodetype where the editor is able to create template sections in a hidden part of the document tree. When creating sections on any page the creation dialogue will ask if a template should be used and if so the childnodes will be copied. This is a killer feature imho.

This uses the mentioned logic with a custom datasource for making the created templates selectable.

So +1 for reimplementing this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants