Extending EntityFieldQuery

Posted on 09/18/12
EntityFieldQuery on drupal.org
I learned most of my coding skillz on the streets. I didn't have much formal training in CS before working with Drupal, and that means I don't have a lot of experience in in OOP. I've read through most of PHP Object-Oriented Solutions by David Powers and its started to help me see things the OOP way. EntityFieldQuery (EFQ) is like a playground for OOP fun. Here, I'll show you how to extend the existing EFQ class to create some helpful shortcuts for yourself. Take it and run with it, and maybe you can come up with something powerful. EntityFieldQuery is a simple yet powerful query builder specifically designed for entities and their attached fields. It can sometimes be restrictive (you can't really do a join, some database information might not be available if it isn't an entity property or a field, etc), but what it does, it does quite well. Here's a simple EFQ example:
<?php
$query
= new EntityFieldQuery();
$query->entityCondition('entity_type','node') // we want node entities.
     
->entityCondition('bundle','article') // of type article
     
->propertyCondition('status',1) // that are published
     
->fieldCondition('field_example_boolean','value',1); // with an example boolean field set to true
$result = $query->execute();
?>
This query will pull up the nids of any published nodes of type article that have a true value set on the example boolean field. As I've been using EFQ a lot, I've started to get annoyed by having to do those first few lines over and over. Frequently, it's nodes that I want, and it's often the published ones. Wouldn't it be nice to not have to add those same entity and property conditions each time? BEHOLD the power of OOP:
<?php
class NodeFieldQuery extends EntityFieldQuery {
  function
__construct($node_type, $status = 1) {
   
$this->entityCondition('entity_type', 'node')
         ->
entityCondition('bundle', $node_type)
         ->
propertyCondition('status', $status);
  }
}
?>
Pretty nice eh? You'd use it like this:
<?php
$query
= new NodeFieldQuery('article');
$query->range(0,10);
$result = $query->execute();
?>
That result will have the nids of 10 published article nodes. If you find yourself using EFQ a lot, you might find some value in extending the class with your own time-saving and code-simplifying solutions.

Comments

koosha
12/22/12 1:25 pm

your blog posts are wonderfull, all of them, thanks.

Home Depot Window Screens will renew your old windowIf you possess painful sunshine getting into your property using your windows, then you definately ought to understand that precisely what christian louboutin shoes you would like may be the solar screens, therefore we have just that in this particular website too Companies must take into account the needs and demands of owners and Christian Louboutin "solve problems" in a manner appropriateHowever, all of this formulation is of no use, if you won't stick to your trading plan If their finest team isn't competent to transcribe louis vuitton that audio nicely, you might should pay additional price regarding it Should a situation arise causing delay, the writing services should be quick to communicate problems encountered and do everything louis vuitton they can to deal with any resulting delaysDrink wares for travel it could be a promotional travel mug, tumbler or cups Also fine emotional stability is significant to any athlete michael kors outlet online and there are a few Extreme Sports where there are evident benefits of clear rules and regulations When I questioned my Twitter followers for his or her expectations when working louis vuitton bags with businesses on Twitter, I had been advised that most importantly of all, it's the response that matters They consume minimum space and let you store maximum objectsStep 8: Make louis vuitton outlet online it official: Once you've designed a few thousand dollars worth of sales, you must really register yourself as a business The keyword should also be within the domain's name as gucci bags well as the page's title for the absolute best results Any special need as well as factors in both options are easily attended to with the control over the pattern louis vuitton Use a lot of banners in your website to attract attention to the productA basedown triangle refers to a narrow forehead that widens at the cheek and chin areasIf you louis vuitton handbags want to stay away from the beach and water, you are able to take a walk in the Sage Mountain Even after getting enrolled to the Medigap health insurance plans gucci it is not possible for the person to switch over to any other plan if he wishes to get the benefits of the Medigap policies They hire people to do michael kors outlet work for them and in exchange they pay them for that work Issues never build and hosted a WordPress michael kors weblog prior to, it's simple in support of has a day possibly even to learn the ropes Research and researches produced independently around the evaluation of making use of automated business expense management plans confirmed that the overall processing expenses of claims are significantly lowered to additional than half prior on the michael kors outlet implementation of the program Item inquiry is the initial step inside the method This is not the best scenario especially when a person is out there camping and use of louis vuitton outlet a computer and internet may not be possible It's tough to kick the habit because we feel that our current habit is OK to do Located in Covent Garden, central louis vuitton outlet online London, the studio building used to be a pineapple warehouse, hence the unusual company nameThere is much to remember when installing your water heater clean This debt reduction approach does michael kors handbags require that all payments to your creditors stop, so contracting with a reputable Christian debt settlement company is advised That sets the stage for an exciting round of qualifying that louis vuitton purses is scheduled for Friday afternoon A lot more winters, these boots can maintain your feet warm

How delicateCoach Outlet Onlineare the tender shoots unfolded layerCoach Factory Outletby layer. Of what a whiteness isCoach Outletthe last baby one of all, of whatCoach Outlet Onlinea sweetness his flavor. It is Coach Outlet Storewell that this should be the lastCoach Outlet Store Onlinerite of the meal -- finis coronatCoach Outletopus -- so that we may go straightCoach Factory Onlineon to the business of the pipe.Coach Factory OutletCelery demands a pipe rather thanCoach Outleta cigar, and it can be eaten betterCoach Factory Outletin an inn or a London tavern thanCoach Outletin the home. Yes, and it should beCoach Outlet Storeeaten alone, for it is the only Coach Outlet Store Onlinedistinctly, "Here we go gatheringCoach Outlet Onlinenuts in May"? Season of mistsTrue Religion Jeansand mellow celery, then letTrue Religion Outletit be. A pat of butter underneathTrue Religionthe bough, a wedge of cheese, aMichael Kors Outlet Onlineoaf of bread and -- Thou.foodMichael Handbags Outletl which one really wants to hearMichael Kors Outlet Store Online oneself eat. Besides, in companyMichael Kors Outlet Storesone may have to consider the wantsChanel Outlet Online of others. Celery is not a thingLouis Vuitton Outletto share with any man. Alone inLouis Vuitton Handbagsyour country

How delicateCoach Outlet Onlineare the tender shoots unfolded layerCoach Factory Outletby layer. Of what a whiteness isCoach Outletthe last baby one of all, of whatCoach Outlet Onlinea sweetness his flavor. It is Coach Outlet Storewell that this should be the lastCoach Outlet Store Onlinerite of the meal -- finis coronatCoach Outletopus -- so that we may go straightCoach Factory Onlineon to the business of the pipe.Coach Factory OutletCelery demands a pipe rather thanCoach Outleta cigar, and it can be eaten betterCoach Factory Outletin an inn or a London tavern thanCoach Outletin the home. Yes, and it should beCoach Outlet Storeeaten alone, for it is the only Coach Outlet Store Onlinedistinctly, "Here we go gatheringCoach Outlet Onlinenuts in May"? Season of mistsTrue Religion Jeansand mellow celery, then letTrue Religion Outletit be. A pat of butter underneathTrue Religionthe bough, a wedge of cheese, aMichael Kors Outlet Onlineoaf of bread and -- Thou.foodMichael Handbags Outletl which one really wants to hearMichael Kors Outlet Store Online oneself eat. Besides, in companyMichael Kors Outlet Storesone may have to consider the wantsChanel Outlet Online of others. Celery is not a thingLouis Vuitton Outletto share with any man. Alone inLouis Vuitton Handbagsyour country