You are here

function farm_area_load_areas in farmOS 7

Load a list of areas (optionally by type).

Parameters

$type: Optionally specify an area type to filter by.

$sort: Optionally specify the property to sort by. Defaults to 'id'.

Return value

array Returns an array of areas.

File

modules/farm/farm_area/farm_area.module, line 166

Code

function farm_area_load_areas($type = '', $sort = 'id') {

  // Start with an empty array.
  $areas = array();

  // Start an entity field query for terms in the farm_areas vocabulary.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'taxonomy_term')
    ->entityCondition('bundle', 'farm_areas');

  // If the $sort argument is 'id', translate it to 'tid' (remove this if/when
  // areas become assets (see https://www.drupal.org/node/2363393).
  if ($sort == 'id') {
    $sort = 'tid';
  }

  // Sort the results.
  $query
    ->propertyOrderBy($sort, 'ASC');

  // If a type is defined, add a field condition to filter by that type.
  if (!empty($type)) {
    $query
      ->fieldCondition('field_farm_area_type', 'value', $type);
  }

  // Execute the query and load the areas.
  $result = $query
    ->execute();
  if (isset($result['taxonomy_term'])) {
    $area_ids = array_keys($result['taxonomy_term']);
    $areas = entity_load('taxonomy_term', $area_ids);
  }

  // Return the areas.
  return $areas;
}