Browse Source

Tripal Content Type Edit Form: Title formats now savedgit add . :-) Added api functions for getting and saving title formats.

Lacey Sanderson 9 years ago
parent
commit
5a74fe6697

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

@@ -163,3 +163,77 @@ function tripal_create_entity_type($cvterm, &$error = '') {
 
   return TRUE;
 }
+
+/**
+ * Get Page Title Format for a given Tripal Entity Type.
+ *
+ * @param TripalBundle $entity
+ *   The Entity object for the Tripal Bundle the title format is for.
+ */
+function tripal_get_title_format($entity) {
+
+  // Title formats are saved as Tripal Bundle Variables.
+  // Therefore, first we need the variable_id for title_formats.
+  $variable_id = db_select('tripal_variables', 'v')
+    ->fields('v', array('variable_id'))
+    ->condition('name', 'title_format')
+    ->execute()
+    ->fetchField();
+  
+  // Then we can check if there is already a title format for this bundle/type.
+  $title_format = db_select('tripal_bundle_variables', 'var')
+    ->fields('var', array('value'))
+    ->condition('var.bundle_id', $entity->id)
+    ->condition('var.variable_id', $variable_id)
+    ->execute()
+    ->fetchField();
+    
+  // If there isn't yet a title format for this bundle/type then we should
+  // determine the default based on the table unique constraint and save it.
+  // @TODO: Replace the label with the base table name.
+  // @TODO: make this chado independant.
+  if (!$title_format) {
+    $title_format = chado_node_get_unique_constraint_format($entity->label);
+    tripal_save_title_format($entity, $title_format);
+  }
+  
+  return $title_format;
+}
+
+/**
+ * Save Page Title Format for a given Tripal Entity Type.
+ *
+ * @param TripalBundle $entity
+ *   The Entity object for the Tripal Bundle the title format is for.
+ * @param string $format
+ *   The pattern to be used when generating entity titles for the above type.
+ */
+function tripal_save_title_format($entity, $format) {
+
+  // Title formats are saved as Tripal Bundle Variables.
+  // Thus first we need to grab the variable_id for title_format.
+  $variable_id = db_select('tripal_variables', 'v')->fields('v', array('variable_id'))->condition('name', 'title_format')->execute()->fetchField();
+
+  // And then we need to write the new format to the tripal_bundle_variables table.
+  $record = array(
+    'bundle_id' => $entity->id,
+    'variable_id' => $variable_id,
+    'value' => $format,
+  );
+  
+  // Check whether there is already a format saved.
+  $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
+    ->fields('var', array('bundle_variable_id'))
+    ->condition('var.bundle_id', $record['bundle_id'])
+    ->condition('var.variable_id', $record['variable_id'])
+    ->execute()
+    ->fetchField();
+  if ($bundle_variable_id) {
+    $record['bundle_variable_id'] = $bundle_variable_id;
+    drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
+  }
+  else {
+    drupal_write_record('tripal_bundle_variables', $record);
+  }
+
+}

+ 17 - 9
tripal_entities/includes/TripalBundleUIController.inc

@@ -88,10 +88,6 @@ function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType
       'type' => 'inline',
     ),
   );
-
-  //dpm($form_state, 'form state');
-  //dpm($entity_type, 'entity type');
-  //dpm($cvterm, 'cvterm');
   
   $form['vocab'] = array(
     '#type' => 'select',
@@ -117,8 +113,8 @@ function tripal_entities_tripal_bundle_form($form, &$form_state, $entityDataType
   
   // Set Title Format.
   //-------------------------
-  $title_format = chado_node_get_unique_constraint_format($chado_basetable);
-  
+  $title_format = tripal_get_title_format($entity_type);
+
   $form['set_titles'] = array(
     '#type' => 'fieldset',
     '#title' => t('Page Title options'),
@@ -220,9 +216,21 @@ function tripal_entities_tripal_bundle_form_validate($form, $form_state) {
 /**
  * Submit: Tripal content type edit form.
  */
-function tripal_entities_tripal_bundle_form_submit($form, $form_state) {
-
-  //dpm($form_state, 'form state in submit');
+function tripal_entities_tripal_bundle_form_submit($form, &$form_state) {
+  
+  if ($form_state['triggering_element']['#value'] == 'Save Content Type') {
+    // Save the page title format.
+    tripal_save_title_format(
+      $form_state['build_info']['args'][0], 
+      $form_state['values']['set_titles']['title_format']
+    );
+    
+    $form_state['redirect'] = 'admin/structure/bio-data';
+    drupal_set_message(t('Successfully saved %type content type.', array('%type' => $form_state['build_info']['args'][0]->label)));
+  }
+  else {
+    drupal_set_message(t('%button is not supported at this time.', array('%button' => $form_state['triggering_element']['#value'])), 'warning');
+  }
 }
 
 /**

+ 15 - 0
tripal_entities/tripal_entities.install

@@ -9,6 +9,21 @@
  */
 function tripal_entities_install() {
 
+  // Tripal Entity Bundles/Types use Tripal Variables to store additional information.
+  // We need to add pertinent variables on install.
+  $vars = array(
+    'title_format' => 'A pattern including tokens that can be used to generate tripal entity titles.',
+    'url_format' => 'A pattern including tokens that can be used to generate tripal entity url aliases.',
+    'description' => 'The description of a Tripal Entity type/bundle.'
+  );
+  foreach ($vars as $name => $description) {
+    $record = array(
+      'name' => $name,
+      'description' => $description
+    );
+    drupal_write_record('tripal_variables', $record);
+  }
+  
   // Unfortunately, some Chado base tables do not have a type_id, so we must
   // take special action for those tables.  These include: organism and
   // analysis. Until we can find an appropriate controlled vocabulary