Walk provides a Craft-aware array walk method, plus some super-convenient console commands, allowing you to easily call Craft service methods on a collection of elements or values.
TL;DR.
You want to perform some action, in bulk, on a bunch of elements.
And you want to do this with several different sets of elements, and with several different bulk actions, without needing to write new code every time.
Applying a method to every item in a list is known as "walking" an array. In fact, PHP provides a method — array_walk()
— to do just that.
However, PHP's array_walk
method isn't aware of how Craft's components are set up, and it doesn't know anything about Elements. Walk provides a Craft-aware walk function — craftyArrayWalk()
— to run a Craft component method on each item in a list.
PHP Usage
First, you need to have a callable in mind. A callable is the method that will be run on each item in the list.
A callable is specified by its name as a string. With this plugin, a callable can be:
- a method in one of Craft's native components:
'component.method'
- a method in a plugin's module components:
'plugin.component.method'
- any accessible custom function:
'myCustomMethod'
- any native PHP function, e.g.
'strtolower'
Second, you need a list of stuff (an array).
Then, just call the craftyArrayWalk()
method, like this:
$success = WalkHelper::craftyArrayWalk($elements, $callable);
For example...
Before Walk, you might invoke a method on each item in a query loop, like this:
$myEntries = Entry::find()->all();
foreach ($myEntries as $entry)
{
Craft::$app->getElements->saveElement($entry);
}
With Walk, you can program functionally, using the special Craft-aware service syntax:
WalkHelper::craftyArrayWalk($myEntries, 'elements.saveElement');
CLI Usage
For extra convenience, Walk also provides a handy command line syntax for you to trigger bulk actions — for any Element type, and any callable — via the console:
Let's say you want to re-save all your blog entries...
./craft walk entries --section=blog --limit=null elements.saveElement
If you have a custom service method, and you want to run it once on each user:
./craft walk users --limit=null myPlugin.myComponent.myMethod
What if your custom method takes an element ID rather than an element object?
./craft walk entryIds myModule.myComponent.methodThatTakesAnId
Tada!
Or, perhaps you want to get a count of elements in a criteria, without actually doing anything to them?
./craft walk countEntries --section=blog
Finally — You can use the special --asJob
command option to schedule each walk step as a queue job, so you don't lock up your CLI on long-running tasks:
./craft walk entries elements.saveElement --asJob
./craft walk entryIds myModule.someComponent.aMethod --asJob
Learn more...
Check out the readme for more details. You'll find it pretty easy to copy/paste your way to success!
To install this plugin, copy the command above to your terminal.
This plugin doesn't have any reviews.