Skip to content

Commit

Permalink
ENH Update code to reflect changes in silverstripe/cms
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 3, 2024
1 parent 52f2b69 commit c7db089
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/GridField.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/src/js/GridField.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const gridField = this.getGridField();
const state = gridField.getState().GridFieldSiteTreeAddNewButton;

state.pageType = this.val();
state.recordType = this.val();
gridField.setState('GridFieldSiteTreeAddNewButton', state);
}
});
Expand Down
34 changes: 18 additions & 16 deletions src/Forms/GridFieldSiteTreeAddNewButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace SilverStripe\Lumberjack\Forms;

use SilverStripe\CMS\Controllers\CMSPageAddController;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\DropdownField;
Expand Down Expand Up @@ -42,7 +43,8 @@ public function getAllowedChildren(?SiteTree $parent = null)
return array();
}

$nonHiddenPageTypes = SiteTree::page_type_classes();
$nonHiddenPageTypes = ClassInfo::getValidSubClasses(SiteTree::class);
SiteTree::singleton()->updateAllowedSubClasses($nonHiddenPageTypes);
$allowedChildren = $parent->allowedChildren();
$children = array();
foreach ($allowedChildren as $class) {
Expand All @@ -66,22 +68,22 @@ public function getHTMLFragments($gridField)
{
$state = $gridField->State->GridFieldSiteTreeAddNewButton;

$parent = SiteTree::get()->byId(Controller::curr()->currentPageID());
$parent = SiteTree::get()->byId(Controller::curr()->currentRecordID());

if ($parent) {
$state->currentPageID = $parent->ID;
$state->currentRecordID = $parent->ID;
}

$children = $this->getAllowedChildren($parent);
if (empty($children)) {
return array();
} elseif (count($children ?? []) > 1) {
$pageTypes = DropdownField::create('PageType', 'Page Type', $children, $parent->defaultChild());
$pageTypes = DropdownField::create('RecordType', 'Page Type', $children, $parent->defaultChild());
$pageTypes
->setFieldHolderTemplate(__CLASS__ . '_holder')
->addExtraClass('gridfield-dropdown no-change-track');

$state->pageType = $parent->defaultChild();
$state->RecordType = $parent->defaultChild();

if (!$this->buttonName) {
$this->buttonName = _t(
Expand All @@ -92,9 +94,9 @@ public function getHTMLFragments($gridField)
}
} else {
$keys = array_keys($children ?? []);
$pageTypes = HiddenField::create('PageType', 'Page Type', $keys[0]);
$pageTypes = HiddenField::create('RecordType', 'Page Type', $keys[0]);

$state->pageType = $keys[0];
$state->recordType = $keys[0];

if (!$this->buttonName) {
$this->buttonName = _t(
Expand Down Expand Up @@ -131,7 +133,7 @@ public function getActions($gridField)
}

/**
* Handles the add action, but only acts as a wrapper for {@link CMSPageAddController::doAdd()}
* Handles the add action, but only acts as a wrapper for CMSMain
*
* @param GridField $gridField
* @param string $actionName
Expand All @@ -147,20 +149,20 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
$tmpData = $tmpData['GridFieldSiteTreeAddNewButton'];

$data = array(
'ParentID' => $tmpData['currentPageID'],
'PageType' => $tmpData['pageType']
'ParentID' => $tmpData['currentRecordID'],
'RecordType' => $tmpData['recordType']
);

$controller = Injector::inst()->create(CMSPageAddController::class);
$controller = Injector::inst()->create(CMSMain::class);
$form = $controller->AddForm();

// pass current request to newly created controller
// pass current request down in case either of these needs it
$request = Controller::curr()->getRequest();
$controller->setRequest($request);
$form->getRequestHandler()->setRequest($request);

$form = $controller->AddForm();
$form->loadDataFrom($data);

return $controller->doAdd($data, $form);
return $form->doAdd($data, $form);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridFieldSiteTreeEditButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getColumnContent($gridField, $record, $columnName)
// which can make the form readonly if no edit permissions are available.

$data = ArrayData::create([
'Link' => $record->CMSEditLink(),
'Link' => $record->getCMSEditLink(),
'ExtraClass' => $this->getExtraClass(),
]);

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Lumberjack.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @author Michael Strong <[email protected]>
*
* @extends SiteTreeExtension<SiteTree&static>
* @extends Extension<SiteTree&static>
*/
class Lumberjack extends Extension
{
Expand Down

0 comments on commit c7db089

Please sign in to comment.