You are here

function restws_field_collection_property_get in RESTful Web Services Field Collection 7

Get a field collection property.

1 string reference to 'restws_field_collection_property_get'
restws_field_collection_entity_property_info_alter in ./restws_field_collection.module
Implements hook_entity_property_info_alter().

File

./restws_field_collection.module, line 90
RESTful Web Services Field collection module.

Code

function restws_field_collection_property_get($data, array $options, $name, $type, $info) {

  // Start an empty property array.
  $property = array();

  // Get information about field collections.
  $field_collections = restws_field_collection_info();

  // Iterate through the field collections.
  foreach ($field_collections as $fc_name => $fc_info) {

    // If the property name does not match the field collection alias, skip it.
    if ($name != $fc_info['alias']) {
      continue;
    }

    // If the field collection is empty in the entity, return.
    if (empty($data->{$fc_name})) {
      return $property;
    }

    // Load the field collection items.
    foreach ($data->{$fc_name}[LANGUAGE_NONE] as $fc_ref) {

      // Start an empty property fields array.
      $property_fields = array();

      // If the field collection item ID is empty, skip it.
      if (empty($fc_ref['value'])) {
        continue;
      }

      // Load the field collection item.
      $fc_item = field_collection_item_load($fc_ref['value']);

      // If the item didn't load, skip it.
      if (empty($fc_item)) {
        continue;
      }

      // Map field values from the field collection item to the property.
      foreach ($fc_info['fields'] as $alias => $field_info) {

        // Create an empty property field.
        $property_fields[$alias] = '';

        // Get value(s) for the property field.
        $values = array();
        if (!empty($fc_item->{$field_info['field_name']}[LANGUAGE_NONE])) {
          foreach ($fc_item->{$field_info['field_name']}[LANGUAGE_NONE] as $delta => $field_value) {
            $values[] = $field_value[$field_info['field_value']];
          }
        }

        // If the field does not support multiple values, only use the first.
        if (empty($field_info['multiple'])) {
          $values = reset($values);
        }

        // Add the value(s) to the property field.
        $property_fields[$alias] = $values;
      }

      // If the property fields aren't empty, add them to the property.
      if (!empty($property_fields)) {
        $property[] = $property_fields;
      }
    }

    // If the field collection does not support multiple values, only use the
    // first.
    if (empty($fc_info['multiple'])) {
      $property = reset($property);
    }

    // Return the property value(s).
    return $property;
  }
}