You are here

public static function EntityFieldHandlerHelper::extract_property_multiple in Entity API 7

Extracts data from several metadata wrappers based on a data selector.

All metadata wrappers passed to this function have to be based on the exact same property information. The data will be returned wrapped by one or more metadata wrappers.

Can be used in query plugins for the get_result_entities() and get_result_wrappers() methods.

Parameters

array $wrappers: The EntityMetadataWrapper objects from which to extract data.

$selector: The selector specifying the data to extract.

Return value

array An array with numeric indices, containing the type of the extracted wrappers in the first element. The second element of the array contains the extracted property value(s) for each wrapper, keyed to the same key that was used for the respective wrapper in $wrappers. All extracted properties are returned as metadata wrappers.

File

views/handlers/entity_views_field_handler_helper.inc, line 245
Contains the EntityFieldHandlerHelper class.

Class

EntityFieldHandlerHelper
Helper class containing static implementations of common field handler methods.

Code

public static function extract_property_multiple(array $wrappers, $selector) {
  $parts = explode(':', $selector, 2);
  $name = $parts[0];
  $results = array();
  $entities = array();
  $type = '';
  foreach ($wrappers as $i => $wrapper) {
    try {
      $property = $wrapper->{$name};
      $type = $property
        ->type();
      if ($property instanceof EntityDrupalWrapper) {

        // Remember the entity IDs to later load all at once (so as to
        // properly utilize multiple load functionality).
        $id = $property
          ->getIdentifier();

        // Only accept valid ids. $id can be FALSE for entity values that are
        // NULL.
        if ($id) {
          $entities[$type][$i] = $id;
        }
      }
      elseif ($property instanceof EntityStructureWrapper) {
        $results[$i] = $property;
      }
      elseif ($property instanceof EntityListWrapper) {
        foreach ($property as $item) {
          $results[$i] = $item;
          $type = $item
            ->type();
          break;
        }
      }

      // Do nothing in case it cannot be applied.
    } catch (EntityMetadataWrapperException $e) {

      // Skip single empty properties.
    }
  }
  if ($entities) {

    // Map back the loaded entities back to the results array.
    foreach ($entities as $type => $id_map) {
      $loaded = entity_load($type, $id_map);
      foreach ($id_map as $i => $id) {
        if (isset($loaded[$id])) {
          $results[$i] = entity_metadata_wrapper($type, $loaded[$id]);
        }
      }
    }
  }

  // If there are no further parts in the selector, we are done now.
  if (empty($parts[1])) {
    return array(
      $type,
      $results,
    );
  }
  return self::extract_property_multiple($results, $parts[1]);
}