From 3d057a71c60c6d0a1548a1af81e43b66dc039b54 Mon Sep 17 00:00:00 2001 From: Jonathan Hunt Date: Thu, 16 Mar 2023 21:49:13 +1300 Subject: [PATCH] Update Entity Reference to 7.x-1.7 --- .../entityreference/entityreference.feeds.inc | 34 +++-------- .../entityreference/entityreference.info | 6 +- .../entityreference_behavior_example.info | 6 +- ...ference_SelectionHandler_Generic.class.php | 57 +++++++++++++++++++ ...Reference_SelectionHandler_Views.class.php | 10 +++- .../entityreference_feeds_test.info | 6 +- 6 files changed, 81 insertions(+), 38 deletions(-) diff --git a/docroot/sites/all/modules/contrib/entityreference/entityreference.feeds.inc b/docroot/sites/all/modules/contrib/entityreference/entityreference.feeds.inc index 3da1479c..8b9a7a7f 100644 --- a/docroot/sites/all/modules/contrib/entityreference/entityreference.feeds.inc +++ b/docroot/sites/all/modules/contrib/entityreference/entityreference.feeds.inc @@ -56,36 +56,14 @@ function entityreference_feeds_processor_targets_alter(&$targets, $entity_type, } /** - * Entity reference callback for mapping. - * - * When the callback is invoked, $target contains the name of the field the - * user has decided to map to and $value contains the value of the feed item - * element the user has picked as a source. - * - * @param $source - * A FeedsSource object. - * @param $entity - * The entity to map to. - * @param $target - * The target key on $entity to map to. - * @param $value - * The value to map. MUST be an array. + * Entity reference callback for hook_feeds_processor_targets(). */ -function entityreference_feeds_set_target($source, $entity, $target, $value) { - +function entityreference_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) { // Don't do anything if we weren't given any data. - if (empty($value)) { + if (empty($values)) { return; } - // Assume that the passed in value could really be any number of values. - if (is_array($value)) { - $values = $value; - } - else { - $values = array($value); - } - // Determine the field we are matching against. if (strpos($target, ':') === FALSE) { $match_key = 'guid'; @@ -116,13 +94,15 @@ function entityreference_feeds_set_target($source, $entity, $target, $value) { // Fetch the entity ID resulting from the mapping table look-up. $entity_id = db_select('feeds_item', 'fi') ->fields('fi', array('entity_id')) - ->condition($match_key, $value,'=') + ->condition($match_key, $value, '=') ->execute() ->fetchField(); break; + case 'etid': $entity_id = $value; break; + case 'label': $options = $handler->getReferencableEntities($value, '='); if ($options) { @@ -147,7 +127,7 @@ function entityreference_feeds_set_target($source, $entity, $target, $value) { if ($entity_id) { // Assign the target ID. - $field[$language][$iterator]['target_id'] = $entity_id; + $field[$language][$iterator]['target_id'] = $entity_id; } else /* there is no $entity_id, no mapping */ { diff --git a/docroot/sites/all/modules/contrib/entityreference/entityreference.info b/docroot/sites/all/modules/contrib/entityreference/entityreference.info index 73c2c6d8..c05ff213 100644 --- a/docroot/sites/all/modules/contrib/entityreference/entityreference.info +++ b/docroot/sites/all/modules/contrib/entityreference/entityreference.info @@ -31,8 +31,8 @@ files[] = tests/entityreference.feeds.test files[] = tests/entityreference.field.test files[] = tests/entityreference.entity_translation.test -; Information added by Drupal.org packaging script on 2023-02-10 -version = "7.x-1.6" +; Information added by Drupal.org packaging script on 2023-03-14 +version = "7.x-1.7" core = "7.x" project = "entityreference" -datestamp = "1676069720" +datestamp = "1678809341" diff --git a/docroot/sites/all/modules/contrib/entityreference/examples/entityreference_behavior_example/entityreference_behavior_example.info b/docroot/sites/all/modules/contrib/entityreference/examples/entityreference_behavior_example/entityreference_behavior_example.info index 8d41affe..de552bd4 100644 --- a/docroot/sites/all/modules/contrib/entityreference/examples/entityreference_behavior_example/entityreference_behavior_example.info +++ b/docroot/sites/all/modules/contrib/entityreference/examples/entityreference_behavior_example/entityreference_behavior_example.info @@ -4,8 +4,8 @@ core = 7.x package = Fields dependencies[] = entityreference -; Information added by Drupal.org packaging script on 2023-02-10 -version = "7.x-1.6" +; Information added by Drupal.org packaging script on 2023-03-14 +version = "7.x-1.7" core = "7.x" project = "entityreference" -datestamp = "1676069720" +datestamp = "1678809341" diff --git a/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php index 15e15d5c..42c60984 100644 --- a/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php +++ b/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Generic.class.php @@ -381,6 +381,35 @@ public function entityFieldQueryAlter(SelectQueryInterface $query) { $query->condition("$base_table.status", NODE_PUBLISHED); } } + + /** + * Implements EntityReferenceHandler::getReferencableEntities(). + */ + public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { + $options = array(); + $entity_type = $this->field['settings']['target_type']; + + $query = $this->buildEntityFieldQuery($match, $match_operator); + $query->entityCondition('entity_type', 'node'); + if ($limit > 0) { + $query->range(0, $limit); + } + + $results = $query->execute(); + + // Use a database lookup instead of the more expensive entity_load() used by the parent function. + if (!empty($results[$entity_type])) { + $nids = array_keys($results[$entity_type]); + $results = db_query('SELECT nid, type, title FROM {node} WHERE nid IN (:nids)', array(':nids' => $nids))->fetchAllAssoc('nid'); + foreach ($nids as $nid) { + if (!empty($results[$nid]->type)) { + $options[$results[$nid]->type][$nid] = check_plain($results[$nid]->title); + } + } + } + + return $options; + } } /** @@ -439,6 +468,34 @@ public function entityFieldQueryAlter(SelectQueryInterface $query) { } } } + + /** + * Implements EntityReferenceHandler::getReferencableEntities(). + */ + public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { + $options = array(); + $entity_type = $this->field['settings']['target_type']; + + $query = $this->buildEntityFieldQuery($match, $match_operator); + $query->entityCondition('entity_type', 'user'); + if ($limit > 0) { + $query->range(0, $limit); + } + $results = $query->execute(); + + // Use a database lookup instead of the more expensive entity_load() used by the parent function. + if (!empty($results[$entity_type])) { + $uids = array_keys($results[$entity_type]); + $results = db_query('SELECT uid, name, status FROM {users} WHERE uid IN (:uids)', array(':uids' => $uids)); + while ($entity = $results->fetchObject()) { + // Our partial entity is sufficient for the entity user access callback + // and getLabel. + $options['user'][$entity->uid] = check_plain($this->getLabel($entity)); + } + } + + return $options; + } } /** diff --git a/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php index 3909bacb..95e7302c 100644 --- a/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php +++ b/docroot/sites/all/modules/contrib/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php @@ -140,8 +140,14 @@ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAI $return = array(); if ($result) { $target_type = $this->field['settings']['target_type']; - $entities = entity_load($target_type, array_keys($result)); - foreach($entities as $entity) { + + // Do not load the entire entity for performance reasons. + $entity_info = entity_get_info($target_type); + $query = db_select($entity_info['base table'], 'e'); + $query->fields('e'); + $query->condition('e.' . $entity_info['entity keys']['id'], array_keys($result), 'IN'); + + foreach($query->execute() as $entity) { list($id,, $bundle) = entity_extract_ids($target_type, $entity); $return[$bundle][$id] = $result[$id]; } diff --git a/docroot/sites/all/modules/contrib/entityreference/tests/modules/entityreference_feeds_test/entityreference_feeds_test.info b/docroot/sites/all/modules/contrib/entityreference/tests/modules/entityreference_feeds_test/entityreference_feeds_test.info index 39677b54..2f8ea580 100644 --- a/docroot/sites/all/modules/contrib/entityreference/tests/modules/entityreference_feeds_test/entityreference_feeds_test.info +++ b/docroot/sites/all/modules/contrib/entityreference/tests/modules/entityreference_feeds_test/entityreference_feeds_test.info @@ -8,8 +8,8 @@ dependencies[] = feeds dependencies[] = feeds_ui dependencies[] = entityreference -; Information added by Drupal.org packaging script on 2023-02-10 -version = "7.x-1.6" +; Information added by Drupal.org packaging script on 2023-03-14 +version = "7.x-1.7" core = "7.x" project = "entityreference" -datestamp = "1676069720" +datestamp = "1678809341"