Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
$limit can be passed to EntityReference_SelectionHandler_Views::getReferencableEntities() as argument:
/**
* Implements EntityReferenceHandler::getReferencableEntities().
*/
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$display_name = $this->field['settings']['handler_settings']['view']['display_name'];
$args = $this->field['settings']['handler_settings']['view']['args'];
$result = array();
if ($this->initializeView($match, $match_operator, $limit)) {
// Get the results.
$result = $this->view->execute_display($display_name, $args);
}
$return = array();
if ($result) {
$target_type = $this->field['settings']['target_type'];
$entities = entity_load($target_type, array_keys($result));
foreach($entities as $entity) {
list($id,, $bundle) = entity_extract_ids($target_type, $entity);
$return[$bundle][$id] = $result[$id];
}
}
return $return;
}
protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
$view_name = $this->field['settings']['handler_settings']['view']['view_name'];
$display_name = $this->field['settings']['handler_settings']['view']['display_name'];
$args = $this->field['settings']['handler_settings']['view']['args'];
$entity_type = $this->field['settings']['target_type'];
// Check that the view is valid and the display still exists.
$this->view = views_get_view($view_name);
if (!$this->view || !isset($this->view->display[$display_name]) || !$this->view->access($display_name)) {
watchdog('entityreference', 'The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label']), WATCHDOG_WARNING);
return FALSE;
}
$this->view->set_display($display_name);
// Make sure the query is not cached.
$this->view->is_cacheable = FALSE;
// Pass options to the display handler to make them available later.
$entityreference_options = array(
'match' => $match,
'match_operator' => $match_operator,
'limit' => $limit,
'ids' => $ids,
);
$this->view->display_handler->set_option('entityreference_options', $entityreference_options);
return TRUE;
}
But in fact $limit always is 0:
/**
* Implements hook_options_list().
*/
function entityreference_options_list($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
if (!$options = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities()) {
return array();
}
// Rebuild the array, by changing the bundle key into the bundle label.
$target_type = $field['settings']['target_type'];
$entity_info = entity_get_info($target_type);
$return = array();
foreach ($options as $bundle => $entity_ids) {
$bundle_label = check_plain($entity_info['bundles'][$bundle]['label']);
$return[$bundle_label] = $entity_ids;
}
return count($return) == 1 ? reset($return) : $return;
}
Currently you can set any limit in view configuration, but all results will be used anyway. More logical to use $limit from view display, but not some hardcoded value.
Comment | File | Size | Author |
---|---|---|---|
#2 | entityreference_hardcoded_limit_in_view-2839526-2.patch | 908 bytes | afi13 |
|
Comments
Comment #2
afi13 CreditAttribution: afi13 commented