Browse Source

Added functionality to generate titles based on the patter/format saved for the entity type/bundle.

Lacey Sanderson 9 years ago
parent
commit
eaef9dacf1

+ 1 - 53
tripal_entities/api/tripal_entities.api.inc

@@ -1,57 +1,5 @@
 <?php
 
-/**
- * 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;
-}
-
 /**
  * Creates a new Tripal Entity type (i.e. bundle).
  *
@@ -236,4 +184,4 @@ function tripal_save_title_format($entity, $format) {
     drupal_write_record('tripal_bundle_variables', $record);
   }
 
-}
+}

+ 39 - 7
tripal_entities/includes/TripalEntityController.inc

@@ -81,13 +81,45 @@ class TripalEntityController extends EntityAPIController {
    * @param $entity
    * @param $title
    */
-  public function setTitle($entity, $title) {
-    db_update('tripal_entity')
-      ->fields(array(
-        'title' => $title
-      ))
-      ->condition('id', $entity->id)
-      ->execute();
+  public function setTitle($entity, $title = NULL) {
+
+    // If no title was supplied then we should try to generate one using the
+    // default format set by admins.
+    if (!$title) {
+      
+      // First get the format for the title based on the bundle of the entity.
+      $bundle_entity = tripal_bundle_load($entity->bundle);
+      $title = tripal_get_title_format($bundle_entity);
+    
+      // Determine which tokens were used in the format string
+      if (preg_match_all('/\[\w+\.\w+\]/', $title, $matches)) {
+        $used_tokens = $matches[0];
+        
+        foreach($used_tokens as $token) {
+          $field = str_replace(array('.','[',']'),array('__','',''),$token);
+          
+          $value = '';
+          if (isset($entity->{$field})) {
+            
+            // Render the value from the field.
+            $field_value = field_get_items('TripalEntity', $entity, $field);
+            $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
+            $value = render($field_render_arr);
+          }
+          $title = str_replace($token, $value, $title);
+        }
+      }
+    }
+    
+    // As long as we were able to determine a title, we should update it ;-).
+    if ($title) {
+      db_update('tripal_entity')
+        ->fields(array(
+          'title' => $title
+        ))
+        ->condition('id', $entity->id)
+        ->execute();
+    }
   }
 
   /**

+ 9 - 10
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -1,23 +1,22 @@
 <?php
 
 /**
- *
- * Implements hook_entity_load().
+ * Implements hook_entity_presave().
  */
-function tripal_entities_entity_presave($entity, $type) {
+function tripal_entities_entity_presave($entity, $type) { }
 
-}
 /**
- *
- * @param $entity
- * @param $type
+ * Implements hook_entity_postsave().
  */
 function tripal_entities_entity_postsave($entity, $type) {
-  // Set the title for this entity using the chado data.
-  $title = chado_get_entity_title($entity);
+  
+  // Set the title for this entity.
+  // This needs to be done post save because it uses the saved data and a format.
   $ec = new TripalEntityController($entity->type);
-  $ec->setTitle($entity, $title);
+  $ec->setTitle($entity);
+  
 }
+
 /**
  *
  * Implements hook_entity_load().