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

Entry title reverts to previous value when publishing multiple drafts #9966

Closed
sidedwards opened this issue Oct 14, 2021 · 11 comments
Closed
Labels

Comments

@sidedwards
Copy link
Contributor

Description

When working with multiple drafts for the same Entry, the Entry title is not merged back into the working draft after the Entry is updated. This only applies to the Entry title; updates to field values are merged back into the draft as described in #4642 (comment).

Steps to reproduce

  1. Open an Entry and create two separate drafts.
  2. In the first draft, update the Entry title for the primary site.
  3. In the second draft, switch to another site and update the Entry title.
  4. Apply the first draft.
  5. In the second draft, Craft merges any field changes for the primary site, but not the title.
  6. Apply the second draft.
  7. See error. The primary site Entry title reverts to the original value.

Additional info

  • Craft version: 3.7.14
  • PHP version: 7.4.16
  • Database driver & version: MySQL 5.7.35
  • Plugins & versions: N/a

Resources

CleanShot.2021-10-14.at.11.08.21.mp4

References

@sidedwards sidedwards added the bug label Oct 14, 2021
@cruiser12
Copy link

ok, we could narrow down the faulty behaviour to the autosave functionality for the entry drafts. we disabled it in general.php with 'autosaveDrafts' => false, and the problem disappears. Hope that helps, because it seems that several issues here relate to it.

@sidedwards
Copy link
Contributor Author

@cruiser12, I noticed the same. It only seems to occur with draft merging.

@chadwells
Copy link

Can confirm this also happens for us, although in slightly different circumstances. The difference is we are not using a multisite config, and we've noticed it primarily with entries with Matrix fields. We DO also have 'autosaveDrafts' => false set in our config.

Craft version: 3.7.17.1
PHP version: 7.3.29

Happy to provide any additional info.

@brandonkelly
Copy link
Member

brandonkelly commented Oct 25, 2021

Just fixed this for the next release.

@chadwells The issue I just fixed is definitely specific to a multi-site entry; if you are seeing a similar issue on a single-site entry, please post a new issue with more details and steps to reproduce.

brandonkelly added a commit that referenced this issue Oct 26, 2021
@brandonkelly
Copy link
Member

Craft 3.7.18 is out now with that fix.

@jornwildenbeest
Copy link

jornwildenbeest commented Aug 15, 2023

@brandonkelly I am experiencing this problem in Craft CMS 4 with multi site.

Craft CMS: 4.4.7.1
PHP: 8.1

Is it possible this is related to this issue or should I make a new issue for this?

@brandonkelly
Copy link
Member

@jornwildenbeest Please update to the latest 4.4 release as a starting point, and verify you can still reproduce. If so, yes, post a new issue about it with steps to reproduce. Thanks!

@jornwildenbeest
Copy link

jornwildenbeest commented Aug 21, 2023

@brandonkelly I traced the issue back to a third party plugin I am using to create the drafts in different languages.

This is plugin is using the following code to create the drafts:

$sourceEntry = Entry::findOne(['id' => $entryId, 'siteId' => $sourceSiteId, 'status' => null]);
$targetEntry = Entry::findOne(['id' => $entryId, 'siteId' => $destinationSiteId, 'status' => null]);

//TODO Handle different section propagation methods ?

$newTitle = Deepl::getInstance()->api->translateString(
    $sourceEntry->title,
    $sourceSite->language,
    $destinationSite->language
);
$targetEntry->title = $newTitle;
$targetEntry->slug = "";

$newValues = Deepl::getInstance()->mapper->entryMapper($sourceEntry, $targetEntry);

// Save the translated version of the entry as a new draft
/** @var Element|DraftBehavior $element */
$draft = Craft::$app->getDrafts()->createDraft(
    $targetEntry,
    Craft::$app->getUser()->getIdentity()->id,
    'Translation',
    'Creating DeepL translation',
);
$draft->setCanonical($targetEntry);
$draft->setScenario(Element::SCENARIO_ESSENTIALS);
$draft->setFieldValues($newValues);

Do you know if there is anything wrong with this code that can cause this kind of issue?

@brandonkelly
Copy link
Member

Actually yes. The updated title and slug are getting set on $targetEntry before the draft is created, so craft\services\Drafts::createDraft() is going to assume that it’s looking at the canonical entry’s title/slug, rather than something that was modified on the draft.

If those attributes are set on the $draft instead, things should start working as expected:

$sourceEntry = Entry::findOne(['id' => $entryId, 'siteId' => $sourceSiteId, 'status' => null]);
$targetEntry = Entry::findOne(['id' => $entryId, 'siteId' => $destinationSiteId, 'status' => null]);

//TODO Handle different section propagation methods ?

$newTitle = Deepl::getInstance()->api->translateString(
    $sourceEntry->title,
    $sourceSite->language,
    $destinationSite->language
);

$newValues = Deepl::getInstance()->mapper->entryMapper($sourceEntry, $targetEntry);

// Save the translated version of the entry as a new draft
/** @var Element|DraftBehavior $element */
$draft = Craft::$app->getDrafts()->createDraft(
    $targetEntry,
    Craft::$app->getUser()->getIdentity()->id,
    'Translation',
    'Creating DeepL translation',
);
$draft->setCanonical($targetEntry);
$draft->setScenario(Element::SCENARIO_ESSENTIALS);
$draft->setFieldValues($newValues);
$draft->title = $newTitle;
$draft->slug = "";

// I'm assuming that is followed by
// Craft::$app->getElements()->saveElement($draft);

@jornwildenbeest
Copy link

@brandonkelly that solved the problem indeed, thanks!

@brandonkelly
Copy link
Member

Glad to hear!

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

5 participants