get entityreferers nodes, fields of target bundle

Public

get entityreferers nodes, fields of target bundle

Get raw version
php
  1. function _my_module_get_entityreferers($target_bundle) {
  2. $child_ids = $field_names = $settings = array();
  3.  
  4. // Get all entityreference field names
  5. $query = db_select('field_config', 'f');
  6. $query->fields('f', array('field_name'));
  7. $query->condition('f.type', 'entityreference');
  8. $query->distinct();
  9. $rows = $query->execute();
  10.  
  11. foreach ($rows as $row) {
  12. $field_name = $row->field_name;
  13. $field = field_info_field($field_name);
  14. if(!isset($field['settings']['handler_settings']['target_bundles'])){
  15. continue;
  16. }
  17. $target_bundles = $field['settings']['handler_settings']['target_bundles'];
  18. if(in_array($target_bundle, $target_bundles)){
  19. $field_names[] = $field_name;
  20. }
  21. }
  22.  
  23. // Loop all entityreference fields. Get all child and parent Ids
  24. foreach ($field_names as $field_name) {
  25. $table = 'field_data_' . $field_name;
  26. $target_id = $field_name . '_target_id';
  27.  
  28. $query = db_select($table, 'f');
  29. $query->fields('f', array('entity_id', $target_id, 'entity_type', 'bundle'));
  30. $query->distinct();
  31. $rows = $query->execute();
  32.  
  33. foreach ($rows as $row) {
  34. $settings[$row->entity_type][$row->bundle][$field_name] = $field_name;
  35. }
  36. }
  37.  
  38. return $settings;
  39. }