Drupal OG Mass Add - check for realname instead of name? EFQ using subquery?

26 Views Asked by At

Would it be possible for og_massadd to look up based on realname instead of username?

  // If not, try to check for usernames
  if (!isset($account) && drupal_strlen($mail)) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'user')
          ->propertyCondition('name', check_plain($mail));
    $result = $query->execute();

    if (!empty($result)) {
      $uids = array_keys($result['user']);
      $account = user_load_multiple($uids);
      $account = array_shift($account);
    }
  }

Already tried

->propertyCondition('realname', check_plain($mail));

I can see it's using EntityFieldQuery with propertyCondition but I don't understand if realname relates to entity properties or if there is any other way to check?

Any advise/help much appreciated.

UPDATE

Just found a way with drush to see available 'user' fields where realname isn't part of them :(

Any other way to cross check against realname?

UPDATE2

Found out that EFQ inner joins aren't supported but read about a workaround using subquery but couldn't make it work and are now stuck, any thoughts?

$query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'user')
      $rnames_subquery = db_select('realname', 'rn');
      $rnames_subquery->fields('rn', array('uid'));
      $query->propertyCondition('uid', $rnames_subquery, 'IN');
  $result = $query->execute();
1

There are 1 best solutions below

0
Markus On

Managed to replace the query with a joined lookup

$query = db_select('realname','rn');
$query->join('users','usr','usr.uid=rn.uid');
$query->fields('rn', array('uid'));
$query->condition('rn.realname',$mail,'=');
$result =$query->execute()->fetchAll();

if (!empty($result)) {
$uids =array($result[0]->uid);
$account = user_load_multiple($uids);
$account = array_shift($account);
}