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

[5.x] Add select modifier #10183

Merged
merged 2 commits into from
May 23, 2024
Merged

Conversation

godismyjudge95
Copy link
Contributor

Sometimes it is useful to do more than just pluck a single set of values from an array/collection of items.

Similar to Laravel's Arr::only this PR adds an only modifier to handle those cases where you need more than one key.

Example Data:

$items = [
    ['title' => 'Bread', 'slug' => 'bread', 'price' => '2'],
    ['title' => 'LEGO Coffee', 'slug' => 'lego-coffee', 'price' => '37'],
];

Example Template:

{{ items | only('title', 'slug') | to_json }}

Example Output:

[{"title":"Bread","slug":"bread"},{"title":"LEGO Coffee","slug":"lego-coffee"}]

@jackmcdade
Copy link
Member

Does the Pluck modifier not do the same thing? https://statamic.dev/modifiers/pluck

@jasonvarga
Copy link
Member

Similar. Pluck grabs a single value and flattens the array. What he's proposing is to maintain the associative array but only specific keys in each.

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

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

I think this modifier should be called select. Copying Collection's select method:

https://laravel.com/docs/11.x/collections#method-select

Collection's only method works differently.

@jackmcdade
Copy link
Member

Ahh, thanks for the clarification! Carry on 🤘

@godismyjudge95
Copy link
Contributor Author

I think this modifier should be called select. Copying Collection's select method:

laravel.com/docs/11.x/collections#method-select

Collection's only method works differently.

Ah yeah good catch. Only is for filtering a single level array.

@godismyjudge95 godismyjudge95 changed the title [5.x] Add Only modifier [5.x] Add select modifier May 23, 2024
Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

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

I think you can probably also use ->select() on the collection now.

@godismyjudge95
Copy link
Contributor Author

I think you can probably also use ->select() on the collection now.

I don't believe that works when the items don't implement ArrayAccess.
I just tried replacing the ->map() call with:

$items = $value->select($keys);

And a bunch of the tests fail.

I think that is why the pluck modifier does this:

return method_exists($item, 'value') ? $item->value($key) : $item->get($key);

@jasonvarga
Copy link
Member

Fair!

@jasonvarga jasonvarga merged commit a621e1f into statamic:5.x May 23, 2024
17 checks passed
@godismyjudge95 godismyjudge95 deleted the add-only-modifier branch May 24, 2024 00:40
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.

None yet

3 participants