function tripal_get_bundle_details
3.x tripal.entities.api.inc | tripal_get_bundle_details($bundle_name) |
Retrieves details, including attached fields, for a given bundle.
Parameters
$bundle_name: The name of the bundle (e.g. bio_data_xx)
Return value
An array containing the name, label, controlled vocabulary details and a list of fields attached to the bundle. Returns FALSE if the bundle does not exist.
Related topics
File
- tripal/
api/ tripal.entities.api.inc, line 1223 - Provides an application programming interface (API) for working with TripalEntity content types (bundles) and their entities.
Code
function tripal_get_bundle_details($bundle_name) {
global $user;
$bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
if (!$bundle) {
return FALSE;
}
$term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
$vocab = $term->vocab;
$instances = field_info_instances('TripalEntity', $bundle->name);
$details = array(
'name' => $bundle->name,
'label' => $bundle->label,
'term' => array(
'accession' => $vocab->vocabulary . ':' . $term->accession,
'name' => $term->name,
'definition' => $term->definition,
'url' => $term->url
),
'fields' => array(),
);
// Iterate through each feild and provide a discription of it and
// it's sub elements.
foreach ($instances as $instance) {
// Skip deleted fields.
if ($instance['deleted']) {
continue;
}
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
$field_class = $field['type'];
$term_vocab = $instance['settings']['term_vocabulary'];
$term_accession = $instance['settings']['term_accession'];
$field_term = tripal_get_term_details($term_vocab, $term_accession);
$field_details = array(
'name' => $field_name,
'label' => $instance['label'],
'term' => array(
'accession' => $term_vocab . ":" . $term_accession,
'name' => $field_term['name'],
'definition' => $field_term['definition'],
'url' => $field_term['url'],
),
// These items can be overridden by the element_info array that
// is present in a TripalField instance. Here we set defaults.
'required' => $instance['required'] ? TRUE : FALSE,
'type' => 'xs:string',
'readonly' => TRUE,
// The cardinatlity value always comes from the field.
'cardinality' => $field['cardinality'],
);
if (tripal_load_include_field_class($field_class)) {
$field_obj = new $field_class($field, $instance);
$element_info = $field_obj->elementInfo();
$element_info = $element_info[$term_vocab . ':' . $term_accession];
// If the element info for this field sets required, type and readonly
// attributes then set those.
$field_details['required'] = array_key_exists('required', $element_info) ? $element_info['required'] : FALSE;
$field_details['type'] = array_key_exists('type', $element_info) ? $element_info['type'] : 'xs:string';
$field_details['readonly'] = array_key_exists('readonly', $element_info) ? $element_info['readonly'] : TRUE;
$field_details['label'] = array_key_exists('label', $element_info) ? $element_info['label'] : $field_details['label'];
$field_details['help'] = array_key_exists('help', $element_info) ? $element_info['help'] : '';
// If this field is an 'xs:complexType' then it will have sub elements.
// we need to add those as well.
if (array_key_exists('elements', $element_info) and is_array($element_info['elements'])) {
_tripal_get_bundle_field_element_details($element_info['elements'], $field_details);
}
$details['fields'][] = $field_details;
}
}
return $details;
}