5 * Definition of EntityReference_SelectionHandler_EntityQueue.
9 * Defines a Entityreference selection handler for Entityqueue.
11 class EntityReference_SelectionHandler_EntityQueue extends EntityReference_SelectionHandler_Generic {
14 * Overrides EntityReference_SelectionHandler_Generic::getInstance().
16 public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
17 return new EntityReference_SelectionHandler_EntityQueue($field, $instance, $entity_type, $entity);
21 * Constructs the EntityQueue selection handler.
23 protected function __construct($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
24 parent::__construct($field, $instance, $entity_type, $entity);
27 if (!empty($entity->queue)) {
28 $queue_name = $entity->queue;
30 elseif (!empty($instance['bundle'])) {
31 $queue_name = $instance['bundle'];
33 if (!empty($queue_name)) {
34 $this->queue = entityqueue_queue_load($queue_name);
37 // Override the entityreference settings with our own.
38 $this->field['settings']['handler_settings']['target_bundles'] = NULL;
42 * Overrides EntityReference_SelectionHandler_Generic::buildEntityFieldQuery().
44 public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
45 $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
46 $query = $handler->buildEntityFieldQuery($match, $match_operator);
48 if (!empty($this->queue->settings['target_bundles'])) {
49 $query->entityCondition('bundle', $this->queue->settings['target_bundles'], 'IN');
56 * Implements EntityReferenceHandler::validateReferencableEntities().
58 public function validateReferencableEntities(array $ids) {
59 $referencable = parent::validateReferencableEntities($ids);
60 // Allow users to save the queue even if they don't have access to an
61 // existing entity in the queue. See https://www.drupal.org/node/2383903
62 $existing = $this->getCurrentlyReferencedEntityIds();
64 return array_unique(array_merge($referencable, $existing));
68 * Gets ids of existing entities in the queue.
71 * Entity ids that are currently referenced by the entity.
73 public function getCurrentlyReferencedEntityIds() {
75 if (isset($this->entity) && isset($this->field)) {
76 $entity_type = $this->entity_type;
77 $field_name = $this->field['field_name'];
78 $wrapper = entity_metadata_wrapper($entity_type, $this->entity);
79 $ret = $wrapper->{$field_name}->raw();