Created
June 5, 2015 21:07
-
-
Save jonathanfranks/a38829b184eb54ebf83a to your computer and use it in GitHub Desktop.
Drupal Facet API Custom Hierarchy Callback for Entity Reference Fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_facetapi_facet_info_alter(). | |
*/ | |
function hook_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) { | |
foreach ($facet_info as $name => $facet) { | |
$name = 'field_parent'; | |
if ($name == $name) { | |
$facet_info[$name]['hierarchy callback'] = 'hook_facets_entity_reference_hierarchy'; | |
} | |
} | |
} | |
function hook_entity_reference_hierarchy(array $values) { | |
$query = new EfqHelper('node'); | |
$query->propertyCondition('type', 'node_type') | |
->fieldCondition('field_parent', 'target_id', $values, 'IN') | |
->addMetaData('account', user_load(1)); // Run the query as user 1. | |
$results = $query->result(); | |
$parents = array(); | |
foreach ($results as $nid => $node) { | |
$w = entity_metadata_wrapper('node', $node); | |
$parent_node = $w->field_parent->value(); | |
if ($parent_node) { | |
$parent_nid = $parent_node->nid; | |
$parents[$nid][] = $parent_nid; | |
} | |
} | |
return $parents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment