From 70d245dd6e9bf6b183eb33f2b65ea9c8a122bfe0 Mon Sep 17 00:00:00 2001 From: Sabina Talipova Date: Wed, 17 Jan 2024 10:15:57 +1300 Subject: [PATCH] ENH Add Readonly field status --- .../LinkPicker/tests/LinkPickerTitle-test.js | 14 ++++++++++++++ src/Controllers/LinkFieldController.php | 7 ++++++- src/Form/LinkField.php | 9 ++------- src/Form/LinkField_Readonly.php | 3 +-- src/Form/MultiLinkField.php | 6 ++---- src/Form/MultiLinkField_Readonly.php | 3 +-- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/client/src/components/LinkPicker/tests/LinkPickerTitle-test.js b/client/src/components/LinkPicker/tests/LinkPickerTitle-test.js index e87df1c4..b2064ba0 100644 --- a/client/src/components/LinkPicker/tests/LinkPickerTitle-test.js +++ b/client/src/components/LinkPicker/tests/LinkPickerTitle-test.js @@ -86,3 +86,17 @@ test('LinkPickerTitle main button should not fire the onClick callback while loa fireEvent.click(container.querySelector('button.link-picker__button')); expect(mockOnClick).toHaveBeenCalledTimes(0); }); + +test('LinkPickerTitle render() should have readonly class if cannot edit', () => { + const { container } = render( + + ); + expect(container.querySelectorAll('.readonly')).toHaveLength(1); +}); + +test('LinkPickerTitle render() should not have readonly class if can edit', () => { + const { container } = render( + + ); + expect(container.querySelectorAll('.readonly')).toHaveLength(0); +}); diff --git a/src/Controllers/LinkFieldController.php b/src/Controllers/LinkFieldController.php index f78f9f58..0e416c7d 100644 --- a/src/Controllers/LinkFieldController.php +++ b/src/Controllers/LinkFieldController.php @@ -310,7 +310,7 @@ private function createLinkForm(Link $link, string $operation): Form // Make readonly if fail can check if ($operation === 'create' && !$link->canCreate() || $operation === 'edit' && !$link->canEdit() - || $this->isReadOnlyField() && !$link->canEdit() + || $this->isReadOnlyField() ) { $form->makeReadonly(); } @@ -467,6 +467,11 @@ private function ownerRelationFromRequest(): string if (!$ownerRelation) { $this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_RELATION', 'Invalid ownerRelation')); } + + if (strpos($ownerRelation, '_Readonly') !== false) { + $ownerRelation = str_replace('_Readonly', '', $ownerRelation); + } + return $ownerRelation; } } diff --git a/src/Form/LinkField.php b/src/Form/LinkField.php index f9b2591d..165741b0 100644 --- a/src/Form/LinkField.php +++ b/src/Form/LinkField.php @@ -2,13 +2,10 @@ namespace SilverStripe\LinkField\Form; -use LogicException; use SilverStripe\Forms\FormField; use SilverStripe\LinkField\Models\Link; use SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait; use SilverStripe\LinkField\Form\Traits\LinkFieldGetOwnerTrait; -use SilverStripe\ORM\DataObject; -use SilverStripe\ORM\DataObjectInterface; /** * Allows CMS users to edit a Link object. @@ -63,13 +60,11 @@ public function getSchemaDataDefaults() return $data; } - /** + /** * Changes this field to the readonly field. */ public function performReadonlyTransformation() { - $copy = $this->castedCopy(LinkField_Readonly::class); - - return $copy; + return $this->castedCopy(LinkField_Readonly::class); } } diff --git a/src/Form/LinkField_Readonly.php b/src/Form/LinkField_Readonly.php index e41b2b91..3dd9b8b7 100644 --- a/src/Form/LinkField_Readonly.php +++ b/src/Form/LinkField_Readonly.php @@ -3,7 +3,6 @@ namespace SilverStripe\LinkField\Form; use SilverStripe\Forms\HiddenField; -use SilverStripe\Forms\ReadonlyField; class LinkField_Readonly extends LinkField { @@ -13,7 +12,7 @@ class LinkField_Readonly extends LinkField public function Field($properties = []) { // Readonly field for display - $field = new LinkField($this->name, $this->title); + $field = new LinkField($this->name . '_Readonly', $this->title); $field->setValue($this->Value()); $field->setForm($this->form); diff --git a/src/Form/MultiLinkField.php b/src/Form/MultiLinkField.php index a454fd14..a870a279 100644 --- a/src/Form/MultiLinkField.php +++ b/src/Form/MultiLinkField.php @@ -156,13 +156,11 @@ private function loadFrom(DataObject $record): void parent::setValue($value); } - /** + /** * Changes this field to the readonly field. */ public function performReadonlyTransformation() { - $copy = $this->castedCopy(MultiLinkField_Readonly::class); - - return $copy; + return $this->castedCopy(MultiLinkField_Readonly::class); } } diff --git a/src/Form/MultiLinkField_Readonly.php b/src/Form/MultiLinkField_Readonly.php index 4cc4768c..4e73f684 100644 --- a/src/Form/MultiLinkField_Readonly.php +++ b/src/Form/MultiLinkField_Readonly.php @@ -3,7 +3,6 @@ namespace SilverStripe\LinkField\Form; use SilverStripe\Forms\HiddenField; -use SilverStripe\Forms\ReadonlyField; class MultiLinkField_Readonly extends MultiLinkField { @@ -13,7 +12,7 @@ class MultiLinkField_Readonly extends MultiLinkField public function Field($properties = []) { // Readonly field for display - $field = new MultiLinkField($this->name, $this->title); + $field = new MultiLinkField($this->name . '_Readonly', $this->title); $field->setValue($this->getValueArray()); $field->setForm($this->form);