Browse Source

Added a URL alias default for Tripal Entities consisting of the term name and entity_id

Lacey Sanderson 9 years ago
parent
commit
ab7a6a2a31

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

@@ -345,6 +345,15 @@ function tripal_get_tokens($entity, $options = array()) {
       'field_name' => NULL,
       'required' => TRUE
     );
+
+    $token = '[TripalEntity__entity_id]';
+    $tokens[$token] = array(
+      'label' => 'Content/Entity ID',
+      'description' => 'The unique identifier for an individual piece of Tripal Content.',
+      'token' => $token,
+      'field_name' => NULL,
+      'required' => TRUE
+    );
   }
   
   $fields = field_info_instances('TripalEntity', $entity->name);
@@ -390,7 +399,7 @@ function tripal_get_tokens($entity, $options = array()) {
  *   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];
@@ -417,6 +426,11 @@ function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
         // This token should be the id of the TripalBundle.
         $value = $bundle_entity->id;
       }
+      elseif ($field === 'TripalEntity__entity_id') {
+
+        // This token should be the id of the TripalEntity.
+        $value = $entity->id;
+      }
       $string = str_replace($token, $value, $string);
     }
   }

+ 20 - 18
tripal_entities/includes/TripalBundleUIController.inc

@@ -196,8 +196,8 @@ function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType
 
   // Set URL Alias Pattern.
   //-------------------------
-  // @TODO: Find a way to use the Entity ID in the url.
   $url_pattern = tripal_get_bundle_variable('url_format', $entity_type->id, '');
+  if (!$url_pattern) $url_pattern = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
   
   $form['url'] = array(
     '#type' => 'fieldset',
@@ -224,6 +224,7 @@ function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType
       alias. The available tokens are listed below. <strong>Make sure the pattern forms a 
       valid, unique URL</strong>. Leave this field blank to use the original path.'),
     '#default_value' => $url_pattern,
+    '#required' => TRUE,
     '#rows' => 1
   );
   
@@ -293,27 +294,28 @@ function tripal_entities_tripal_bundle_form_validate($form, $form_state) {
   }
   
   // PART 2: URL Alias'
-  $tokens_available = unserialize($form_state['values']['url']['tokens']);
-  if (preg_match_all('/(\[\w+\])/', $form_state['values']['url']['url_pattern'], $matches)) {
-
-    // The matches of the first and only pattern will be our tokens.
-    $tokens_used = $matches[1];
-    // Determine if any of the tokens used were not in the original list of available tokens.
-    $tokens_missing = array_diff($tokens_used, array_keys($tokens_available));
+  if ($form_state['values']['url']['url_pattern']) {
+    $tokens_available = unserialize($form_state['values']['url']['tokens']);
+    if (preg_match_all('/(\[\w+\])/', $form_state['values']['url']['url_pattern'], $matches)) {
+
+      // The matches of the first and only pattern will be our tokens.
+      $tokens_used = $matches[1];
+      // Determine if any of the tokens used were not in the original list of available tokens.
+      $tokens_missing = array_diff($tokens_used, array_keys($tokens_available));
+
+      if ($tokens_missing) {
+        $msg = t('You must only use tokens listed under available tokens. You used the following incorrect tokens: %tokens',
+          array('%tokens' => implode(', ', $tokens_missing)));
+        form_set_error('url][url_pattern', $msg);
+      }
 
-    if ($tokens_missing) {
-      $msg = t('You must only use tokens listed under available tokens. You used the following incorrect tokens: %tokens',
-        array('%tokens' => implode(', ', $tokens_missing)));
+    }
+    else {
+      $msg = t('You should use at least one token in your URL pattern or the URL for all %type pages will be the same.',
+        array('%type' => $form_state['build_info']['args'][0]->label));
       form_set_error('url][url_pattern', $msg);
     }
-
   }
-  else {
-    $msg = t('You should use at least one token in your URL pattern or the URL for all %type pages will be the same.',
-      array('%type' => $form_state['build_info']['args'][0]->label));
-    form_set_error('url][url_pattern', $msg);
-  }
-
 }
 
 /**

+ 15 - 0
tripal_entities/includes/TripalEntityController.inc

@@ -127,6 +127,21 @@ class TripalEntityController extends EntityAPIController {
       // And then replace all the tokens with values from the entity fields.
       $alias = tripal_replace_tokens($alias, $entity, $bundle_entity);
     }
+    
+    // If there was no defaults supplied by the admins
+    // then we should gneerate our own using the term name and entity id.
+    if (!$alias) {
+      
+      // Load the term for this TripalEntity.
+      $term = entity_load('TripalTerm', array('id' => $entity->term_id));
+      $term = reset($term);
+
+      // Set a default based on the term name and entity id.
+      $alias = str_replace(' ', '', $term->name) . '/[TripalEntity__entity_id]';
+
+      // 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.
     $alias = preg_replace('/\s+/','-',$alias);