services_entity.module in Services Entity API 7
File
services_entity.module
View source
<?php
function services_entity_prepare_structure(EntityStructureWrapper $wrapper, array $properties = array()) {
if (empty($properties)) {
$properties = array();
foreach ($wrapper
->getPropertyInfo() as $property_name => $property_info) {
if (empty($property_info['computed'])) {
$properties[$property_name] = $property_name;
}
}
}
return _services_entity_prepare_structure($wrapper, $properties);
}
function _services_entity_prepare_structure(EntityMetadataWrapper $wrapper, array $fields) {
$result = array();
foreach ($fields as $property_name => $sub_properties) {
$property_wrapper = $wrapper->{$property_name};
$type = $property_wrapper
->type();
$info = $property_wrapper
->info();
if (!empty($info['field'])) {
$field_info = field_info_field($property_name);
$translatable = !empty($field_info['translatable']) ? 1 : 0;
$cardinality = $field_info['cardinality'];
$underlying_entity = $wrapper
->value();
$languages = isset($underlying_entity->{$property_name}) ? array_keys($underlying_entity->{$property_name}) : array();
}
else {
$translatable = 0;
$cardinality = $type == 'list' || entity_property_list_extract_type($type) ? -1 : 1;
$languages = array(
LANGUAGE_NONE,
);
}
if ($sub_type = entity_property_list_extract_type($type)) {
$type = $sub_type;
$is_list = TRUE;
}
else {
$is_list = FALSE;
}
$property_result = array();
foreach ($languages as $language) {
$wrapper
->language($language);
$property_wrapper = $wrapper->{$property_name};
foreach ($is_list ? $property_wrapper : array(
$property_wrapper,
) as $index => $sub_wrapper) {
if ($sub_wrapper instanceof EntityStructureWrapper) {
$sub_wrapper
->language($language);
}
if (is_array($sub_properties)) {
$property_result[$language][$index] = services_entity_prepare_structure($sub_wrapper, $sub_properties);
}
else {
$value = $sub_wrapper
->raw();
if (is_array($value)) {
$property_result[$language][$index] = $value;
}
else {
$property_result[$language][$index] = array(
'value' => $type == 'boolean' && !$value ? '0' : $value,
);
}
}
}
}
if (count($property_result)) {
$result[$property_name] = $property_result + array(
'type' => $type,
'cardinality' => $cardinality,
'translatable' => $translatable,
);
}
else {
$result[$property_name] = NULL;
}
}
return $result;
}
function services_entity_entity_query_alter($query) {
$conditions =& $query->entityConditions;
if (isset($conditions['entity_type']) && $conditions['entity_type']['value'] == 'taxonomy_term' && isset($conditions['bundle'])) {
$vids = array();
if (is_array($conditions['bundle']['value'])) {
foreach ($conditions['bundle']['value'] as $vocabulary_machine_name) {
$vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
$vids[] = $vocabulary->vid;
}
}
else {
$vocabulary = taxonomy_vocabulary_machine_name_load($conditions['bundle']['value']);
$vids = $vocabulary->vid;
}
$query
->propertyCondition('vid', $vids, $conditions['bundle']['operator']);
unset($conditions['bundle']);
}
}