Compare commits

..

No commits in common. "1ad6acbb84f235e29fc364e28fe2ee332a590eb9" and "cfcf8d2e8e9ec2b669fabff2eb3d3119b4237a9d" have entirely different histories.

4 changed files with 65 additions and 150 deletions

View File

@ -517,7 +517,7 @@
/** /**
* GridFieldNestedForm * GridFieldNestedForm
*/ */
$('.grid-field .col-listChildrenLink button').entwine({ $('.grid-field .col-listChildrenLink a').entwine({
onclick: function(e) { onclick: function(e) {
let gridField = $(this).closest('.grid-field'); let gridField = $(this).closest('.grid-field');
let currState = gridField.getState(); let currState = gridField.getState();
@ -552,7 +552,7 @@
} }
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: $(this).attr('data-url'), url: $(this).attr('href'),
data: data, data: data,
headers: { headers: {
'X-Pjax': pjaxTarget 'X-Pjax': pjaxTarget
@ -572,7 +572,6 @@
} }
$(this).removeClass('font-icon-right-dir'); $(this).removeClass('font-icon-right-dir');
$(this).addClass('font-icon-down-dir'); $(this).addClass('font-icon-down-dir');
$(this).attr('aria-expanded', 'true');
} }
else { else {
$.ajax({ $.ajax({
@ -581,7 +580,6 @@
$(this).closest('tr').next('.nested-gridfield').hide(); $(this).closest('tr').next('.nested-gridfield').hide();
$(this).removeClass('font-icon-down-dir'); $(this).removeClass('font-icon-down-dir');
$(this).addClass('font-icon-right-dir'); $(this).addClass('font-icon-right-dir');
$(this).attr('aria-expanded', 'false');
} }
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();

View File

@ -6,10 +6,8 @@ use Exception;
use SilverStripe\Admin\ModelAdmin; use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\GridField\AbstractGridFieldComponent; use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
@ -18,11 +16,11 @@ use SilverStripe\Forms\GridField\GridField_DataManipulator;
use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_SaveHandler; use SilverStripe\Forms\GridField\GridField_SaveHandler;
use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridField_URLHandler;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\Forms\GridField\GridFieldStateAware; use SilverStripe\Forms\GridField\GridFieldStateAware;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\Filterable;
use SilverStripe\ORM\Hierarchy\Hierarchy; use SilverStripe\ORM\Hierarchy\Hierarchy;
use SilverStripe\ORM\SS_List; use SilverStripe\ORM\SS_List;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
@ -41,61 +39,26 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
{ {
use Configurable, GridFieldStateAware; use Configurable, GridFieldStateAware;
/**
* The key used in the post data to identify nested form data
*/
const POST_KEY = 'GridFieldNestedForm'; const POST_KEY = 'GridFieldNestedForm';
private static $allowed_actions = [ private static $allowed_actions = [
'handleNestedItem' 'handleNestedItem'
]; ];
/** private static $max_nesting_level = 10;
* The default max nesting level. Nesting further than this will throw an exception.
*
* @var boolean
*/
private static $default_max_nesting_level = 10;
/** /**
* @var string * @var string
*/ */
private $name; protected $name;
protected $expandNested = false;
/** protected $forceCloseNested = false;
* @var bool protected $gridField = null;
*/ protected $record = null;
private $expandNested = false; protected $relationName = 'Children';
protected $inlineEditable = false;
/** protected $canExpandCheck = null;
* @var bool protected $maxNestingLevel = null;
*/
private $forceCloseNested = false;
/**
* @var GridField
*/
private $gridField = null;
/**
* @var string
*/
private $relationName = 'Children';
/**
* @var bool
*/
private $inlineEditable = false;
/**
* @var callable|string
*/
private $canExpandCheck = null;
/**
* @var int
*/
private $maxNestingLevel = null;
public function __construct($name = 'NestedForm') public function __construct($name = 'NestedForm')
{ {
@ -104,24 +67,27 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
/** /**
* Get the grid field that this component is attached to * Get the grid field that this component is attached to
* @return GridField
*/ */
public function getGridField(): GridField public function getGridField()
{ {
return $this->gridField; return $this->gridField;
} }
/** /**
* Get the relation name to use for the nested grid fields * Get the relation name to use for the nested grid fields
* @return string
*/ */
public function getRelationName(): string public function getRelationName()
{ {
return $this->relationName; return $this->relationName;
} }
/** /**
* Set the relation name to use for the nested grid fields * Set the relation name to use for the nested grid fields
* @param string $relationName
*/ */
public function setRelationName(string $relationName) public function setRelationName($relationName)
{ {
$this->relationName = $relationName; $this->relationName = $relationName;
return $this; return $this;
@ -129,16 +95,18 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
/** /**
* Get whether the nested grid fields should be inline editable * Get whether the nested grid fields should be inline editable
* @return boolean
*/ */
public function getInlineEditable(): bool public function getInlineEditable()
{ {
return $this->inlineEditable; return $this->inlineEditable;
} }
/** /**
* Set whether the nested grid fields should be inline editable * Set whether the nested grid fields should be inline editable
* @param boolean $editable
*/ */
public function setInlineEditable(bool $editable) public function setInlineEditable($editable)
{ {
$this->inlineEditable = $editable; $this->inlineEditable = $editable;
return $this; return $this;
@ -146,8 +114,9 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
/** /**
* Set whether the nested grid fields should be expanded by default * Set whether the nested grid fields should be expanded by default
* @param boolean $expandNested
*/ */
public function setExpandNested(bool $expandNested) public function setExpandNested($expandNested)
{ {
$this->expandNested = $expandNested; $this->expandNested = $expandNested;
return $this; return $this;
@ -155,54 +124,49 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
/** /**
* Set whether the nested grid fields should be forced closed on load * Set whether the nested grid fields should be forced closed on load
* @param boolean $forceClosed
*/ */
public function setForceClosedNested(bool $forceClosed) public function setForceClosedNested($forceClosed)
{ {
$this->forceCloseNested = $forceClosed; $this->forceCloseNested = $forceClosed;
return $this; return $this;
} }
/** /**
* Set a callback function to check which items in this grid that should show the expand link * Set a callback to check which items in this grid that should show the expand link
* for nested gridfields. The callback should return a boolean value. * for nested gridfields. The callback should return a boolean value.
* You can either pass a callable or a method name as a string. * @param callable $checkFunction
*/ */
public function setCanExpandCheck(callable|string $callback) public function setCanExpandCheck($checkFunction)
{ {
$this->canExpandCheck = $callback; $this->canExpandCheck = $checkFunction;
return $this; return $this;
} }
/** /**
* Set the maximum nesting level allowed for nested grid fields * Set the maximum nesting level allowed for nested grid fields
* @param int $level
*/ */
public function setMaxNestingLevel(int $level) public function setMaxNestingLevel($level)
{ {
$this->maxNestingLevel = $level; $this->maxNestingLevel = $level;
return $this; return $this;
} }
/** public function getMaxNestingLevel()
* Get the max nesting level allowed for this grid field.
*/
public function getMaxNestingLevel(): int
{ {
return $this->maxNestingLevel ?: static::config()->get('default_max_nesting_level'); return $this->maxNestingLevel ?: $this->config()->max_nesting_level;
} }
/** protected function getNestingLevel($gridField)
* Check if we are currently at the max nesting level allowed.
*/
protected function atMaxNestingLevel(GridField $gridField): bool
{ {
$level = 0; $level = 0;
$controller = $gridField->getForm()->getController(); $c = $gridField->getForm()->getController();
$maxLevel = $this->getMaxNestingLevel(); while ($c && $c instanceof GridFieldDetailForm_ItemRequest) {
while ($level < $maxLevel && $controller && $controller instanceof GridFieldNestedFormItemRequest) { $c = $c->getController();
$controller = $controller->getController();
$level++; $level++;
} }
return $level >= $maxLevel; return $level;
} }
public function getColumnMetadata($gridField, $columnName) public function getColumnMetadata($gridField, $columnName)
@ -229,12 +193,14 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
public function getColumnContent($gridField, $record, $columnName) public function getColumnContent($gridField, $record, $columnName)
{ {
if ($this->atMaxNestingLevel($gridField)) { $nestingLevel = $this->getNestingLevel($gridField);
if ($nestingLevel >= $this->getMaxNestingLevel()) {
return ''; return '';
} }
$gridField->addExtraClass('has-nested'); $gridField->addExtraClass('has-nested');
if ($record->ID && $record->exists()) { if ($record->ID && $record->exists()) {
$this->gridField = $gridField; $this->gridField = $gridField;
$this->record = $record;
$relationName = $this->getRelationName(); $relationName = $this->getRelationName();
if (!$record->hasMethod($relationName)) { if (!$record->hasMethod($relationName)) {
return ''; return '';
@ -246,7 +212,7 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
return ''; return '';
} elseif (is_string($this->canExpandCheck) } elseif (is_string($this->canExpandCheck)
&& $record->hasMethod($this->canExpandCheck) && $record->hasMethod($this->canExpandCheck)
&& !$record->{$this->canExpandCheck}($record) && !$this->record->{$this->canExpandCheck}($record)
) { ) {
return ''; return '';
} }
@ -285,30 +251,15 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
} }
/** /**
* @param GridField $gridField * @param GridField $field
* @return array
*/ */
public function getHTMLFragments($gridField) public function getHTMLFragments($field)
{ {
/** if (DataObject::has_extension($field->getModelClass(), Hierarchy::class)) {
* If we have a DataObject with the hierarchy extension, we want to allow moving items to a new parent. $field->setAttribute('data-url-movetoparent', $field->Link('movetoparent'));
* This is enabled by setting the data-url-movetoparent attribute on the grid field, so that the client
* javascript can handle the move.
* Implemented in getHTMLFragments since this attribute needs to be added before any rendering happens.
*/
if (is_a($gridField->getModelClass(), DataObject::class, true)
&& DataObject::has_extension($gridField->getModelClass(), Hierarchy::class)
) {
$gridField->setAttribute('data-url-movetoparent', $gridField->Link('movetoparent'));
} }
return [];
} }
/**
* Handle moving a record to a new parent
*
* @return string
*/
public function handleMoveToParent(GridField $gridField, $request) public function handleMoveToParent(GridField $gridField, $request)
{ {
$move = $request->postVar('move'); $move = $request->postVar('move');
@ -375,30 +326,22 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
return $gridField->FieldHolder(); return $gridField->FieldHolder();
} }
/**
* Handle the request to show a nested item
*
* @param GridField $gridField
* @param HTTPRequest|null $request
* @param ViewableData|null $record
* @return HTTPResponse|RequestHandler
*/
public function handleNestedItem(GridField $gridField, $request = null, $record = null) public function handleNestedItem(GridField $gridField, $request = null, $record = null)
{ {
if ($this->atMaxNestingLevel($gridField)) { $nestingLevel = $this->getNestingLevel($gridField);
if ($nestingLevel >= $this->getMaxNestingLevel()) {
throw new Exception('Max nesting level reached'); throw new Exception('Max nesting level reached');
} }
$list = $gridField->getList(); if (!$record && $request) {
if (!$record && $request && $list instanceof Filterable) {
$recordID = $request->param('RecordID'); $recordID = $request->param('RecordID');
$record = $list->byID($recordID); $record = $gridField->getList()->byID($recordID);
} }
if (!$record) { if (!$record) {
return ''; return '';
} }
$relationName = $this->getRelationName(); $relationName = $this->getRelationName();
if (!$record->hasMethod($relationName)) { if (!$record->hasMethod($relationName)) {
throw new Exception('Invalid relation name'); return '';
} }
$manager = $this->getStateManager(); $manager = $this->getStateManager();
$stateRequest = $request ?: $gridField->getForm()->getRequestHandler()->getRequest(); $stateRequest = $request ?: $gridField->getForm()->getRequestHandler()->getRequest();
@ -406,6 +349,7 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$gridField->getState(false)->setValue($gridStateStr); $gridField->getState(false)->setValue($gridStateStr);
} }
$this->gridField = $gridField; $this->gridField = $gridField;
$this->record = $record;
$itemRequest = GridFieldNestedFormItemRequest::create( $itemRequest = GridFieldNestedFormItemRequest::create(
$gridField, $gridField,
$this, $this,
@ -429,19 +373,11 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
} }
} }
/**
* Handle the request to toggle a nested item in the gridfield state
*
* @param GridField $gridField
* @param HTTPRequest|null $request
* @param ViewableData|null $record
*/
public function toggleNestedItem(GridField $gridField, $request = null, $record = null) public function toggleNestedItem(GridField $gridField, $request = null, $record = null)
{ {
$list = $gridField->getList(); if (!$record) {
if (!$record && $request && $list instanceof Filterable) {
$recordID = $request->param('RecordID'); $recordID = $request->param('RecordID');
$record = $list->byID($recordID); $record = $gridField->getList()->byID($recordID);
} }
$manager = $this->getStateManager(); $manager = $this->getStateManager();
if ($gridStateStr = $manager->getStateFromRequest($gridField, $request)) { if ($gridStateStr = $manager->getStateFromRequest($gridField, $request)) {
@ -453,20 +389,14 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$state->$stateRelation = (int)$request->getVar('toggle'); $state->$stateRelation = (int)$request->getVar('toggle');
} }
/** public function Link($action = null)
* Get the link for the nested grid field
*/
public function Link($action = null): string
{ {
$link = Director::absoluteURL(Controller::join_links($this->gridField->Link('nested'), $action)); $link = Director::absoluteURL(Controller::join_links($this->gridField->Link('nested'), $action));
$manager = $this->getStateManager(); $manager = $this->getStateManager();
return $manager->addStateToURL($this->gridField, $link); return $manager->addStateToURL($this->gridField, $link);
} }
/** public function ToggleLink($action = null)
* Get the link for the toggle action
*/
public function ToggleLink($action = null): string
{ {
$link = Director::absoluteURL(Controller::join_links($this->gridField->Link('toggle'), $action, '?toggle=')); $link = Director::absoluteURL(Controller::join_links($this->gridField->Link('toggle'), $action, '?toggle='));
$manager = $this->getStateManager(); $manager = $this->getStateManager();
@ -487,13 +417,11 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$gridField->getState(false)->setValue($gridStateStr); $gridField->getState(false)->setValue($gridStateStr);
} }
foreach ($request->postVars() as $key => $val) { foreach ($request->postVars() as $key => $val) {
$list = $gridField->getList(); if (preg_match("/{$gridField->getName()}-{$postKey}-(\d+)/", $key, $matches)) {
if ($list instanceof Filterable && preg_match("/{$gridField->getName()}-{$postKey}-(\d+)/", $key, $matches)) {
$recordID = $matches[1]; $recordID = $matches[1];
$nestedData = $val; $nestedData = $val;
$record = $list->byID($recordID); $record = $gridField->getList()->byID($recordID);
if ($record) { if ($record) {
/** @var GridField */
$nestedGridField = $this->handleNestedItem($gridField, null, $record); $nestedGridField = $this->handleNestedItem($gridField, null, $record);
$nestedGridField->setValue($nestedData); $nestedGridField->setValue($nestedData);
$nestedGridField->saveInto($record); $nestedGridField->saveInto($record);
@ -505,10 +433,8 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
public function getManipulatedData(GridField $gridField, SS_List $dataList) public function getManipulatedData(GridField $gridField, SS_List $dataList)
{ {
if ($this->relationName == 'Children' if ($this->relationName == 'Children'
&& is_a($gridField->getModelClass(), DataObject::class, true)
&& DataObject::has_extension($gridField->getModelClass(), Hierarchy::class) && DataObject::has_extension($gridField->getModelClass(), Hierarchy::class)
&& $gridField->getForm()->getController() instanceof ModelAdmin && $gridField->getForm()->getController() instanceof ModelAdmin
&& $dataList instanceof Filterable
) { ) {
$dataList = $dataList->filter('ParentID', 0); $dataList = $dataList->filter('ParentID', 0);
} }

View File

@ -24,11 +24,9 @@ use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
/**
* Request handler class for nested grid field forms.
*/
class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
{ {
public function Link($action = null) public function Link($action = null)
{ {
return Controller::join_links($this->component->Link($this->record->ID), $action); return Controller::join_links($this->component->Link($this->record->ID), $action);
@ -74,10 +72,7 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
} }
if ($this->record->hasExtension(Hierarchy::class)) { if ($this->record->hasExtension(Hierarchy::class)) {
$config->addComponent($nestedForm = new GridFieldNestedForm(), GridFieldOrderableRows::class); $config->addComponent(new GridFieldNestedForm(), GridFieldOrderableRows::class);
// use max nesting level from parent component
$nestedForm->setMaxNestingLevel($this->component->getMaxNestingLevel());
/** @var GridFieldOrderableRows */ /** @var GridFieldOrderableRows */
$orderableRows = $config->getComponentByType(GridFieldOrderableRows::class); $orderableRows = $config->getComponentByType(GridFieldOrderableRows::class);
if ($orderableRows) { if ($orderableRows) {
@ -122,6 +117,8 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
$gridField->setAttribute('data-class', str_replace('\\', '-', $relationClass)); $gridField->setAttribute('data-class', str_replace('\\', '-', $relationClass));
$gridField->addExtraClass('nested'); $gridField->addExtraClass('nested');
$form = new Form($this, 'ItemEditForm', $fields, new FieldList()); $form = new Form($this, 'ItemEditForm', $fields, new FieldList());
$form->setStrictFormMethodCheck(false);
$form->disableSecurityToken();
$className = str_replace('\\', '-', get_class($this->record)); $className = str_replace('\\', '-', get_class($this->record));
$state = $this->gridField->getState()->GridFieldNestedForm; $state = $this->gridField->getState()->GridFieldNestedForm;

View File

@ -1,10 +1,4 @@
<button <a class="btn btn-secondary btn--no-text btn--icon-large <% if $Toggle == 'open' %>font-icon-down-dir<% else %>font-icon-right-dir<% end_if %> cms-panel-link list-children-link" data-pjax-target="$PjaxFragment" href="$Link" data-toggle="$ToggleLink"></a>
class="btn btn-secondary btn--no-text btn--icon-large <% if $Toggle == 'open' %>font-icon-down-dir<% else %>font-icon-right-dir<% end_if %> cms-panel-link list-children-link"
aria-expanded="<% if $Toggle == 'open' %>true<% else %>false<% end_if %>"
data-pjax-target="$PjaxFragment"
data-url="$Link"
data-toggle="$ToggleLink"
></button>
<% if $Toggle == 'open' %> <% if $Toggle == 'open' %>
$NestedField $NestedField
<% else %> <% else %>