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

Duplicate content on same entry from one site to another #744

Open
hadomskyi opened this issue May 24, 2023 · 3 comments
Open

Duplicate content on same entry from one site to another #744

hadomskyi opened this issue May 24, 2023 · 3 comments
Labels

Comments

@hadomskyi
Copy link

hadomskyi commented May 24, 2023

What question would you like to ask?

Hi all,

We are working on copy content functionality.

Could you please help me to understand, is there anything wrong what we do when we try to duplicate content on same entry from one site to another. We use this code:

    /**
     * @param ElementInterface $from
     * @param ElementInterface $to
     *
     * @throws ElementNotFoundException
     * @throws Exception
     * @throws InvalidFieldException
     * @throws Throwable
     */
    private function copyEntryContent(ElementInterface $from, ElementInterface $to): void
    {
        ...

        $fieldLayout = $from->getFieldLayout();
        $fields = $fieldLayout ? $fieldLayout->getFields() : [];

        foreach ($fields as $field) {
            
            ...

            // Check if the field is of Neo type and the required classes and methods are available
            if (
                get_class($field) === CraftliltpluginParameters::BENF_NEO_FIELD
                && class_exists('benf\neo\Plugin')
                && method_exists('benf\neo\Plugin', 'getInstance')
            ) {
                // Get the Neo plugin instance
                /** @var \benf\neo\Plugin $neoPluginInstance */
                $neoPluginInstance = call_user_func(['benf\neo\Plugin', 'getInstance']);

                // Get the Neo plugin Fields service
                /** @var \benf\neo\services\Fields $neoPluginFieldsService  */
                $neoPluginFieldsService = $neoPluginInstance->get('fields');

                // Clear current neo field value
                $neoField = $to->getFieldValue($field->handle);
                foreach ($neoField as $block) {
                    Craft::$app->getElements()->deleteElement($block);
                }
                Craft::$app->getElements()->saveElement($to);

                // Duplicate the blocks for the field
                $neoPluginFieldsService->duplicateBlocks($field, $from, $to);

                continue;
            }

           ...

        }
    }

We're delighted with the Neo plugin for Craft CMS! Thanks for developing this fantastic tool that has greatly improved our content management capabilities. Your hard work is truly appreciated!

Thanks in advance!

@ttempleton
Copy link
Contributor

There shouldn't be any need to delete the blocks belonging to $to before duplicating the $from blocks over - those blocks should get deleted automatically during the duplication.

Other than that, your code should work - I've just tested a content migration based on your code to duplicate blocks from the same entry between sites, and that worked. If it isn't working for you after removing the old block deletion code, is there an error that occurs or does the target site just end up with no blocks? Also, which Craft CMS and Neo versions are you using?

@hadomskyi
Copy link
Author

hadomskyi commented May 25, 2023

@ttempleton thanks for quick reply.

There shouldn't be any need to delete the blocks belonging to $to before duplicating the $from blocks over - those blocks should get deleted automatically during the duplication.

In some cases blocks getting duplicated, if we don't clean it before copying. Not sure what can cause this and it is hard to investigate that, because different users has different plugin versions

I've just tested a content migration based on your code to duplicate blocks from the same entry between sites, and that worked

Is there any chance to check it on drafts? For example we have 4 sites (languages) and we create 4 drafts for each language, to keep changes for each language in separate draft (but each draft has version for all languages)

But for some reason other drafts not getting updated when we do apply one of them (I mean Neo blocks are not getting updated):

 $draft = Craft::$app->getDrafts()->applyDraft($draft);

What is happening:

First draft merged and content applied to entry
Second draft is not getting updated (merge canonical changes, even we call $draft->mergeCanonicalChanges() before applying it)
...
Last draft will apply only it is changes.

Neo is 2.12.5 (draft issue reproducible on 2.13.16 also)
Craft is 3.7.54

@ttempleton
Copy link
Contributor

Hmm, your reply makes me think the draft blocks are losing track of their canonical blocks. duplicateBlocks() does assume the copied block is the canonical block if the target owner element isn't the canonical owner, so that might be causing your issue.

If you change the above-linked line to set 'canonicalId' => $target->getIsDerivative() ? $block->getCanonicalId() : null, does that resolve your issue? If not, then is there any chance you can send to plugins@spicyweb.com.au a database backup, composer.json/lock files, and the full version of the code you're running?

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

No branches or pull requests

2 participants