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

Duplicating a page with Widgets doesn't assign new SideBarID #190

Open
jfullerton opened this issue Apr 21, 2022 · 2 comments
Open

Duplicating a page with Widgets doesn't assign new SideBarID #190

jfullerton opened this issue Apr 21, 2022 · 2 comments

Comments

@jfullerton
Copy link

This was supposedly fixed before in #58 and I think #188 might also be referencing this issue.

Expected Behavior

Duplicating a page should also duplicate the WidgetArea and Widgets in it, and the resulting new page be assigned a SideBarID corresponding to the duplicated WidgetArea.

Actual Behavior

Duplicating a page does create copies of the original WidgetArea and Widgets but the resulting page's SideBarID still points to the original page's WidgetArea.

Steps to Reproduce the Problem

  1. Create a new Silverstripe project
composer create-project silverstripe/installer <project-name>
  1. Add Widgets module and Blog module (for default widgets)
composer require silverstripe/widgets silverstripe/blog
  1. Add WidgetPageExtension to Page in config
Page:
  extensions:
    - SilverStripe\Widgets\Extensions\WidgetPageExtension
  1. Choose a page in the CMS, add a widget, Save & Publish.
  2. Duplicate the page, rename the widget on the new page, Save & Publish.
  3. The name of the widget has changed on the original page.
  4. Looking in the database, both the Widget and WidgetArea have been duplicated, but the duplicated page still has the original page's SideBarID.

Specifications

  • PHP 8.0.17
  • Silverstripe CMS 4.10.1
  • Silverstripe Framework 4.10.4
  • Silverstripe Blog 3.9.0
  • Silverstripe Widgets 2.2.0
@dhensby
Copy link
Contributor

dhensby commented Apr 21, 2022

Is this all pages or just child pages? It looks like from the Page extension that maybe there could be a problem if InheritSideBar is true... (which is default) and the page is a child page... I can't really see immediately why this would happen on parent pages, though...


edit: It definitely looks like this feature could use some test coverage ;)

@jfullerton
Copy link
Author

Yeah, this is happening on all pages, I tried both top level pages and child pages. Unchecking "Inherit Sidebar From Parent" doesn't change the result.

Stepping through the onBeforeDuplicate function on WidgetPageExtension, everything seems fine.

public function onBeforeDuplicate($duplicatePage)
{
if ($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate();
foreach ($sideBar->Items() as $originalWidget) {
$widget = $originalWidget->duplicate(false);
$widget->ParentID = $duplicateWidgetArea->ID;
$widget->write();
}
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
}
return $duplicatePage;
}

The values being assigned were all correct and new records were being created. Even the SIdeBarID assigned on this line was also correct, but once the function returns, it apparently doesn't stick or is overwritten?

$duplicatePage->SideBarID = $duplicateWidgetArea->ID;

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