Skip to content

Commit

Permalink
ENH Show a cannot create message when types is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 17, 2024
1 parent 663a13f commit 9710be3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions client/src/components/LinkPicker/LinkPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ const LinkPicker = ({ types, onModalSuccess, onModalClosed, canCreate }) => {
const shouldOpenModal = typeKey !== '';
const className = classnames('link-picker', 'form-control');
const typeArray = Object.values(types);
const message = i18n._t('LinkField.CANNOT_CREATE_LINK', 'Cannot create link');

if (!canCreate) {
if (!canCreate || typeArray.length === 0) {
return (
<div className={className}>
<div className="link-picker__cannot-create">
{i18n._t('LinkField.CANNOT_CREATE_LINK', 'Cannot create link')}
{message}
</div>
</div>
);
Expand Down
8 changes: 8 additions & 0 deletions client/src/components/LinkPicker/tests/LinkPicker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ test('LinkPickerMenu render() should display cannot create message if cannot cre
expect(container.querySelectorAll('.link-picker__cannot-create')).toHaveLength(1);
});

test('LinkPickerMenu render() should display cannot create message if types is empty', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPicker {...makeProps({ types: {} })} />
</LinkFieldContext.Provider>);
expect(container.querySelectorAll('.link-picker__menu-toggle')).toHaveLength(0);
expect(container.querySelectorAll('.link-picker__cannot-create')).toHaveLength(1);
});

test('LinkPickerMenu render() should display link type icon if can create', () => {
const { container } = render(<LinkFieldContext.Provider value={{ loading: false }}>
<LinkPicker {...makeProps({ canCreate: true })} />
Expand Down
1 change: 0 additions & 1 deletion lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ en:
UPDATE_LINK: 'Update link'
SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait:
INVALID_TYPECLASS: '"{class}": {typeclass} is not a valid Link Type'
INVALID_TYPECLASS_EMPTY: '"{class}": Allowed types cannot be empty'
SilverStripe\LinkField\Models\EmailLink:
EMAIL_FIELD: 'Email address'
LINKLABEL: 'Link to email address'
Expand Down
31 changes: 10 additions & 21 deletions src/Form/Traits/AllowedLinkClassesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*/
trait AllowedLinkClassesTrait
{
private array $allowed_types = [];
private array $allowedTypes = [];

private bool $hasSetAllowedTypes = false;

/**
* Set allowed types for LinkField
Expand All @@ -22,8 +24,9 @@ trait AllowedLinkClassesTrait
public function setAllowedTypes(array $types): static
{
if ($this->validateTypes($types)) {
$this->allowed_types = $types;
$this->allowedTypes = $types;
}
$this->hasSetAllowedTypes = true;

return $this;
}
Expand All @@ -33,7 +36,7 @@ public function setAllowedTypes(array $types): static
*/
public function getAllowedTypes(): array
{
return $this->allowed_types;
return $this->allowedTypes;
}

/**
Expand All @@ -43,16 +46,6 @@ public function getAllowedTypes(): array
*/
private function validateTypes(array $types): bool
{
if (empty($types)) {
throw new InvalidArgumentException(
_t(
__TRAIT__ . '.INVALID_TYPECLASS_EMPTY',
'"{class}": Allowed types cannot be empty',
['class' => static::class],
),
);
}

$validClasses = [];
foreach ($types as $type) {
if (is_subclass_of($type, Link::class)) {
Expand All @@ -72,8 +65,7 @@ private function validateTypes(array $types): bool
);
}
}

return count($validClasses) > 0;
return true;
}

/**
Expand Down Expand Up @@ -113,14 +105,11 @@ public function getTypesProps(): string
*/
private function genarateAllowedTypes(): array
{
$typeDefinitions = $this->getAllowedTypes() ?? [];

if (empty($typeDefinitions)) {
if (!$this->hasSetAllowedTypes) {
return LinkTypeService::create()->generateAllLinkTypes();
}

$result = array();
foreach ($typeDefinitions as $class) {
$result = [];
foreach ($this->getAllowedTypes() as $class) {
if (is_subclass_of($class, Link::class)) {
$type = Injector::inst()->get($class)->getShortCode();
$result[$type] = $class;
Expand Down
3 changes: 0 additions & 3 deletions tests/php/Traits/AllowedLinkClassesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ public function testSetAllowedTypesException(array $enabled)
public function provideTypesExceptionDataProvider() : array
{
return [
'allow all with empty array' => [
'enabled' => [],
],
'all all non-Link classes' => [
'enabled' => [DataObject::class, 'WrongClass', 1, true],
],
Expand Down

0 comments on commit 9710be3

Please sign in to comment.