I'm trying to build a new widget for the entity reference module. (nested checkboxes = tree according to entity references between nodes)

The only answer I'm looking for is : "How to get the list of all entity reference fields and their params? (child and parent node)

Thank you for your help!


Edit : working on D7

  • I can get the list of all entity reference fields for one child, but I don't know how to get the parent node type of each field.
    – Ludovic
    Commented Aug 22, 2012 at 15:17
  • I have the same problem. You can get parent entity in field table "field_data_$field_name" but i cant get all field names of all entityreference fields
    – milkovsky
    Commented Mar 5, 2013 at 10:04
  • see my solution in the answer below
    – milkovsky
    Commented Mar 5, 2013 at 10:56

1 Answer 1


Here is my custom function that gets id's of all files referenced entities and their parents. Also get's all field names, entity types and bundles of entityreference fields.

function _my_module_get_entityreferences_settings() {
  $child_ids = $field_names = $settings = array();

  // Get all entityreference field names
  $query = db_select('field_config', 'f');
  $query->fields('f', array('field_name'));
  $query->condition('f.type', 'entityreference');
  $rows = $query->execute();

  foreach ($rows as $row) {
    $field_name = $row->field_name;
    $field_names[] = $field_name;

  // Loop all entityreference fields. Get all child and parent Ids
  foreach ($field_names as $field_name) {
    $table = 'field_data_' . $field_name;
    $target_id = $field_name . '_target_id';

    $query = db_select($table, 'f');
    $query->fields('f', array('entity_id', $target_id, 'entity_type', 'bundle'));
    $rows = $query->execute();

    foreach ($rows as $row) {
      $settings[$row->entity_type][$row->bundle][$field_name] = $field_name;
      $child_ids[$row->{$target_id}] = array(
        'id' => $row->{$target_id},
        'parent' => $row->entity_id,

  return array(
    'childs' => $child_ids,
    'settings' => $settings,

Maybe you don't need all this stuff. When you get $field_names array you can use

$field = field_info_field($field_name);

to get settings of current field. You can find here referenced bundles, field type, handler_settings etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.