Skip to content

Instantly share code, notes, and snippets.

@amitaibu
Created October 31, 2012 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitaibu/3989148 to your computer and use it in GitHub Desktop.
Save amitaibu/3989148 to your computer and use it in GitHub Desktop.
/**
* Implements hook_init().
*/
function foo_init() {
$field = field_info_field('bar');
$instance = field_info_instance('node', 'bar', 'baz');
// This will call SelectionBase
$handler = entity_reference_get_selection_handler($field, $instance);
$ids = $handler->getReferencableEntities();
}
class SelectionBase implements SelectionInterface {
/**
* Returns a list of referencable entities.
*/
public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
// ...
$query = $this->buildEntityFieldQuery($match, $match_operator);
}
/**
* Builds an EntityFieldQuery to get referencable entities.
*/
protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
//...
// Add a generic entity access tag to the query.
//$query->addMetaData('field', $this->field);
//$query->addMetaData('entity_reference_selection_handler', $this);
// ...
}
}
/**
* Implements hook_query_TAG_alter().
*/
function entity_reference_query_entity_reference_alter(AlterableInterface $query) {
$handler = $query->getMetadata('entity_reference_selection_handler');
// Alter the query to fix entity access per entity-type.
$handler->entityFieldQueryAlter($query);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment