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

View File

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

View File

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

View File

@ -1,10 +1,4 @@
<button
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>
<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>
<% if $Toggle == 'open' %>
$NestedField
<% else %>