Skip to content

Commit

Permalink
ENH Add Readonly field status
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Jan 17, 2024
1 parent 1cf61bb commit 70d245d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
14 changes: 14 additions & 0 deletions client/src/components/LinkPicker/tests/LinkPickerTitle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPickerTitle {...makeProps({ canCreate: false })} />
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.readonly')).toHaveLength(1);
});

test('LinkPickerTitle render() should not have readonly class if can edit', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPickerTitle {...makeProps({ canCreate: true })} />
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.readonly')).toHaveLength(0);
});
7 changes: 6 additions & 1 deletion src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
}
9 changes: 2 additions & 7 deletions src/Form/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
3 changes: 1 addition & 2 deletions src/Form/LinkField_Readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace SilverStripe\LinkField\Form;

use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\ReadonlyField;

class LinkField_Readonly extends LinkField
{
Expand All @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions src/Form/MultiLinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 1 addition & 2 deletions src/Form/MultiLinkField_Readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace SilverStripe\LinkField\Form;

use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\ReadonlyField;

class MultiLinkField_Readonly extends MultiLinkField
{
Expand All @@ -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);

Expand Down

0 comments on commit 70d245d

Please sign in to comment.