Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Allowed link types #137

Conversation

sabina-talipova
Copy link
Contributor

@sabina-talipova sabina-talipova commented Dec 7, 2023

Description

  • Added a new trait AllowedLinkClassesTrait that adds methods to set available link types or exclude them.
  • The Registry::class has been removed.
  • Files and methods for working with GraphQL have also been removed
  • Added a service class to generate available existing Link classes

Parent issue

@sabina-talipova sabina-talipova force-pushed the pulls/4/link-type-per-field-2 branch 10 times, most recently from 4dd66d5 to 7368ff8 Compare December 7, 2023 22:29
@sabina-talipova sabina-talipova marked this pull request as ready for review December 7, 2023 22:30
Copy link

@maxime-rainville maxime-rainville left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good solid first pass. I tested it locally and it works great.

There wasn't an explicit AC for this, but I think logically we should have an exception thrown if you try to save a link of a type that is disallowed for the field. e.g. Your LinkField is configured to only allow EmailLink, but you do a manual AJAX query to save a SiteTreeLink into it.

README.md Outdated Show resolved Hide resolved
src/Services/LinkTypeService.php Outdated Show resolved Hide resolved
src/Services/LinkTypeService.php Outdated Show resolved Hide resolved
src/Services/LinkTypeService.php Outdated Show resolved Hide resolved
src/Services/LinkTypeService.php Show resolved Hide resolved
src/Traits/AllowedLinkClassesTrait.php Outdated Show resolved Hide resolved
*/
public function setDisabledTypes(array $types): static
{
if (is_array($types) || !empty($types)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is_array($types) || !empty($types)) {
if (!empty($types)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

src/Traits/AllowedLinkClassesTrait.php Outdated Show resolved Hide resolved
src/Traits/AllowedLinkClassesTrait.php Outdated Show resolved Hide resolved
src/Traits/AllowedLinkClassesTrait.php Outdated Show resolved Hide resolved
@sabina-talipova sabina-talipova force-pushed the pulls/4/link-type-per-field-2 branch 4 times, most recently from 421a198 to 5a2c284 Compare December 12, 2023 01:31
Copy link

@maxime-rainville maxime-rainville left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest concern is that this won't work with Elemental.

I left a link to how to react props so it works with Elemental. If it doesn't make sense let me know, and we can go through it together.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/Form/Traits/AllowedLinkClassesTrait.php Show resolved Hide resolved
src/Form/Traits/AllowedLinkClassesTrait.php Show resolved Hide resolved
src/Form/Traits/AllowedLinkClassesTrait.php Outdated Show resolved Hide resolved
src/Form/Traits/AllowedLinkClassesTrait.php Show resolved Hide resolved
src/Models/Link.php Show resolved Hide resolved
src/Services/LinkTypeService.php Show resolved Hide resolved
src/Traits/AllowedLinkClassesTrait.php Outdated Show resolved Hide resolved
@sabina-talipova sabina-talipova force-pushed the pulls/4/link-type-per-field-2 branch 3 times, most recently from 9605dfe to 4df1552 Compare December 14, 2023 22:40
@sabina-talipova
Copy link
Contributor Author

sabina-talipova commented Dec 17, 2023

#137 (comment)
Updated. Now user gets InvalidArgumentException if he's trying to pass empty array or non-Link type classes in setAllowedTypes.

@sabina-talipova sabina-talipova force-pushed the pulls/4/link-type-per-field-2 branch 3 times, most recently from 6d0cae5 to 6981fb6 Compare December 18, 2023 01:58
Copy link

@maxime-rainville maxime-rainville left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's almost there. Just a few ambiguities left to address.

*/
public function getShortCode(): string
{
return strtolower(str_replace([' ', 'Link'], '', ClassInfo::shortName($this))) ?? '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor tweak.

  • ClassInfo::shortName won't return spaces, so you don't need to remove spaces.
  • It would a bit safer to only remove the Link part if it's at the end of the class name. So using rtrim would be a bit safer here.
Suggested change
return strtolower(str_replace([' ', 'Link'], '', ClassInfo::shortName($this))) ?? '';
return strtolower(rtrim(ClassInfo::shortName($this), 'Link')) ?? '';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

src/Form/Traits/AllowedLinkClassesTrait.php Show resolved Hide resolved
* Set allowed types for LinkField
* @param string[] $types
*/
public function setAllowedTypes(array $types): static

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's our thinking when an empty array is passed to this method?

  • Do we want to throw an exception?
  • Do we want to unset any restriction and go back to allowing all Link types?

Copy link
Contributor Author

@sabina-talipova sabina-talipova Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put my comment here #137 (comment).
I think an empty array is invalid argument.

@@ -1,2 +1,2 @@
<input $AttributesHTML />
<div data-field-id="$ID" data-schema-component="$SchemaComponent" class="entwine-linkfield"></div>
<div data-field-id="$ID" data-schema-component="$SchemaComponent" class="entwine-linkfield" data-types="$TypesProps"></div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't need to be handle here and now, but we'll probably want to pass all our props in a single attribute here. Otherwise, every time we'll need to make our bootstrapping logic more complex to handle things like read only state.

That's more of a comment. Nothing to action at this moment.

}

/**
* @dataProvider allLinkTypesDataProvider

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have only one test case, you shouldn't be using a data provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@sabina-talipova sabina-talipova force-pushed the pulls/4/link-type-per-field-2 branch from 6981fb6 to 84824c7 Compare December 18, 2023 19:17
Copy link

@maxime-rainville maxime-rainville left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happiness has been achieved.

@maxime-rainville maxime-rainville merged commit c171ae8 into silverstripe:4 Dec 19, 2023
10 checks passed
@maxime-rainville maxime-rainville deleted the pulls/4/link-type-per-field-2 branch December 19, 2023 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants