Преглед изворни кода

Working on field UI layouts

Stephen Ficklin пре 9 година
родитељ
комит
e536ca7bb9

+ 100 - 0
tripal_entities/tripal_entities.install

@@ -474,6 +474,106 @@ function tripal_entities_schema() {
     ),
     'primary key' => array('chado_entity_id'),
   );
+
+  $schema['tripal_panels'] = array(
+    'description' => 'The list of panels into which fields can be placed.',
+    'fields' => array(
+      'panel_id' => array(
+        'description' => 'The primary identifier for this table.',
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'bundle_id' => array(
+        'description' => 'A bundle ID from the tripal_bundle table.',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'name' => array(
+        'description' => 'The computer readable name for the panel. This name must only have alphanumerical characters and underscores and must not begin with a number. ',
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'label' => array(
+        'description' => 'A human readable name for panel. This name will be shown to users in the sidebar menu.',
+        'type' => 'varchar',
+        'length' => 128,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'settings' => array(
+        'description' => 'Contains a serialized array of settings for the panel.',
+        'type' => 'text',
+        'not null' => FALSE,
+      ),
+      'weight' => array(
+        'type' => 'int',
+        'not null' => FALSE,
+        'default' => 0
+      ),
+    ),
+    'indexes' => array(
+      'bundle_id' => array('bundle_id'),
+    ),
+    'unique keys' => array(
+      'bundle_panel' => array('bundle_id', 'name'),
+    ),
+    'foreign keys' => array(
+      'tripal_bundle' => array(
+        'table' => 'tripal_bundle',
+        'columns' => array(
+          'bundle_id' => 'id',
+        ),
+      ),
+    ),
+    'primary key' => array('panel_id'),
+  );
+
+  $schema['tripal_panel_fields'] = array(
+    'description' => 'The list of panels into which fields can be placed.',
+    'fields' => array(
+      'panel_field_id' => array(
+        'description' => 'The primary identifier for this table.',
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+      ),
+      'panel_id' => array(
+        'description' => 'The primary identifier for this table.',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'field_id' => array(
+        'description' => 'A bundle ID from the tripal_bundle table.',
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+    ),
+    'indexes' => array(
+      'panel_id' => array('panel_id'),
+      'field_id' => array('field_id'),
+    ),
+    'unique keys' => array(
+      'panel_field' => array('panel_id', 'field_id'),
+    ),
+    'foreign keys' => array(
+      'tripal_panel' => array(
+        'table' => 'tripal_panel',
+        'columns' => array(
+          'panel_id' => 'panel_id',
+        ),
+      ),
+      'field_config' => array(
+        'table' => 'field_config',
+        'columns' => array(
+          'field_id' => 'id',
+        ),
+      ),
+    ),
+    'primary key' => array('panel_field_id'),
+  );
   return $schema;
 }
 /**

+ 197 - 16
tripal_entities/tripal_entities.module

@@ -382,24 +382,205 @@ function tripal_entity_load($id, $reset = FALSE) {
 }
 
 /**
- * Implements hook_form_alter().
  *
- * @param unknown $form
- * @param unknown $form_state
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * The field_ui_field_edit_form is used for customizing the settings of
+ * a field attached to an entity.
  */
-function tripal_entities_form_alter(&$form, &$form_state, $form_id) {
-
-  switch ($form_id) {
-    case 'field_ui_field_edit_form':
-      // For entity fields added by Tripal Entities we don't want the
-      // the end-user to change the cardinality and the required fields
-      // such that record can't be saved in Chado.
-      $dbs = tripal_entities_get_db_names_for_published_vocabularies ();
-      if (in_array($form['#instance']['entity_type'], $dbs)) {
-         $form['field']['cardinality']['#access'] = FALSE;
-         $form['instance']['required']['#access'] = FALSE;
-      }
-      break;
+function tripal_entities_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
+  // For entity fields added by Tripal Entities we don't want the
+  // the end-user to change the cardinality and the required fields
+  // such that record can't be saved in Chado.
+  $dbs = tripal_entities_get_db_names_for_published_vocabularies();
+  if (in_array($form['#instance']['entity_type'], $dbs)) {
+    $form['field']['cardinality']['#access'] = FALSE;
+    $form['instance']['required']['#access'] = FALSE;
+  }
+
+  // TODO: don't the the maximum length be larger than the field size.
+}
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * The field_ui_display_overview_form is used for formatting the display
+ * or layout of fields attached to an entity.
+ */
+function tripal_entities_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
+
+  $entity_type = $form['#entity_type'];
+  $bundle_name = $form['#bundle'];
+
+  // Get the bundle record.
+  $bundle = db_select('tripal_bundle', 'tb')
+    ->fields('tb')
+    ->condition('bundle', $bundle_name)
+    ->execute()
+    ->fetchObject();
+
+  if (module_exists('ds')) {
+    drupal_set_message('Tripal is not compatible with the Display Suite (ds) module. If you would like to use Tripal-style panels for the layout of your pages please disable the Display Suite module.', 'warning');
+  }
+
+  // Add a vertical tab fieldset at the bottom of the
+  $form['overview_vert_tabs'] = array(
+    '#type' => 'vertical_tabs'
+  );
+  $form['modes']['#group'] = 'overview_vert_tabs';
+  $form['modes']['#weight'] = 1000;
+  $form['tripal_entity_layout'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Panels',
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#group' => 'overview_vert_tabs'
+  );
+  $form['tripal_entity_layout']['panel_name'] = array(
+    '#type' => 'textfield',
+    '#title' => 'Add a new Panel',
+    '#description' => t('You may add as many panels to your page layout as
+        desired. Panels can be used to organize fields into categories....')
+  );
+
+  // Make sure our default panels are in the database.
+  _tripal_entities_check_default_field_panels();
+
+  // Now add each panel as a region.
+  $form['fields']['#regions'] = array();
+  $panels = db_select('tripal_panels', 'tp')
+    ->fields('tp')
+    ->condition('bundle_id', $bundle->id)
+    ->orderBy('weight', 'ASC')
+    ->orderBy('label', 'ASC')
+    ->execute();
+  $panel_options = array();
+  while ($panel = $panels->fetchObject()) {
+    $settings = unserialize($panel->settings);
+    $form['fields']['#regions'][$panel->name] = array(
+      'title' => t($panel->label),
+      'message' => t($settings['message']),
+    );
+    $panel_options[$panel->name] = $panel->label;
+  }
+
+  // Set the table headers to add a new 'region' column.
+  $form['fields']['#header'] = array(
+    t('Field'),
+    t('Weight'),
+    t('Parent'),
+    t('Label'),
+    array('data' => t('Format'), 'colspan' => 3),
+    t('Region'),
+  );
+
+  // Change the region callback for each field to place each field in the
+  // proper "panel" region. Also, set the default region to be the base region.
+  $fields = $form['fields'];
+  foreach (element_children($fields) as $field_name) {
+    $form['fields'][$field_name]['#region_callback'] = 'tripal_entities_field_ui_row_region';
+    $form['fields'][$field_name]['region'] = array(
+      '#type' => 'select',
+      '#options' => $panel_options,
+      '#default_value' => 'te_base',
+      '#attributes' => array(
+        'class' => array('te-field-region'),
+      )
+    );
+  }
+
+  // Add validate and submit handlers. Rearrange the submit callbacks
+  // so ours is first.
+  $form['#validate'][] = 'tripal_entities_field_ui_validate';
+  $submit = $form['#submit'];
+  $form['#submit'] = array('tripal_entities_field_ui_submit');
+  $form['#submit'] = array_merge($form['#submit'], $submit);
+}
+
+/**
+ * A helper function for checking if the default panels are in the database.
+ */
+function _tripal_entities_check_default_field_panels() {
+  // Make sure we have records for our default regions: te_base and te_hidden.
+  // First check if the base region is in the database. If not, add it.
+  $te_base = db_select('tripal_panels', 'tp')
+    ->fields('tp')
+    ->condition('name', 'te_base')
+    ->execute()
+    ->fetchObject();
+  if (!$te_base) {
+    $settings = array(
+      'message' => 'Fields that should appear at the top of each page should be placed in the base panel. These fields are always present and help orient the user by at least indicating the necessary information to uniquely identiy the content.',
+    );
+    db_insert('tripal_panels')
+      ->fields(array(
+        'bundle_id' => $bundle->id,
+        'name' => 'te_base',
+        'label' => 'Base Content',
+        'settings' => serialize($settings),
+        'weight' => -9999999
+      ))
+      ->execute();
+  }
+  // Next check if the hidden region is in the database. If not, add it.
+  $te_base = db_select('tripal_panels', 'tp')
+    ->fields('tp')
+    ->condition('name', 'te_hidden')
+    ->execute()
+    ->fetchObject();
+  if (!$te_base) {
+    $settings = array(
+      'message' => 'Place field here to hide them from display.',
+    );
+    db_insert('tripal_panels')
+      ->fields(array(
+        'bundle_id' => $bundle->id,
+        'name' => 'te_hidden',
+        'label' => 'Hidden',
+        'settings' => serialize($settings),
+        'weight' => 9999999
+      ))
+      ->execute();
   }
 }
 
+/**
+ * Returns the region to which a row on the Field UI page belongs.
+ *
+ * @param $row
+ *   The current row that is being rendered in the Field UI page.
+ */
+function tripal_entities_field_ui_row_region($row) {
+  $default_panel =  'te_base';
+  $panel = '';
+
+  $field_name = $row['#parents'][1];
+  $field = field_info_field($field_name);
+
+  // First get the panel
+  if (!$panel) {
+    $panel = $default_panel;
+  }
+
+  // See if there is a record in the tripal_panel_fields for this field.
+
+  return $default_panel;
+
+}
+/**
+ * Validates the field UI form to ensure proper panel assignments.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function tripal_entities_field_ui_validate($form, &$form_state) {
+
+}
+/**
+ * Responds to a submit from the field UI form for saving panel assignments.
+ *
+ * @param $form
+ * @param $form_state
+ */
+function tripal_entities_field_ui_submit($form, &$form_state) {
+
+}

+ 3 - 3
tripal_fields/includes/tripal_fields.field_storage.inc

@@ -44,9 +44,9 @@ function tripal_fields_field_storage_write($entity_type, $entity, $op, $fields)
     $record = array(
       'entity_id' => $entity->id,
       'record_id' => $record_id,
-      'data_table' => $tablename,
-      'type_table' => $tablename, // TODO: this must be fixed.
-      'field' => $type_field,
+      'data_table' => $entity->chado_table,
+      'type_table' => $entity->chado_table,
+      'field' => $entity->chado_field,
     );
     $success = drupal_write_record('chado_entity', $record);
     if (!$success) {