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

FEATURE: Assets Eel helper for Neos.Media #4155

Merged
merged 7 commits into from
Apr 19, 2023
Merged

Conversation

Sebobo
Copy link
Member

@Sebobo Sebobo commented Apr 1, 2023

This helper provides access to assets in Neos by either searching by Tag, AssetCollection, or
a searchTerm combined with optionally Tags and an AssetCollection.

Review instructions

Add some assets to collections and tag them, then use the helpers in Fusion like this:

assetsByTag = ${Neos.Media.Assets.findByTag('MyTag')}
assetsByCollection = ${Neos.Media.Assets.findByCollection('MyCollection')}
assetsBySearchTerm = ${Neos.Media.Assets.findBySearchTerm('alice')}

Checklist

  • Code follows the PSR-2 coding style
  • Tests have been created, run and adjusted as needed
  • The PR is created against the lowest maintained branch
  • Reviewer - PR Title is brief but complete and starts with FEATURE|TASK|BUGFIX
  • Reviewer - The first section explains the change briefly for change-logs
  • Reviewer - Breaking Changes are marked with !!! and have upgrade-instructions

@Sebobo
Copy link
Member Author

Sebobo commented Apr 1, 2023

I thought also about adding datasource to allow the selection of tags and collections, but that would require adding Neos as dependency to Neos.Media, which is Quatsch.

But it would help a lot with making it possible to select Tags and Collections as node property combined with this helper.
So an alternative would be to add them to Neos.Neos instead or to Flowpack.MediaUI 🤔

And currently the searchTerm method is not including the optional tags and collection in its name. But somehow findBySearchTermAndTagsAndCollections is also silly...

@Sebobo Sebobo marked this pull request as ready for review April 1, 2023 05:13
@kitsunet
Copy link
Member

kitsunet commented Apr 1, 2023

I would suggest creating separete methods for the respective ObjectType|string inputs, eg. findByLabel and findByLabelName or something, same for the other. Makes for a cleaner interface?

@mhsdesign
Copy link
Member

I would suggest creating separete methods for the respective ObjectType|string inputs, eg. findByLabel and findByLabelName or something, same for the other. Makes for a cleaner interface?

i think we should follow the mentality of the new eel helpers for the ESCR ... idont know exactly how they look like, but they must have the same challenge of dtos vs primitives ...

@kitsunet
Copy link
Member

kitsunet commented Apr 1, 2023

I would suggest creating separete methods for the respective ObjectType|string inputs, eg. findByLabel and findByLabelName or something, same for the other. Makes for a cleaner interface?

i think we should follow the mentality of the new eel helpers for the ESCR ... idont know exactly how they look like, but they must have the same challenge of dtos vs primitives ...

I would definitely hard suggest it there too 😂

@Sebobo
Copy link
Member Author

Sebobo commented Apr 1, 2023

And do you guys think I should add any other methods from the AssetRepo?

@bwaidelich
Copy link
Member

bwaidelich commented Apr 1, 2023

i think we should follow the mentality of the new eel helpers for the ESCR

There is not really a (new) mentality as far as I know. But I would suggest to see Eel helpers as adapters from the loosely typed fusion world into a strictly typed PHP world and therefore keep union types where it makes sense (i.e. where multiple variants mean the same thing).
e.g. I found it rather confusing to have findByLabel and findByLabelName on a fusion level.

But I think @kitsunet's comment was re findByTag and I agree that findByTag and findByTagLabel are two different things.

More concretely:

I think this is totally fine and makes it easier to use a helper:

function someHelper(SomeValueObject|string|null $foo): ?string {
    if ($foo === null) {
        return null;
    }
    if (is_string($foo)) {
       $foo = SomeValueObject::fromString($foo);
    }
    // ... process
}

While for the following I would suggest to split it up:

function someHelper(SomeEntity|string|null $foo): ?string {
    if ($foo === null) {
        return null;
    }
    if (is_string($foo)) {
       $foo = $this->someRepository->findByFoo($foo);
    }
    // ... process
}

@Sebobo Sebobo force-pushed the feature/media-ee-helpers branch 2 times, most recently from 7763e71 to 6f4874e Compare April 1, 2023 16:33
@Sebobo Sebobo requested a review from mhsdesign April 1, 2023 17:20
This helper provides access to assets in Neos
by either searching by Tag, AssetCollection, or
a searchTerm combined with optionally Tags
and an AssetCollection.
@markusguenther
Copy link
Member

As @bwaidelich and @kitsunet talked about it. Do we want to adjust the helper like the proposal?

Copy link
Member

@bwaidelich bwaidelich left a comment

Choose a reason for hiding this comment

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

As @bwaidelich and @kitsunet talked about it.

