Examples of using Laravel Collections in Drupal
Since starting to work with Laravel as well as Drupal and Symfony, watching Adam Wathan’s Refactoring to Collections course as well as lessons on Laracasts, I’ve become a fan of Laravel’s Illuminate Collections and the object-orientated pipeline approach for interacting with PHP arrays.
In fact I’ve given a talk on using Collections outside Laravel and have written a Collection class module for Drupal 7.
I’ve also tweeted several examples of code that I’ve written within Drupal that use Collections, and I thought it would be good to collate them all here for reference.
Thanks again to Tighten for releasing and maintaining the tightenco/collect library that makes it possible to pull in Collections via Composer.
Putting @laravelphp's Collection class to good use, cleaning up some of my @drupal 8 code. Thanks @TightenCo for the Collect library! pic.twitter.com/Bn1UfudGvp
— Oliver Davies (@opdavies) August 18, 2017
Putting more @laravelphp Collections to work in my @drupal code today. 😁 pic.twitter.com/H8xDTT063X
— Oliver Davies (@opdavies) February 14, 2018
I knew that you could specify a property like 'price' in Twig and it would also look for methods like 'getPrice()', but I didn't know (or had maybe forgotten) that @laravelphp Collections does it too.
This means that these two Collections return the same result.
Nice! 😎 pic.twitter.com/2g2IfThzdy
— Oliver Davies (@opdavies) June 20, 2018
More @laravelphp Collection goodness, within my #Drupal8 project! pic.twitter.com/mWgpNbNIrh
— Oliver Davies (@opdavies) August 10, 2018
Some more #Drupal 8 fun with Laravel Collections. Loading the tags for a post and generating a formatted string of tweetable hashtags. pic.twitter.com/GbyiRPzIRo
— Oliver Davies (@opdavies) August 23, 2018