Get id's of all files referenced entities and their parents

Public

Gets id's of all files referenced entities and their parents. Works with entity_reference module

</> CopyGet raw version
php
  1. function _my_module_get_entityreferences_settings() {
  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_names[] = $field_name;
  14. }
  15.  
  16. // Loop all entityreference fields. Get all child and parent Ids
  17. foreach ($field_names as $field_name) {
  18. $table = 'field_data_' . $field_name;
  19. $target_id = $field_name . '_target_id';
  20.  
  21. $query = db_select($table, 'f');
  22. $query->fields('f', array('entity_id', $target_id, 'entity_type', 'bundle'));
  23. $query->distinct();
  24. $rows = $query->execute();
  25.  
  26. foreach ($rows as $row) {
  27. $settings[$row->entity_type][$row->bundle][$field_name] = $field_name;
  28. $child_ids[$row->{$target_id}] = array(
  29. 'id' => $row->{$target_id},
  30. 'parent' => $row->entity_id,
  31. );
  32. }
  33. }
  34.  
  35. return array(
  36. 'childs' => $child_ids,
  37. 'settings' => $settings,
  38. );
  39. }