Load Drupal Entity - more readable code (easy one-liner)

Submitted by admin on Mon, 05/08/2023 - 10:09

In my project (on Drupal 7) I'm using EntityFieldQuery alot to get some entities with given conditions, but working with resulting array of execute() function - is boring and makes code dirty IMHO. So I created helper class used only to load entities. Most of the time result is self describing one-liners instead of variables mess. I know it not covers all posibilities of EntityQuery, but 90% of my usage of it is loading Entities with some conditions. With the help of my class, loading one entity with conditions will be as simple as:

$country = (new EntityLoadQuery('country'))->propertyCondition('city_tid', 88)->loadSingle();

Here is the code of class used in Drupal 7:

<?php
/**
 * Easily load entities with power of EntityFieldQuery
 *
 * Usage example:
 * $country = (new EntityLoadQuery('country'))->propertyCondition('city_tid', 88)->loadSingle();
 *
 * @author Kirill Kuzmitskyy (toopro.org)
 */
class EntityLoadQuery extends EntityFieldQuery {

    /**
     * @param string $entity_type tell the type of the entity to load
     */
    public function __construct($entity_type) {
        $this->entityCondition('entity_type', $entity_type);
    }

        /**
     * @param $entity_type tell the type of the entity to load
     * @return EntityLoad
     */
    public static function query($entity_type) {
        return new EntityLoad($entity_type);
    }

    /**
     * @return array of loaded entities keyed by Names or IDs
     */
    public function load() {
        $eqResult = $this->execute();
        foreach ($eqResult as $entity_type=>$keyedArray)
            return (module_exists('entity'))
                ? entity_load_multiple_by_name($entity_type, array_keys($keyedArray))
                : entity_load($entity_type, array_keys($keyedArray));
        return array();
    }

    /**
     * @return object one loaded entity of given type
     */
    public function loadSingle() {
        $ents = $this->load();
        return reset($ents);
    }

    /**
     * @return array of Names or IDs of the entities matching query
     */
    public function loadIDs() {
        $eqResult = $this->execute();
        $eqResult = reset($eqResult);
        return ($eqResult) ? $eqResult : array();
    }

}
?>

As the result, instead of doing like this:

<?php
$result = (new EntityFieldQuery())
                    ->entityCondition('entity_type','country')
                   ->propertyCondition('city_tid', 55)
                   ->execute();

if (isset($result['country'])) {
  $news_items_ids = array_keys($result['country']);
  $entities = entity_load('country', $news_items_nids);
  $country = reset($entities);
}
?>

I have this code:

<?php
$country = EntityLoad::query('country')->propertyCondition('city_tid', 88)->loadSingle();
?>

I think it's more readable and less non-business logic in code. Hope someone will like it too :) And correct me if I'm doing something wrong with such approach.