We haven't really talked about it and I might have misinterpreted Christians point.

Do we want to adjust the helper like the proposal?

Actually my suggestion was to keep it separate methods.. But thinking about it again: Tags are a special entity (and IMO they should have never been an entity but a value object)

So maybe it makes sense to adjust findByTag() to allow entity or string (=label) like Christian suggested because you won't have an instance of the tag in many places.

And maybe the same for findByCollection.. I'm not sure

Neos.Media/Classes/Eel/AssetsHelper.php Outdated Show resolved Hide resolved
Neos.Media/Classes/Eel/AssetsHelper.php Outdated Show resolved Hide resolved
Comment on lines 99 to 103
/**
* @param Tag[] $tags
* @return QueryResultInterface<AssetInterface> | null
*/
public function search(string $searchTerm, array $tags = [], AssetCollection $collection = null): ?QueryResultInterface
Copy link
Member

Choose a reason for hiding this comment

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

For this one it makes a lot of sense, too, IMO:

Suggested change
/**
* @param Tag[] $tags
* @return QueryResultInterface<AssetInterface> | null
*/
public function search(string $searchTerm, array $tags = [], AssetCollection $collection = null): ?QueryResultInterface
/**
* @param Tag[]|string[] $tags
* @return QueryResultInterface<AssetInterface> | null
*/
public function search(?string $searchTerm, array $tags = [], AssetCollection|string $collection = null): ?QueryResultInterface

@Sebobo
Copy link
Member Author

Sebobo commented Apr 5, 2023

So I revert back to my first version that allowed both strings and objects for both methods?

@bwaidelich
Copy link
Member

So I revert back to my first version that allowed both strings and objects for both methods?

Sorry, I didn't follow this PR from the beginning and didn't know that this was the first version?
The suggestions just reflect my personal opinion and I'm not even sure about it – feel free to ignore me ;)

@bwaidelich
Copy link
Member

bwaidelich commented Apr 5, 2023

OK, I think I finally understood Christians comment (I had read it looking at the current version).

I'd say: Lets keep the separate methods as is.
But maybe we can change search as suggested? (i.e. allow term to be null and tags to be an array of strings)?

@mhsdesign
Copy link
Member

I dindt completely follow the discussion, but can we later extract the conclusion into an issue or something, so we know how to write eel helpers in the future and dont have to re discuss this again ^^

The asset helper can handle tags and collections as string and instances to make the API simpler.
@markusguenther
Copy link
Member

The search does not work with tag labels, as the repository does not handle strings. If this is wanted, we need to transform all strings into tag instances first.

Also added some tests.

@markusguenther
Copy link
Member

@bwaidelich Does this fit better?

@bwaidelich
Copy link
Member

Does this fit better?

IMO it's great like this, but this is just my personal preference and @kitsunet opposed this solution if I got it right?

The search does not work with tag labels, as the repository does not handle strings.
If this is wanted, we need to transform all strings into tag instances first.

That's what I would expect to be honest, like:

$tagInstances = array_filter(array_map(fn (string|Tag $tag) => is_string($tag) ? $this->tagRepository->findOneByLabel($tag) : $tag));

@kitsunet
Copy link
Member

It's fine by me, even though with the null (why did that come into play?) it now looks pretty convoluted with the type hints. I just think this will be hard to refactor later and fall on our feet sooner or later 😆 but if you all are fine with this, lets do it.

@bwaidelich
Copy link
Member

with the null (why did that come into play?)

I'm not sure, but IMO it's a good idea to be fault tolerant in Eel helpers, i.e. don't throw an exception for ${some.helper(foo)} if foo happens to be null.
And many existing helpers already behave that way (either because they don't have type hints or because we explicitly allowed null, e.g. https://github.com/neos/neos-development-collection/blob/8.3/Neos.Fusion/Classes/Eel/BaseUriHelper.php#L31 or https://github.com/neos/neos-development-collection/blob/8.3/Neos.Neos/Classes/Fusion/Helper/NodeHelper.php#L56)

@Sebobo
Copy link
Member Author

Sebobo commented Apr 12, 2023

Yes. Often people feed variables into helpers which are not set yet, so we should be gentle.

@Sebobo
Copy link
Member Author

Sebobo commented Apr 12, 2023

@markusguenther I can make the tags adjustment in the search method later like we discussed. Thx for taking care of the PR while I was tripping.

@Sebobo
Copy link
Member Author

Sebobo commented Apr 12, 2023

I made the changes but now wonder, whether I shouldn't add a new search method to the AssetsRepository as search by tag OR collection is pretty useless IMO.

@markusguenther
Copy link
Member

Wrote with @Sebobo yesterday, and we keep it as it is now.
So, the change of the AssetRepository can be a future improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

5 participants