Introduction to EntityFieldJoinQuery

Tien Vo

Drupal developer at GO1 with 2 years experiences.

Outline

  • About EntityFieldQuery
  • Introduce EntityFieldJoinQuery

What is EntityFieldQuery

  • Easy way to query entities base on fields and properties
  • Object oriented
  • Easy to use
  • Alterable and more...

How to use?

$query = new EntityFieldQuery();

$query->entityCondition('entity_type',
      'node')
  ->entityCondition('bundle',
      'book')
  ->propertyCondition('status',
      'published')
  ->fieldCondition('field_price',
      'value', 100)
  ->fieldOrderBy('field_no_pages',
      'value', 'DESC')
  ->propertyOrderBy('uid');

$result = $query->execute();

Wishlist

  • Join tables.

Introducing EntityFieldJoinQuery

  • Include everything of EntityFieldQuery
  • Support join tables
  • Allow to extend via plugins
  • Fully unit tested

EntityFieldJoinQuery

$query = new EntityFieldJoinQuery();
$query
  ->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'book')
  // Uploaded by authenticated users.
  ->propertyCondition(
      'author.entity.{users_roles}.rid',
      DRUPAL_AUTHENTICATED_RID)
  ->fieldCondition(
      'field_author.entity.field_name',
      'value', 'J. K. Rowling')
  ->fieldOrderBy(
      'field_publisher.entity.field_name',
      'value', 'ASC')
  ->propertyOrderBy(
      'field_co_writer.entity.age',
      'value', 'DESC');

$result = $query->execute();

EntityFieldJoinQuery

$query = new EntityFieldJoinQuery();
$query
  ->entityCondition('entity_type', 'user')
  ->propertyCondition('{og_membership}.type',
      'silver')
  ->propertyCondition(
      '{og_membership}.group<club>.field_name',
      'Let Dance');

$result = $query->execute();

EntityFieldJoinQuery

$query = new EntityFieldJoinQuery();
$query
  ->entityCondition('entity_type', 'user')
  ->propertyCondition(
      '{og_membership}.group<course>.' .
      '{og_membership(required)}.' .
      'group<major>.field_name',
      'Information Technology');

$result = $query->execute();

Extend by plugin

/**
 * Implements hook_ctools_plugin_directory().
 */
function MODULE_ctools_plugin_directory
      ($module, $plugin) {
  if (($module == 'entity_field_join_query') &&
      ($plugin == 'efjq_join_part')) {
    return 'plugins/efjq_join_part';
  }
}

Extend by plugin


$plugin = array(
  'title' => t('My Plugin'),
  'handler' => array(
    'class' => 'efjq_join_part_my_plugin',
  ),
  // Plugin with lower weight will
  // override plugin with hight weight.
  'weight' => 2,
);

class efjq_join_part_my_plugin extends
    efjq_join_part_base {
  //...
}

What else about EntityFieldJoinQuery

  • Fully unit tested.
  • What about views?

Q&A

efjq

By Tiến Võ Xuân

efjq

EntityFieldJoinQuery

  • 1,109