function tripal_insert_entity
3.x tripal.entities.api.inc | tripal_insert_entity($bundle_name, $values) |
Is this completed? It doesn't look right and I can't find it used anywhere in the existing code.
Parameters
$bundle_name: The name of the bundle (e.g. bio_data_xx)
unknown $values:
Throws
Exception
File
- tripal/
api/ tripal.entities.api.inc, line 1353 - Provides an application programming interface (API) for working with TripalEntity content types (bundles) and their entities.
Code
function tripal_insert_entity($bundle_name, $values) {
global $user;
$bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
// Get the fields associated with this content type.
$instances = field_info_instances('TripalEntity', $bundle->name);
foreach ($instances as $instance) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
$field_type = $field['type'];
$field_settings = $field['settings'];
$instance_settings = $instance['settings'];
$field_name = $field['field_name'];
$vocabulary = $instance['settings']['term_vocabulary'];
$accession = $instance['settings']['term_accession'];
$field_accession = $vocabulary . ':' . $accession;
$field_term = tripal_get_term_details($vocabulary, $accession);
$field_key = $field_term['name'];
$field_key = strtolower(preg_replace('/ /', '_', $key));
// There are three ways that a field value can be specified. Those
// are as the controlled vocabulary accession (e.g. GO:0000134), sa
// the field name or as the field key which is the term name with
// spaces replaced with underscores.
// First make sure that required fields are present.
if ($instance['required'] == TRUE) {
if (!array_key_exists($field_key, $values) and
!array_key_exists($field_accession, $values) and
!array_key_exists($field_name, $values)) {
throw new Exception(t('Cannot insert the record. Missing the required field "%missing".',
array('%missing' => $field_name)));
}
}
}
// Make sure that all required fields are present.
// TODO: make sure the user has permission to do this.
$ec = entity_get_controller('TripalEntity');
$entity = $ec->create(array(
'bundle' => $bundle_name,
'term_id' => $bundle->term_id,
));
$entity = $entity->save();
}