protected function DataProviderEntity::getReferencedId in RESTful 7.2
Get referenced ID.
@todo: Add testing to this functionality.
Parameters
string $value: The provided value, it can be an ID or not.
ResourceFieldInterface $resource_field: The resource field that points to another entity.
Return value
string If the field uses an alternate ID property, then the ID gets translated to the original entity ID. If not, then the same provided ID is returned.
2 calls to DataProviderEntity::getReferencedId()
- DataProviderEntity::getReferencedIds in src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php - Get reference IDs for multiple values.
- DataProviderEntity::queryForListFilter in src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php - Filter the query for list.
File
- src/
Plugin/ resource/ DataProvider/ DataProviderEntity.php, line 1107 - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.
Class
- DataProviderEntity
- Class DataProviderEntity.
Namespace
Drupal\restful\Plugin\resource\DataProviderCode
protected function getReferencedId($value, ResourceFieldInterface $resource_field) {
$field_definition = $resource_field
->getDefinition();
if (empty($field_definition['referencedIdProperty'])) {
return $value;
}
// Get information about the the Drupal field to see what entity type we are
// dealing with.
$field_info = field_info_field($resource_field
->getProperty());
// We support:
// - Entity Reference.
// - Taxonomy Term.
// - File & Image field.
// - uid property.
// - vid property.
// If you need to support other types, you can create a custom data provider
// that overrides this method.
$target_entity_type = NULL;
$bundles = array();
if (!$field_info) {
if ($resource_field
->getProperty() == 'uid') {
// We make a special case for the user id.
$target_entity_type = 'user';
}
elseif ($resource_field
->getProperty() == 'vid') {
// We make a special case for the vocabulary id.
$target_entity_type = 'taxonomy_vocabulary';
}
}
elseif (!empty($field_info['type']) && $field_info['type'] == 'entityreference') {
$target_entity_type = $field_info['settings']['target_type'];
$bundles = empty($field_info['settings']['handler_settings']['target_bundles']) ? array() : $field_info['settings']['handler_settings']['target_bundles'];
}
elseif (!empty($field_info['type']) && $field_info['type'] == 'file') {
$target_entity_type = 'file';
}
elseif (!empty($field_info['type']) && $field_info['type'] == 'taxonomy_term_reference') {
$target_entity_type = 'taxonomy_term';
// Narrow down with the vocabulary information. Very useful if there are
// multiple terms with the same name in different vocabularies.
foreach ($field_info['settings']['allowed_values'] as $allowed_value) {
$bundles[] = $allowed_value['vocabulary'];
}
}
if (empty($target_entity_type) && $resource_field instanceof ResourceFieldResourceInterface && ($resource_info = $resource_field
->getResource())) {
$instance_id = sprintf('%s:%d.%d', $resource_info['name'], $resource_info['majorVersion'], $resource_info['minorVersion']);
try {
$handler = restful()
->getResourceManager()
->getPlugin($instance_id);
if ($handler instanceof ResourceEntity) {
$target_entity_type = $handler
->getEntityType();
$bundles = $handler
->getBundles();
}
} catch (PluginNotFoundException $e) {
// Do nothing.
}
}
if (empty($target_entity_type)) {
return $value;
}
// Now we have the entity type and bundles to look for the entity based on
// the contents of the field or the entity property.
$query = $this
->EFQObject();
$query
->entityCondition('entity_type', $target_entity_type);
if (!empty($bundles)) {
// Narrow down for bundles.
$query
->entityCondition('bundle', $bundles, 'IN');
}
// Check if the referencedIdProperty is a field or a property.
$id_property = $field_definition['referencedIdProperty'];
if (field_info_field($id_property)) {
$query
->fieldCondition($id_property, $resource_field
->getColumn(), $value);
}
else {
$query
->propertyCondition($id_property, $value);
}
// Only one result is returned. This assumes the reference fields are unique
// for every entity.
$results = $query
->range(0, 1)
->execute();
if ($results[$target_entity_type]) {
return key($results[$target_entity_type]);
}
// If no entity could be found, fall back to the original value.
return $value;
}