Fixed linting issues

This commit is contained in:
Niklas Forsdahl 2024-04-18 12:41:58 +03:00
parent c043220949
commit c415d43731
2 changed files with 50 additions and 11 deletions

View File

@ -172,9 +172,14 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
return ''; return '';
} }
if ($this->canExpandCheck) { if ($this->canExpandCheck) {
if (is_callable($this->canExpandCheck) && !call_user_func($this->canExpandCheck, $record)) { if (is_callable($this->canExpandCheck)
&& !call_user_func($this->canExpandCheck, $record)
) {
return ''; return '';
} elseif (is_string($this->canExpandCheck) && $record->hasMethod($this->canExpandCheck) && !$this->record->{$this->canExpandCheck}($record)) { } elseif (is_string($this->canExpandCheck)
&& $record->hasMethod($this->canExpandCheck)
&& !$this->record->{$this->canExpandCheck}($record)
) {
return ''; return '';
} }
} }
@ -182,7 +187,11 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$className = str_replace('\\', '-', get_class($record)); $className = str_replace('\\', '-', get_class($record));
$state = $gridField->State->GridFieldNestedForm; $state = $gridField->State->GridFieldNestedForm;
$stateRelation = $className.'-'.$record->ID.'-'.$this->relationName; $stateRelation = $className.'-'.$record->ID.'-'.$this->relationName;
if (!$this->forceCloseNested && (($this->expandNested && $record->$relationName()->count() > 0) || ($state && (int)$state->getData($stateRelation) === 1))) { $openState = $state && (int)$state->getData($stateRelation) === 1;
$forceExpand = $this->expandNested && $record->$relationName()->count() > 0;
if (!$this->forceCloseNested
&& ($forceExpand || $openState)
) {
$toggle = 'open'; $toggle = 'open';
} }
@ -228,7 +237,11 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
if ($id) { if ($id) {
// should be possible either on parent or child grid field, or nested grid field from parent // should be possible either on parent or child grid field, or nested grid field from parent
$parent = $to ? $list->byID($to) : null; $parent = $to ? $list->byID($to) : null;
if (!$parent && $to && $gridField->getForm()->getController() instanceof GridFieldNestedFormItemRequest && $gridField->getForm()->getController()->getRecord()->ID == $to) { if (!$parent
&& $to
&& $gridField->getForm()->getController() instanceof GridFieldNestedFormItemRequest
&& $gridField->getForm()->getController()->getRecord()->ID == $to
) {
$parent = $gridField->getForm()->getController()->getRecord(); $parent = $gridField->getForm()->getController()->getRecord();
} }
$child = $list->byID($id); $child = $list->byID($id);
@ -285,12 +298,19 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
return ''; return '';
} }
$manager = $this->getStateManager(); $manager = $this->getStateManager();
if ($gridStateStr = $manager->getStateFromRequest($gridField, $request ?: $gridField->getForm()->getRequestHandler()->getRequest())) { $stateRequest = $request ?: $gridField->getForm()->getRequestHandler()->getRequest();
if ($gridStateStr = $manager->getStateFromRequest($gridField, $stateRequest)) {
$gridField->getState(false)->setValue($gridStateStr); $gridField->getState(false)->setValue($gridStateStr);
} }
$this->gridField = $gridField; $this->gridField = $gridField;
$this->record = $record; $this->record = $record;
$itemRequest = GridFieldNestedFormItemRequest::create($gridField, $this, $record, $gridField->getForm()->getController(), $this->name); $itemRequest = GridFieldNestedFormItemRequest::create(
$gridField,
$this,
$record,
$gridField->getForm()->getController(),
$this->name
);
if ($request) { if ($request) {
$pjaxFragment = $request->getHeader('X-Pjax'); $pjaxFragment = $request->getHeader('X-Pjax');
$targetPjaxFragment = str_replace('\\', '-', get_class($record)).'-'.$record->ID.'-'.$this->relationName; $targetPjaxFragment = str_replace('\\', '-', get_class($record)).'-'.$record->ID.'-'.$this->relationName;
@ -349,7 +369,8 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
$gridField->getState(false)->setValue($value['GridState']); $gridField->getState(false)->setValue($value['GridState']);
} }
$manager = $this->getStateManager(); $manager = $this->getStateManager();
if ($gridStateStr = $manager->getStateFromRequest($gridField, $gridField->getForm()->getRequestHandler()->getRequest())) { $request = $gridField->getForm()->getRequestHandler()->getRequest();
if ($gridStateStr = $manager->getStateFromRequest($gridField, $request)) {
$gridField->getState(false)->setValue($gridStateStr); $gridField->getState(false)->setValue($gridStateStr);
} }
foreach ($value[self::POST_KEY] as $recordID => $nestedData) { foreach ($value[self::POST_KEY] as $recordID => $nestedData) {
@ -364,7 +385,10 @@ 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' && DataObject::has_extension($gridField->getModelClass(), Hierarchy::class) && $gridField->getForm()->getController() instanceof ModelAdmin) { if ($this->relationName == 'Children'
&& DataObject::has_extension($gridField->getModelClass(), Hierarchy::class)
&& $gridField->getForm()->getController() instanceof ModelAdmin
) {
$dataList = $dataList->filter('ParentID', 0); $dataList = $dataList->filter('ParentID', 0);
} }
return $dataList; return $dataList;

View File

@ -46,7 +46,8 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
$relationName = $this->component->getRelationName(); $relationName = $this->component->getRelationName();
$list = $this->record->$relationName(); $list = $this->record->$relationName();
if ($relationName == 'Children' && $this->record->hasExtension(Hierarchy::class)) { if ($relationName == 'Children' && $this->record->hasExtension(Hierarchy::class)) {
// we really need a HasManyList for Hierarchy objects, otherwise adding new items will not properly set the ParentID // we really need a HasManyList for Hierarchy objects,
// otherwise adding new items will not properly set the ParentID
$list = HasManyList::create(get_class($this->record), 'ParentID') $list = HasManyList::create(get_class($this->record), 'ParentID')
->setDataQueryParam($this->record->getInheritableQueryParams()) ->setDataQueryParam($this->record->getInheritableQueryParams())
->forForeignID($this->record->ID); ->forForeignID($this->record->ID);
@ -93,7 +94,17 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
$title = _t(get_class($this->record).'.'.strtoupper($relationName), ' '); $title = _t(get_class($this->record).'.'.strtoupper($relationName), ' ');
$fields = new FieldList( $fields = new FieldList(
$gridField = new GridField($this->component->getGridField()->getName().'['.GridFieldNestedForm::POST_KEY.']['.$this->record->ID.']', $title, $list, $config) $gridField = new GridField(
sprintf(
'%s[%s][%s]',
$this->component->getGridField()->getName(),
GridFieldNestedForm::POST_KEY,
$this->record->ID
),
$title,
$list,
$config
)
); );
if (!trim($title)) { if (!trim($title)) {
$gridField->addExtraClass('empty-title'); $gridField->addExtraClass('empty-title');
@ -137,7 +148,11 @@ class GridFieldNestedFormItemRequest extends GridFieldDetailForm_ItemRequest
])); ]));
} else { } else {
$items->push(ArrayData::create([ $items->push(ArrayData::create([
'Title' => _t('SilverStripe\\Forms\\GridField\\GridField.NewRecord', 'New {type}', ['type' => $this->record->i18n_singular_name()]), 'Title' => _t(
'SilverStripe\\Forms\\GridField\\GridField.NewRecord',
'New {type}',
['type' => $this->record->i18n_singular_name()]
),
'Link' => false 'Link' => false
])); ]));
} }