You are here

public static function FieldHelper::getEntityReferencingFields in Helper 7

Find all field columns that have data that refer to entities.

Return value

array

3 calls to FieldHelper::getEntityReferencingFields()
FieldHelper::getEntityReferencingFieldColumns in lib/FieldHelper.php
FieldHelper::getEntityReferencingFieldsByType in lib/FieldHelper.php
FieldHelperTestCase::testGetReferencingFields in tests/FieldHelperTestCase.test
Tests FieldHelper::getEntityReferencingFields() and FieldHelper::getEntityReferencingFieldColumns().

File

lib/FieldHelper.php, line 106

Class

FieldHelper

Code

public static function getEntityReferencingFields() {
  $results = array();
  if ($cache = cache_get('field_info:helper_referencing_fields', 'cache_field')) {
    $results = $cache->data;
  }
  else {
    $entity_info = entity_get_info();
    $base_tables = array();
    foreach ($entity_info as $type => $type_info) {
      if (!empty($type_info['base table']) && !empty($type_info['entity keys']['id'])) {
        $base_tables[$type_info['base table']] = array(
          'type' => $type,
          'column' => $type_info['entity keys']['id'],
        );
      }
    }
    $fields = field_info_fields();
    foreach ($fields as $field) {

      // Cannot rely on entityreference fields having correct foreign key info.
      // @todo Remove when http://drupal.org/node/1969018 is fixed.
      if ($field['type'] == 'entityreference') {
        $results[$field['field_name']]['target_id'] = $field['settings']['target_type'];
        continue;
      }
      foreach ($field['foreign keys'] as $foreign_key) {
        if (isset($base_tables[$foreign_key['table']])) {
          $base_table = $base_tables[$foreign_key['table']];
          if ($column = array_search($base_table['column'], $foreign_key['columns'])) {
            $results[$field['field_name']][$column] = $base_table['type'];
          }
        }
      }
    }
    drupal_alter('helper_field_get_referencing_fields', $results, $fields);
    cache_set('field_info:helper_referencing_fields', $results, 'cache_field');
  }
  return $results;
}