소스 검색

fixes to the entity menu behavior

Shawna Spoor 8 년 전
부모
커밋
b07d5f51d9
3개의 변경된 파일88개의 추가작업 그리고 112개의 파일을 삭제
  1. 1 74
      tripal_ds/includes/tripal_ds.field_formatter.inc
  2. 7 37
      tripal_ds/includes/tripal_ds.field_group.inc
  3. 80 1
      tripal_ds/tripal_ds.module

+ 1 - 74
tripal_ds/includes/tripal_ds.field_formatter.inc

@@ -56,77 +56,4 @@ function tripal_ds_field_group_pre_render(&$element, $group, &$form) {
       $element['#suffix'] = '</div>';
       break;
   }
-}
-
-/*
- * Implements hook_field_group_create_field_group
- * 
- */
-function tripal_ds_field_group_create_field_group($group) {
-
-  if($group->format_type == 'tripalpane'){
-    //write to the tripal_ds table to record the new tripal pane
-    $field_for_table = new stdClass();
-    $field_for_table->tripal_ds_field_name = $group->group_name;
-    $field_for_table->tripal_ds_field_label = $group->label;
-    $field_for_table->entity_type = 'TripalEntity';
-    $field_for_table->bundle = $group->bundle;
-
-    drupal_write_record('tripal_ds', $field_for_table);
-
-  }
-}
-
-/*
- * Implements hook_field_group_delete_field_group
- * 
- */
-function tripal_ds_field_group_delete_field_group($group) {
-
-  if($group->format_type == 'tripalpane'){
-    db_delete('tripal_ds')
-      ->condition('bundle', $group->bundle, '=')
-      ->condition('tripal_ds_field_name', $group->group_name, '=')
-      ->execute();
-    }
-}
-
-/*
- * Implements hook_field_group_update_field_group
- * checks for changes from tripalpane to another type
- */
-function tripal_ds_field_group_update_field_group($group) {
-  //Change from tripal pane to another type.
-  if(!empty($group->bundle)){
-    $result = db_select('tripal_ds', 't')
-      ->fields('t')
-      ->condition('bundle', $group->bundle,'=')
-      ->condition('tripal_ds_field_name', $group->group_name,'=')
-      ->execute()
-      ->fetchAssoc();
-
-    //Change from tripal pane to another type.
-    if($group->format_type !== 'tripalpane' && !empty($result)){
-       db_delete('tripal_ds')
-      ->condition('bundle', $result['bundle'], '=')
-      ->condition('tripal_ds_field_name', $result['tripal_ds_field_name'], '=')
-      ->execute();
-    }
-
-    //Change from other type to tripalpane.
-    if($group->format_type == 'tripalpane' && empty($result)){
-
-      if(strpos($group->group_name, 'Tripal Pane')){
-        $group_name = str_replace('Tripal Pane', "", $group->group_name);
-      } 
-      $field_for_table = new stdClass();
-      $field_for_table->tripal_ds_field_name = $group_name;
-      $field_for_table->tripal_ds_field_label = $group->label;
-      $field_for_table->entity_type = 'TripalEntity';
-      $field_for_table->bundle = $group->bundle;
-
-      drupal_write_record('tripal_ds', $field_for_table);
-    }
-  }
-}
-
+}

+ 7 - 37
tripal_ds/includes/tripal_ds.field_group.inc

