public function chado_linker__prop::load

3.x chado_linker__prop.inc public chado_linker__prop::load($entity)

Overrides TripalField::load

See also

TripalField::load()

File

tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop.inc, line 64

Class

chado_linker__prop

Code

public function load($entity) {
  $field_name = $this->field['field_name'];
  $field_type = $this->field['type'];
  $field_table = $this->instance['settings']['chado_table'];
  $field_column = $this->instance['settings']['chado_column'];
  $base_table = $this->instance['settings']['base_table'];

  $vocabulary = $this->instance['settings']['term_vocabulary'];
  $accession = $this->instance['settings']['term_accession'];
  $cvterm = chado_get_cvterm(array(
    'dbxref_id' => array(
      'db_id' => array(
        'name' => $vocabulary,
      ),
      'accession' => $accession,
    ),
  ));
  $cvterm_id = $cvterm->cvterm_id;

  // Get the FK that links to the base record.
  $schema = chado_get_schema($field_table);
  $pkey = $schema['primary key'][0];
  if (!isset($schema['foreign keys'][$base_table]['columns'])) {
    return;
  }
  $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];

  // Set some defaults for the empty record.
  $chado_record = $entity->chado_record;
  $entity->{$field_name}['und'][0] = array(
    'value' => '',
    'chado-' . $field_table . '__' . $pkey => '',
    'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_lcolumn,
    'chado-' . $field_table . '__value' => '',
    'chado-' . $field_table . '__type_id' => $cvterm_id,
    'chado-' . $field_table . '__rank' => '',
  );

  // Get the properties associated with this record for the given type.
  $columns = array('*');
  $match = array(
    $fkey_lcolumn => $chado_record->{$fkey_lcolumn},
    'type_id' => $cvterm_id,
  );
  $options = array(
    'return_array' => TRUE,
    'order_by' => array('rank' => 'ASC')
  );
  $properties = chado_select_record($field_table, $columns, $match, $options);
  for ($i = 0; $i < count($properties); $i++) {
    $property = $properties[$i];
    foreach ($schema['fields'] as $fname => $details) {
      $entity->{$field_name}['und'][$i] = array(
        'value' => $property->value,
        'chado-' . $field_table . '__' . $pkey => $property->$pkey,
        'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
        'chado-' . $field_table . '__value' => $property->value,
        'chado-' . $field_table . '__type_id' => $property->type_id,
        'chado-' . $field_table . '__rank' => $property->rank,
      );
    }
  }

  // Ensure there are no values if there are no properties.
  // This is necessary to make sure the field is not rendered when there are no properies.
  // @todo: We should revisit this in the future as none of the other fields do this.
  //        It was added here to make it easier to detect when the field was empty
  //        but in hindsight, we would just check $entity->{$field_name}['und'][$i]['value']
  //        in the formatter.
  if (empty($properties)) {
    unset($entity->{$field_name});
  }
}