You are here

function farm_plan_linked_records in farmOS 7

Load a list of records associated with a plan.

Parameters

string $record_type: The record type (see farm_plan_record_relationships()).

int $plan_id: The plan ID.

Return value

array Returns an array of record IDs associated with the plan ID.

4 calls to farm_plan_linked_records()
farm_plan_delete_form in modules/farm/farm_plan/farm_plan.pages.inc
Delete confirmation form.
farm_plan_entity_update in modules/farm/farm_plan/farm_plan.module
Implements hook_entity_update().
farm_plan_entity_view in modules/farm/farm_plan/farm_plan.module
Implements hook_entity_view().
farm_plan_map_entity_view_alter in modules/farm/farm_plan/farm_plan_map/farm_plan_map.module
Implements hook_entity_view_alter().

File

modules/farm/farm_plan/farm_plan.module, line 709
Farm plan - A farm plan entity type.

Code

function farm_plan_linked_records($record_type, $plan_id) {

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

  // If the plan ID is empty, bail.
  if (empty($plan_id)) {
    return;
  }

  // Get available relationships between plans and other record types.
  $relationships = farm_plan_record_relationships();

  // If a database table and field are not available, bail.
  if (empty($relationships[$record_type]['table']) || empty($relationships[$record_type]['field'])) {
    return;
  }

  // Get the table and field.
  $table = $relationships[$record_type]['table'];
  $field = $relationships[$record_type]['field'];

  // Query for record IDs.
  $query = db_select($table, 'r');
  $query
    ->addField('r', $field);
  $query
    ->condition('plan_id', $plan_id);
  $result = $query
    ->execute();
  foreach ($result as $row) {
    if (!empty($row->{$field})) {
      $records[] = $row->{$field};
    }
  }

  // Return the record IDs.
  return $records;
}