@@ -34,13 +34,8 @@ function _summary_field_group_info($bundle_name, $fields){
   drupal_write_record('field_group', $field_group_tripalpane);
 
   //Write to the tripal_ds table to record the new tripal pane.
-  $field_for_table = new stdClass();
-  $field_for_table->tripal_ds_field_name = 'group_summary_tripalpane';
-  $field_for_table->tripal_ds_field_label = 'Summary';
-  $field_for_table->entity_type = 'TripalEntity';
-  $field_for_table->bundle = $bundle_name;
+  tripal_ds_bundle_menu_item($bundle_name, 'Summary', 'group_summary_tripalpane', 'TripalEntity');
 
-  drupal_write_record('tripal_ds', $field_for_table);
 
   //Fieldset field to nest the table within.
   $field_group_fieldset = new stdClass();
@@ -122,7 +117,7 @@ function _prop_field_group_info($bundle_name, $fields){
   $field_group_tripalpane->mode = 'default';
   $field_group_tripalpane->parent_name = '';
   $field_group_tripalpane->data = array(
-    'label' => 'Tripal Pane Properties',
+    'label' => 'Properties Tripal Pane',
     'weight' => '2',
     'children' => array(
         0 => 'group_prop',
@@ -140,13 +135,7 @@ function _prop_field_group_info($bundle_name, $fields){
   drupal_write_record('field_group', $field_group_tripalpane);
 
   //write to the tripal_ds table to record the new tripal pane.
-  $field_for_table = new stdClass();
-  $field_for_table->tripal_ds_field_name = 'group_prop_tripalpane';
-  $field_for_table->tripal_ds_field_label = 'Properties';
-  $field_for_table->entity_type = 'TripalEntity';
-  $field_for_table->bundle = $bundle_name;
-
-  drupal_write_record('tripal_ds', $field_for_table);
+  tripal_ds_bundle_menu_item($bundle_name, 'Properties', 'group_prop_tripalpane', 'TripalEntity');
 
 
   //Fieldset field to nest the table within.
@@ -245,13 +234,7 @@ function _data_sequence_field_group_info($bundle_name, $fields){
   drupal_write_record('field_group', $field_group_tripalpane);
 
   //Write to the tripal_ds table to record the new tripal pane.
-  $field_for_table = new stdClass();
-  $field_for_table->tripal_ds_field_name = 'group_sequence_tripalpane';
-  $field_for_table->tripal_ds_field_label = 'Sequence';
-  $field_for_table->entity_type = 'TripalEntity';
-  $field_for_table->bundle = $bundle_name;
-
-  drupal_write_record('tripal_ds', $field_for_table);
+  tripal_ds_bundle_menu_item($bundle_name, 'Sequence', 'group_sequence_tripalpane', 'TripalEntity');
 
   //Fieldset field to nest the table within.
   $field_group_fieldset = new stdClass();
@@ -323,13 +306,7 @@ function _data_sequence_field_group_info($bundle_name, $fields){
 
 function _additional_fields_field_group_info($bundle_name, $field_label, $group_field_name, $fieldset_field_name, $field_name){
   //Write to the tripal_ds table to record the new tripal pane.
-  $field_for_table = new stdClass();
-  $field_for_table->tripal_ds_field_name = $group_field_name;
-  $field_for_table->tripal_ds_field_label = $field_label;
-  $field_for_table->entity_type = 'TripalEntity';
-  $field_for_table->bundle = $bundle_name;
-
-  drupal_write_record('tripal_ds', $field_for_table);
+  tripal_ds_bundle_menu_item($bundle_name, $field_label, $group_field_name, 'TripalEntity');
 
   //Tripal pane to nest the fieldset within.
   $field_group_fieldset = new stdClass();
@@ -410,7 +387,7 @@ function _publication_prop_field_group_info($bundle_name, $fields){
   $field_group_tripalpane->mode = 'default';
   $field_group_tripalpane->parent_name = '';
   $field_group_tripalpane->data = array(
-    'label' => 'Tripal Pane Properties',
+    'label' => 'Properties Tripal Pane',
     'weight' => '2',
     'children' => array(
       0 => 'group_prop',
@@ -428,14 +405,7 @@ function _publication_prop_field_group_info($bundle_name, $fields){
   drupal_write_record('field_group', $field_group_tripalpane);
 
   //write to the tripal_ds table to record the new tripal pane.
-  $field_for_table = new stdClass();
-  $field_for_table->tripal_ds_field_name = 'group_prop_tripalpane';
-  $field_for_table->tripal_ds_field_label = 'Properties';
-  $field_for_table->entity_type = 'TripalEntity';
-  $field_for_table->bundle = $bundle_name;
-
-  drupal_write_record('tripal_ds', $field_for_table);
-
+  tripal_ds_bundle_menu_item($bundle_name, 'Properties', 'group_prop_tripalpane', 'TripalEntity');
 
   //Fieldset field to nest the table within.
   $field_group_fieldset = new stdClass();

+ 80 - 1
tripal_ds/tripal_ds.module

@@ -60,16 +60,95 @@ function tripal_ds_bundle_postcreate($bundle) {
 
 }
 
+/**
+ * Update the tripal_ds table when a tripal pane is deleted.
+ */
 function tripal_ds_table_column_delete($bundle){
     $bundle_name = $bundle->name;
     db_delete('tripal_ds')
       ->condition('bundle', $bundle_name, '=')
       ->execute();
 }
-
+/**
+ * Trigger the update to the tripal_ds table when a tripal pane is deleted.
+ */
 function tripal_ds_bundle_delete($bundle){
   tripal_ds_table_column_delete($bundle);
 }
+
+/*
+ * Implements hook_ds_field_settings_alter()
+ */
+function tripal_ds_ds_field_settings_alter(&$field_settings, $form, $form_state){
+  // Get the form info from the bundle about to be saved.
+  $tripal_entity_object = $form_state['build_info']['args']['1'];
+  // Grab the bundle.
+  $bundle_id = $tripal_entity_object->name;
+  // Grab the field groups from the bundle.
+  $updated_field_groups = $form_state['field_group'];
+  // Grab the fields from the bundle.
+  $fields = $form_state['values']['fields'];
+
+  // Delete the menu items associated with the bundle id.
+  db_delete('tripal_ds')
+    ->condition('bundle', $bundle_id, '=')
+    ->execute();
+
+  // Traverse the updated field_groups grabbing the tripal pane items.
+  $tripal_pane_field_groups = array();
+  $i = 0;
+  foreach($updated_field_groups as $updated_field_group){
+    if($updated_field_group->format_type == 'tripalpane'){
+      $tripal_pane_field_groups += [ $i => $updated_field_group->group_name];
+      $i++;
+    }
+  }
+
+  // Now grab the labels of the field_groups whose parent is a tripalpane.
+  foreach($updated_field_groups as $updated_field_group){
+    foreach($tripal_pane_field_groups as $tripal_pane_field_group){
+      if($updated_field_group->parent_name == $tripal_pane_field_group){
+        if($fields[$tripal_pane_field_group]['region'] !== 'hidden'){
+          tripal_ds_bundle_menu_item($bundle_id, $updated_field_group->label, $tripal_pane_field_group, 'tripalentity');
+        }
+      }
+    }
+  }
+}
+
+/**
+ * Trigger the update to the tripal_ds table when a tripal pane is deleted.
+ */
+function tripal_ds_bundle_menu_item($bundle_name, $field_label, $field_name, $entity_type){
+
+  //Check the record does not already exist
+  $tripal_ds_rows = db_select('tripal_ds', 'ds')
+    ->fields('ds', array('tripal_ds_field_name', 'tripal_ds_field_label'))
+    ->condition('bundle', $bundle_name, '=')
+    ->condition('tripal_ds_field_label', $field_label, '=')
+    ->condition('tripal_ds_field_name', $field_name, '=')
+    ->execute()->fetchAll();
+
+  if(!empty($tripal_ds_rows)){
+    foreach ($tripal_ds_rows as $tripal_ds_row){
+      if(($field_label == $tripal_ds_row->tripal_ds_field_label) && ($field_name == $tripal_ds_row->tripal_ds_field_name) && ($bundle_name == $tripal_ds_rows->bundle)) {
+        // Do not write the field to the table
+        drupal_set_message("Could not update the bundle menu because that field already exists.", 'error');
+      }
+    }
+  }
+  else {
+    //Write to the tripal_ds table to record the new tripal pane.
+    $field_for_table = new stdClass();
+    $field_for_table->tripal_ds_field_name = $field_name;
+    $field_for_table->tripal_ds_field_label = $field_label;
+    $field_for_table->entity_type = $entity_type;
+    $field_for_table->bundle = $bundle_name;
+    drupal_write_record('tripal_ds', $field_for_table);
+
+  }
+}
+
 /*
  * Implements hook_ds_layout_info() to define layouts from code in a module for
  * display suite