You are here

function _entity_views_field_identifier in Entity API 7

Helper function for creating valid Views field identifiers out of data selectors.

Uses $table to test whether the identifier is already used, and also recognizes if a definition for the same field is already present and returns that definition's identifier.

Return value

string A valid Views field identifier that is not yet used as a key in $table.

1 call to _entity_views_field_identifier()
entity_views_field_definition in views/entity.views.inc
Helper function for adding a Views field definition to data selection based Views tables.

File

views/entity.views.inc, line 291
Provide views data for modules making use of the entity CRUD API.

Code

function _entity_views_field_identifier($field, array $table) {
  $key = $base = preg_replace('/[^a-zA-Z0-9]+/S', '_', $field);
  $i = 0;

  // The condition checks whether this sanitized field identifier is already
  // used for another field in this table (and whether the identifier is
  // "table", which can never be used).
  // If $table[$key] is set, the identifier is already used, but this might be
  // already for the same field. To test that, we need the original field name,
  // which is either $table[$key]['real field'], if set, or $key. If this
  // original field name is equal to $field, we can use that key. Otherwise, we
  // append numeric suffixes until we reach an unused key.
  while ($key == 'table' || isset($table[$key]) && (isset($table[$key]['real field']) ? $table[$key]['real field'] : $key) != $field) {
    $key = $base . '_' . ++$i;
  }
  return $key;
}