Browse Source

Added support for Entity titles

Stephen Ficklin 9 years ago
parent
commit
d88748f1bb

+ 50 - 0
tripal_entities/api/tripal_entities.api.inc

@@ -44,4 +44,54 @@ function tripal_entity_uri(TripalEntity $entity){
   );
 }
 
+/**
+ * TODO: The code for creating the title needs to be updated to not
+ * use nodes but rather entities.
+ *
+ * @param unknown $node
+ * @return mixed
+ */
+function chado_get_entity_title($entity) {
+
+  // Get the base table for the entity
+  $details = db_select('chado_entity', 'ce')
+    ->fields('ce')
+    ->condition('entity_id', $entity->id)
+    ->execute()
+    ->fetchObject();
+
+  $tablename = $details->data_table;
+  $type_field = $details->field;
+  $schema = chado_get_schema($tablename);
+  $pkey_field = $schema['primary key'][0];
+  $record_id = $details->record_id;
+
+  $record = chado_generate_var($tablename, array($pkey_field => $record_id));
 
+  // TODO: fix this so it's native for entities and doesn't expect nodes.
+  // Fake a node
+  $node = new stdClass();
+  $node->$tablename = $record;
+
+  // Get the tokens and format
+  $tokens = array(); // this will be set by chado_node_get_title_format
+  $title = chado_node_get_title_format('chado_' . $tablename, $tokens);
+
+  // Determine which tokens were used in the format string
+  if (preg_match_all('/\[[^]]+\]/', $title, $used_tokens)) {
+
+    // Get the value for each token used
+    foreach ($used_tokens[0] as $token) {
+      $token_info = $tokens[$token];
+      if (!empty($token_info)) {
+        $value = chado_get_token_value($token_info, $node);
+        $title = str_replace($token, $value, $title);
+      }
+    }
+  }
+  else {
+    return $title;
+  }
+
+  return $title;
+}

+ 22 - 6
tripal_entities/includes/TripalEntityController.inc

@@ -76,6 +76,21 @@ class TripalEntityController extends EntityAPIController {
     }
   }
 
+  /**
+   * Sets the title for an entity.
+   *
+   * @param $entity
+   * @param $title
+   */
+  public function setTitle($entity, $title) {
+    db_update('tripal_entity')
+      ->fields(array(
+        'title' => $title
+      ))
+      ->condition('id', $entity->id)
+      ->execute();
+  }
+
   /**
    * Saves the custom fields using drupal_write_record().
    */
@@ -95,14 +110,14 @@ class TripalEntityController extends EntityAPIController {
     }
 
     // Invoke hook_entity_presave().
-    module_invoke_all('entity_presave', $entity, $entity->entity_type);
+    module_invoke_all('entity_presave', $entity, $entity->type);
 
     // Write out the entity record.
     $record = array(
       'cvterm_id' => $entity->cvterm_id,
-      'type'      => $entity->entity_type,
+      'type'      => $entity->type,
       'bundle'    => $entity->bundle,
-      'title'     => 'title',
+      'title'     => $entity->title,
       'uid'       => $user->uid,
       'created'   => $entity->created,
       'changed'   => time(),
@@ -120,14 +135,15 @@ class TripalEntityController extends EntityAPIController {
     // to determine whether to update or insert, and which hook we
     // need to invoke.
     if ($invocation == 'entity_insert') {
-      field_attach_insert($entity->entity_type, $entity);
+      field_attach_insert($entity->type, $entity);
     }
     else {
-      field_attach_update($entity->entity_type, $entity);
+      field_attach_update($entity->type, $entity);
     }
 
     // Invoke either hook_entity_update() or hook_entity_insert().
-    module_invoke_all($invocation, $entity, $entity->entity_type);
+    module_invoke_all('entity_postsave', $entity, $entity->type);
+    module_invoke_all($invocation, $entity, $entity->type);
 
     return $entity;
   }

+ 3 - 3
tripal_entities/includes/TripalEntityUIController.inc

@@ -203,7 +203,7 @@ function tripal_entity_form($form, &$form_state, $entity = NULL) {
       '#type'  => 'hidden',
       '#value' => $cv_id,
     );
-    $form['entity_type'] = array(
+    $form['type'] = array(
       '#type'  => 'hidden',
       '#value' => $cvterm->dbxref_id->db_id->name,
     );
@@ -283,7 +283,7 @@ function tripal_entity_form_validate($form, &$form_state) {
 
   if ($form_state['clicked_button']['#name'] == 'add_data') {
     $tripal_entity = (object) $form_state['values'];
-    $entity_type = $form_state['values']['entity_type'];
+    $entity_type = $form_state['values']['type'];
     field_attach_form_validate($entity_type, $tripal_entity, $form, $form_state);
   }
 }
@@ -311,7 +311,7 @@ function tripal_entity_form_submit($form, &$form_state) {
       $form_state['clicked_button']['#name'] == 'add_data') {
     // Use the Entity API to get the entity from the form state, then
     // attach the fields and save.
-    $entity_type = $form_state['values']['entity_type'];
+    $entity_type = $form_state['values']['type'];
     $entity = entity_ui_controller($entity_type)->entityFormSubmitBuildEntity($form, $form_state);
     $entity->save();
     $form_state['redirect'] = "data/$entity->id";

+ 18 - 0
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -1,5 +1,23 @@
 <?php
 
+/**
+ *
+ * Implements hook_entity_load().
+ */
+function tripal_entities_entity_presave($entity, $type) {
+
+}
+/**
+ *
+ * @param $entity
+ * @param $type
+ */
+function tripal_entities_entity_postsave($entity, $type) {
+  // Set the title for this entity using the chado data.
+  $title = chado_get_entity_title($entity);
+  $ec = new TripalEntityController($entity->type);
+  $ec->setTitle($entity, $title);
+}
 /**
  *
  * Implements hook_entity_load().

+ 1 - 1
tripal_entities/includes/tripal_entities.fields.inc

@@ -144,7 +144,7 @@ function tripal_entities_field_widget_form(&$form, &$form_state, $field,
       $accession = '';
       $version = '';
       $description = '';
-      if ($items[0]['value']) {
+      if (count($items) > 0 and $items[0]['value']) {
         $dbxref = chado_generate_var('dbxref', array('dbxref_id' => $items[0]['value']));
         $dbxref_id = $dbxref->dbxref_id;
         $db_id = $dbxref->db_id->db_id;