/**
  * Overrides OgSelectionHandler::buildEntityFieldQuery().
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $group_type = $this->field['settings']['target_type'];
     // See if the Entity allows for non-member postings
     $user_access = FALSE;
     $event = NULL;
     if ($this->entity && isset($this->entity->og_group_ref[LANGUAGE_NONE][0]['target_id'])) {
         $event = $this->entity->og_group_ref[LANGUAGE_NONE][0]['target_id'];
     } elseif (module_exists('og_context') && isset($_SESSION)) {
         $event = isset($_SESSION['og_context']) ? $_SESSION['og_context'] : og_context('node');
         $event = isset($event['gid']) ? $event['gid'] : NULL;
     }
     if ($event) {
         $user_access = og_user_access('node', $event, "create " . $this->instance['bundle'] . " content") || og_user_access('node', $event, "update own " . $this->instance['bundle'] . " content") || og_user_access('node', $event, "edit any " . $this->instance['bundle'] . " content");
     }
     if (empty($this->instance['field_mode']) || $group_type != 'node' || user_is_anonymous() || !$user_access) {
         return parent::buildEntityFieldQuery($match, $match_operator);
     }
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1);
     // Add this property to make sure we will have the {node} table later on in
     // OgCommonsSelectionHandler::entityFieldQueryAlter().
     $query->propertyCondition('nid', 0, '>');
     $query->addMetaData('entityreference_selection_handler', $this);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     return $query;
 }
 /**
  * Implements EntityReferenceHandler::validateReferencableEntities().
  */
 public function validateReferencableEntities(array $ids)
 {
     $referencable = parent::validateReferencableEntities($ids);
     // Allow users to save the queue even if they don't have access to an
     // existing entity in the queue. See https://www.drupal.org/node/2383903
     $existing = $this->getCurrentlyReferencedEntityIds();
     return array_unique(array_merge($referencable, $existing));
 }
 /**
  * {@inheritdoc}
  */
 protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $query = parent::buildEntityFieldQuery($match, $match_operator);
     // Search only from the list of users allowed to be assigned to this issue.
     $assignees = $this->getAllowedAssignees();
     if (!empty($assignees)) {
         $query->propertyCondition('uid', $assignees, 'IN');
     }
     return $query;
 }
 /**
  * Build an EntityFieldQuery to get referencable entities.
  */
 protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     // @todo Enable filtering by first OR last name. This will require adding
     // metadata and/or tags to the query, altering it using a
     // hook_query_TAG_alter(), and using a db_or() to generate the OR condition.
     // See http://drupal.stackexchange.com/questions/14499/using-or-with-entityfieldquery
     // and https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_or/7
     // and http://www.phase2technology.com/blog/or-queries-with-entityfieldquery/.
     // For now, the filter is just on first name.
     $query = parent::buildEntityFieldQuery($match, $match_operator);
     $query->propertyCondition('first_name', $match, $match_operator);
     return $query;
 }
 /**
  * Overrides TicketStateSelectionHandler::buildEntityFieldQuery().
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $group_type = $this->field['settings']['target_type'];
     if (empty($this->instance['field_mode']) || $group_type != 'node' || user_is_anonymous()) {
         return parent::buildEntityFieldQuery($match, $match_operator);
     }
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // Show only the entities that are active groups.
     $query->propertyCondition('active', 1);
     $query->addMetaData('entityreference_selection_handler', $this);
     $query->addTag('entity_field_access');
     return $query;
 }
 /**
  * Generate a settings form for this handler.
  */
 public static function settingsForm($field, $instance)
 {
     $project_behaviors = project_get_behavior_info();
     $project_behavior_options = array();
     foreach ($project_behaviors as $module => $behavior_info) {
         $machine_name = $behavior_info['machine name'];
         $project_behavior_options[$machine_name] = $behavior_info['label'];
     }
     $form['behavior'] = array('#type' => 'select', '#title' => t('Project behavior'), '#options' => $project_behavior_options, '#default_value' => !empty($field['settings']['handler_settings']['behavior']) ? $field['settings']['handler_settings']['behavior'] : 'none');
     $form += parent::settingsForm($field, $instance);
     // We're overriding the bundles.
     unset($form['target_bundles']);
     return $form;
 }
 /**
  * Build an EntityFieldQuery to get referencable entities.
  */
 protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $query = parent::buildEntityFieldQuery($match, $match_operator);
     // Filtering by first and last name. EFQs do not support OR conditions, so
     // a tag is added, which allows the resulting query to be altered. In that
     // query_alter, all of the conditions are added, so they are not set here.
     // See http://drupal.stackexchange.com/questions/14499/using-or-with-entityfieldquery
     // and https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_or/7
     // and http://www.phase2technology.com/blog/or-queries-with-entityfieldquery/.
     $query->addTag('redhen_contact_generic_selection');
     // Add a 'search_string' metadata so the query_alter can easily find the
     // search text.
     $query->addMetaData('search_string', $match);
     return $query;
 }
 /**
  * Overrides OgSelectionHandler::buildEntityFieldQuery().
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $group_type = $this->field['settings']['target_type'];
     if (empty($this->instance['field_mode']) || $group_type != 'node' || user_is_anonymous()) {
         return parent::buildEntityFieldQuery($match, $match_operator);
     }
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1);
     $query->fieldCondition('field_og_subscribe_settings', 'value', 'anyone');
     // Add this property to make sure we will have the {node} table later on in
     // OgCommonsSelectionHandler::entityFieldQueryAlter().
     $query->propertyCondition('nid', 0, '>');
     $query->addMetaData('entityreference_selection_handler', $this);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     return $query;
 }
 /**
  * {@inheritdoc}
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     parent::buildEntityFieldQuery();
     global $user;
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     // FIXME: drupal.org/node/1413108
     unset($query->tags['entityreference']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     $group_type = $this->field['settings']['target_type'];
     $entity_info = entity_get_info($group_type);
     if (!field_info_field(OG_GROUP_FIELD)) {
         // There are no groups, so falsify query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         return $query;
     }
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1, '=');
     $account = user_load($user->uid);
     if (user_access('administer site configuration', $account)) {
         // Site administrator can choose also groups he is not member of.
         $query->fieldCondition('c4m_og_status', 'value', array('deleted'), 'NOT IN');
         return $query;
     }
     $user_groups = og_get_groups_by_user(NULL, $group_type);
     $user_groups = $user_groups ? $user_groups : array();
     if ($user_groups) {
         $query->propertyCondition($entity_info['entity keys']['id'], $user_groups, 'IN');
     } else {
         // User doesn't have permission to select any group so falsify this
         // query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
     }
     $unallowed_values = array('requested', 'archived', 'rejected', 'deleted');
     $query->fieldCondition('c4m_og_status', 'value', $unallowed_values, 'NOT IN');
     return $query;
 }
 /**
  * Overrides OgSelectionHandler::buildEntityFieldQuery().
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $group_type = $this->field['settings']['target_type'];
     // See if the Entity allows for non-member postings
     $event_entity_types = cod_events_get_group_content_entity_types();
     if (empty($this->instance['field_mode']) || $group_type != 'node' || user_is_anonymous() || isset($this->instance['bundle']) && !(isset($event_entity_types['node'][$this->instance['bundle']]['non_member']) && user_access("create " . $this->instance['bundle'] . " content"))) {
         return parent::buildEntityFieldQuery($match, $match_operator);
     }
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1);
     // Add this property to make sure we will have the {node} table later on in
     // OgCommonsSelectionHandler::entityFieldQueryAlter().
     $query->propertyCondition('nid', 0, '>');
     $query->addMetaData('entityreference_selection_handler', $this);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     return $query;
 }
 /**
  * Overrides OgSelectionHandler::buildEntityFieldQuery().
  *
  * This is an example of "subgroups" (but without getting into the logic of
  * sub-grouping).
  * The idea here is to show we can set "My groups" and "Other groups" to
  * reference different groups by different
  * logic. In this example, all group nodes below node ID 5, will appear under
  * "My groups", and the rest will appear under "Other groups",
  * for administrators.
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $group_type = $this->field['settings']['target_type'];
     if (empty($this->instance['field_mode']) || $group_type != 'node') {
         return parent::buildEntityFieldQuery($match, $match_operator);
     }
     $field_mode = $this->instance['field_mode'];
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1, '=');
     if ($field_mode == 'default') {
         $query->propertyCondition('nid', '5', '<=');
     } else {
         $query->propertyCondition('nid', '5', '>');
     }
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     // FIXME: drupal.org/node/1413108
     unset($query->tags['entityreference']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     return $query;
 }
 /**
  * Implements EntityReferenceHandler::getReferencableEntities().
  */
 public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0)
 {
     if ($match || $limit) {
         return parent::getReferencableEntities($match, $match_operator, $limit);
     }
     $options = array();
     $entity_type = $this->field['settings']['target_type'];
     // We imitate core by calling taxonomy_get_tree().
     $entity_info = entity_get_info('taxonomy_term');
     $bundles = !empty($this->field['settings']['handler_settings']['target_bundles']) ? $this->field['settings']['handler_settings']['target_bundles'] : array_keys($entity_info['bundles']);
     foreach ($bundles as $bundle) {
         if ($vocabulary = taxonomy_vocabulary_machine_name_load($bundle)) {
             if ($terms = taxonomy_get_tree($vocabulary->vid, 0, NULL, TRUE)) {
                 foreach ($terms as $term) {
                     $options[$vocabulary->machine_name][$term->tid] = str_repeat('-', $term->depth) . check_plain($term->name);
                 }
             }
         }
     }
     return $options;
 }
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     $query = parent::buildEntityFieldQuery($match, $match_operator);
     // The user entity doesn't have a label column.
     if (isset($match)) {
         $query->propertyCondition('name', $match, $match_operator);
     }
     // Adding the 'user_access' tag is sadly insufficient for users: core
     // requires us to also know about the concept of 'blocked' and
     // 'active'.
     if (!user_access('administer users')) {
         $query->propertyCondition('status', 1);
     }
     return $query;
 }
 public function entityFieldQueryAlter(SelectQueryInterface $query)
 {
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance);
     // FIXME: Allow altering, after fixing http://drupal.org/node/1413108
     // $handler->entityFieldQueryAlter($query);
 }
 /**
  * {@inheritdoc}
  */
 protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     // We use the same settings as the parent class, therefore it's fine for
     // this to call the parent, as it will produce correct results. It's also
     // fine for methods we don't override such as countReferencableEntities() to
     // call this.
     return parent::buildEntityFieldQuery($match, $match_operator);
 }
 /**
  * Overrides OgSelectionHandler::buildEntityFieldQuery().
  *
  * Includes inherited groups.
  */
 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS')
 {
     global $user;
     $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
     $query = $handler->buildEntityFieldQuery($match, $match_operator);
     // FIXME: http://drupal.org/node/1325628
     unset($query->tags['node_access']);
     // FIXME: drupal.org/node/1413108
     unset($query->tags['entityreference']);
     $query->addTag('entity_field_access');
     $query->addTag('og');
     $group_type = $this->field['settings']['target_type'];
     $entity_info = entity_get_info($group_type);
     if (!field_info_field(OG_GROUP_FIELD)) {
         // There are no groups, so falsify query.
         $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         return $query;
     }
     // Show only the entities that are active groups.
     $query->fieldCondition(OG_GROUP_FIELD, 'value', 1, '=');
     if (empty($this->instance['field_mode'])) {
         return $query;
     }
     $field_mode = $this->instance['field_mode'];
     $user_groups = og_get_groups_by_user(NULL, $group_type);
     $user_groups = $user_groups ? $user_groups : array();
     $user_groups = array_merge($user_groups, $this->getGidsForCreate());
     // Show the user only the groups they belong to.
     if ($field_mode == 'default') {
         if ($user_groups && !empty($this->instance) && $this->instance['entity_type'] == 'node') {
             // Determine which groups should be selectable.
             $node = $this->entity;
             $node_type = $this->instance['bundle'];
             $ids = array();
             foreach ($user_groups as $gid) {
                 // Check if user has "create" permissions on those groups.
                 // If the user doesn't have create permission, check if perhaps the
                 // content already exists and the user has edit permission.
                 if (og_user_access($group_type, $gid, "create {$node_type} content", $user)) {
                     $ids[] = $gid;
                     // Include inherited groups as well
                     $inherited_groups = og_subgroup_user_groups_load($user);
                     if (isset($inherited_groups[$group_type])) {
                         foreach (array_keys($inherited_groups[$group_type]) as $inherited_gid) {
                             $ids[] = $inherited_gid;
                         }
                     }
                 } elseif (!empty($node->nid) && (og_user_access($group_type, $gid, "update any {$node_type} content") || $user->uid == $node->uid && og_user_access($group_type, $gid, "update own {$node_type} content"))) {
                     $node_groups = isset($node_groups) ? $node_groups : og_get_entity_groups('node', $node->nid);
                     if (in_array($gid, $node_groups['node'])) {
                         $ids[] = $gid;
                         // Include inherited groups as well
                         $inherited_groups = og_subgroup_user_groups_load($user);
                         if (isset($inherited_groups[$group_type])) {
                             foreach (array_keys($inherited_groups[$group_type]) as $inherited_gid) {
                                 $ids[] = $inherited_gid;
                             }
                         }
                     }
                 }
             }
         } else {
             $ids = $user_groups;
         }
         if ($ids) {
             $query->propertyCondition($entity_info['entity keys']['id'], $ids, 'IN');
         } else {
             // User doesn't have permission to select any group so falsify this
             // query.
             $query->propertyCondition($entity_info['entity keys']['id'], -1, '=');
         }
     } elseif ($field_mode == 'admin' && $user_groups) {
         // Show only groups the user doesn't belong to.
         if (!empty($this->instance) && $this->instance['entity_type'] == 'node') {
             // Don't include the groups, the user doesn't have create
             // permission.
             $node_type = $this->instance['bundle'];
             foreach ($user_groups as $delta => $gid) {
                 if (!og_user_access($group_type, $gid, "create {$node_type} content")) {
                     unset($user_groups[$delta]);
                 }
             }
         }
         if ($user_groups) {
             $query->propertyCondition($entity_info['entity keys']['id'], $user_groups, 'NOT IN');
         }
     }
     return $query;
 }