Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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!

Ludovic

Edit : working on D7

share|improve this question
    
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 Aug 22 '12 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 Mar 5 '13 at 10:04
    
see my solution in the answer below –  milkovsky Mar 5 '13 at 10:56
add comment

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');
  $query->distinct();
  $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'));
    $query->distinct();
    $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,
  );
}

Might 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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