Browse Source

Added bundle_id token for use in url alias' and titles

Lacey Sanderson 9 years ago
parent
commit
5b2a7b7f35

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

@@ -334,6 +334,18 @@ function tripal_get_tokens($entity, $options = array()) {
 
   // Set default options.
   $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
+  $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
+  
+  if ($options['include id']) {
+    $token = '[TripalBundle__bundle_id]';
+    $tokens[$token] = array(
+      'label' => 'Bundle ID',
+      'description' => 'The unique identifier for this Tripal Content Type.',
+      'token' => $token,
+      'field_name' => NULL,
+      'required' => TRUE
+    );
+  }
   
   $fields = field_info_instances('TripalEntity', $entity->name);
   foreach ($fields as $f) {
@@ -362,6 +374,56 @@ function tripal_get_tokens($entity, $options = array()) {
   return $tokens;
 }
 
+/**
+ * Replace all Tripal Tokens in a given string.
+ *
+ * NOTE: If there is no value for a token then the token is removed.
+ *
+ * @param string $string
+ *   The string containing tokens.
+ * @param TripalEntity $entity
+ *   The entity with field values used to find values of tokens.
+ * @param TripalBundle $bundle_entity
+ *   The bundle enitity containing special values sometimes needed for token replacement.
+ *
+ * @return
+ *   The string will all tokens replaced with values.
+ */
+function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
+
+  // Determine which tokens were used in the format string
+  if (preg_match_all('/\[\w+\]/', $string, $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.
+        // @TODO: Handle the case where thefield is empty... currently returns error.
+        $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);
+      }
+      elseif ($field === 'TripalBundle__bundle_id') {
+        
+        // Load the bundle entity if we weren't given it.
+        if (!$bundle_entity) {
+          $bundle_entity = tripal_load_bundle_entity($entity->bundle);
+        }
+
+        // This token should be the id of the TripalBundle.
+        $value = $bundle_entity->id;
+      }
+      $string = str_replace($token, $value, $string);
+    }
+  }
+
+  return $string;
+}
+
 /**
  * Formats the tokens for display.
  *
@@ -376,7 +438,7 @@ function theme_token_list($tokens) {
   $rows = array();
   foreach ($tokens as $details) {
     $rows[] = array(
-      '[' . $details['field_name'] . ']',
+      $details['token'],
       $details['label'],
       $details['description'],
     );

+ 11 - 40
tripal_entities/includes/TripalEntityController.inc

@@ -86,29 +86,15 @@ class TripalEntityController extends EntityAPIController {
     // default format set by admins.
     if (!$title) {
 
-      // First get the format for the title based on the bundle of the entity.
+      // Load the TripalBundle entity for this TripalEntity.
       $bundle_entity = tripal_load_bundle_entity($entity->bundle);
+      
+      // First get the format for the title based on the bundle of the entity.
       $title = tripal_get_title_format($bundle_entity);
+      
+      // And then replace all the tokens with values from the entity fields.
+      $title = tripal_replace_tokens($title, $entity, $bundle_entity);
 
-      // Determine which tokens were used in the format string
-      if (preg_match_all('/\[\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.
-            // @TODO: Handle the case where thefield is empty... currently returns error.
-            $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 ;-).
@@ -132,29 +118,14 @@ class TripalEntityController extends EntityAPIController {
     // default format set by admins.
     if (!$alias) {
     
-      // First get the format for the url alias based on the bundle of the entity.
+      // Load the TripalBundle entity for this TripalEntity.
       $bundle_entity = tripal_bundle_load($entity->bundle);
+      
+      // First get the format for the url alias based on the bundle of the entity.
       $alias = tripal_get_bundle_variable('url_format', $bundle_entity->id);
 
-      // Determine which tokens were used in the format string
-      if (preg_match_all('/\[\w+\]/', $alias, $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.
-            // @TODO: Handle the case where thefield is empty... currently returns error.
-            $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);
-          }
-          $alias = str_replace($token, trim($value), $alias);
-        }
-      }
+      // And then replace all the tokens with values from the entity fields.
+      $alias = tripal_replace_tokens($alias, $entity, $bundle_entity);
     }
 
     // Make sure the alias doesn't contain spaces.