一个非常动态的解决方案(也有点肮脏,但我很快就需要了它),因此您无需对引用字段的名称进行硬编码,它会自动随您将来添加的新引用字段一起处理:
在您的自定义模块中:
/**
* Implement hook_field_create_instance().
*/
function MY_CUSTOM_MODULE_field_create_instance() {
_MY_CUSTOM_MODULE_set_variable_node_back_references();
}
/**
* Implement hook_field_delete_field().
*/
function MY_CUSTOM_MODULE_field_delete_field() {
_MY_CUSTOM_MODULE_set_variable_node_back_references();
}
/**
* Set Variable node_back_references.
*/
function _MY_CUSTOM_MODULE_set_variable_node_back_references() {
$field_list = db_select('field_config', 'fc')
->fields('fc', array('field_name', 'data'))
->condition('fc.data', '%"foreign keys";a:1:{s:4:"node"%', 'like')
->condition('fc.deleted', 0);
$field_list->innerJoin('field_config_instance', 'fci', 'fci.field_name = fc.field_name');
$field_list->rightJoin('node_type', 'n', 'n.type = fci.bundle');
$fields = $field_list->execute()->fetchAll();
$fields_array = array();
foreach ($fields as $field) {
$unserialized = unserialize($field->data);
if (isset($unserialized['settings']['handler_settings']['target_bundles'])) {
foreach ($unserialized['settings']['handler_settings']['target_bundles'] as $bundle) {
$fields_array[$bundle][] = $field->field_name;
}
}
}
variable_set('node_back_references', $fields_array);
}
function _MY_CUSTOM_MODULE_get_referencing_nodes($node) {
$nids = array();
$fields = variable_get('node_back_references', array());
if (isset($fields[$node->type])) {
foreach ($fields[$node->type] as $field) {
$query = new \EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->propertyCondition('status', 1);
$query->fieldCondition($field, 'target_id', $node->nid);
$result = $query->execute();
$nids = isset($result['node']) ? array_merge(array_keys($result['node']), $nids) : $nids;
}
$nodes = (!empty($nids)) ? node_load_multiple($nids) : array();
return $nodes;
}
return $nids;
}
在给定子节点的情况下需要获取父节点的位置:
$nodes = _MY_CUSTOM_MODULE_get_referencing_nodes($node);
EntityFieldQuery
只能引用实体的一组,它不能创造遗憾的是其他实体的关系。它一次只能返回一种类型的实体,因此即使您可以创建这些关系,结果也将不可靠。