Browse Source

Making progress on entities, fields and field storage

Stephen Ficklin 9 years ago
parent
commit
98ec396e6d
1 changed files with 333 additions and 76 deletions
  1. 333 76
      tripal_entities/tripal_entities.module

+ 333 - 76
tripal_entities/tripal_entities.module

@@ -1,12 +1,13 @@
 <?php
 // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
+// http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
 /**
  * Implement hook_entity_info().
  */
 function tripal_entities_entity_info() {
   $entities = array();
 
-  $entities['cvterm'] = array(
+  $entities['chado_data'] = array(
     'label' => t('Vocabulary Term'),
     'uri callback' => 'tripal_entities_vocbulary_term_uri',
     'plural label' => t('Vocabulary Terms'),
@@ -14,7 +15,8 @@ function tripal_entities_entity_info() {
     'controller class' => 'TrpVocabularyTermController',
     'fieldable' => TRUE,
     'entity keys' => array(
-      'id' => 'internal_id'
+      'id' => 'entity_id',
+      'bundle' => 'bundle',
     ),
     // Bundles are defined by the model types below
     'bundles' => array(),
@@ -36,146 +38,401 @@ function tripal_entities_entity_info() {
   return $entities;
 }
 
-function tripal_entities_load($pid = NULL, $reset = FALSE){
-  $pids = (isset ($pid) ? array($pid) : array());
-  $cvterm = trp_vocabulary_term_load_multiple($pids, $reset);
-  return $cvterm ? reset ($cvterm) : FALSE;
+function tripal_entities_load($entity_id = NULL, $reset = FALSE){
+  $ids = (isset ($entity_id) ? array($entity_id) : array());
+  $chado_data = tripal_entities_load_multiple($entity_ids, $reset);
+  return $chado_data ? reset ($chado_data) : FALSE;
 }
 
-function tripal_entities_load_multiple($pids = array(), $conditions = array(), $reset = FALSE){
-  return entity_load('cvterm', $pids, $conditions, $reset);
+function tripal_entities_load_multiple($entity_ids = array(), $conditions = array(), $reset = FALSE){
+  return entity_load('chado_data', $entity_ids, $conditions, $reset);
 }
 
 function tripal_entities_menu() {
-  $items['admin/structure/cvterm/manage'] = array(
+  $items['admin/structure/chado_data/manage'] = array(
     'title' => 'Term Admin',
-    'description' => 'Manage cvterm structure',
-    'page callback' => 'cvterm_info',
-    'access arguments' => array('administer cvterms'),
+    'description' => 'Manage chado_data structure',
+    'page callback' => 'chado_data_info',
+    'access arguments' => array('administer chado_datas'),
   );
-  $items['cvterm/%cvterm'] = array(
-    'title callback' => 'cvterm_page_title',
+  $items['chado_data/%chado_data'] = array(
+    'title callback' => 'chado_data_page_title',
     'title arguments' => array(1),
-    'page callback' => 'cvterm_page_view',
+    'page callback' => 'chado_data_page_view',
     'page arguments' => array(1),
-    'access arguments' => array('view cvterms'),
+    'access arguments' => array('view chado_datas'),
     'type' => MENU_CALLBACK,
   );
 
-
-  $items['data/gene/add'] = array(
-    'title' => 'Add a gene',
-    'page callback' => 'cvterm_add',
-    'access arguments' => array('create cvterm'),
+  $items['data/add'] = array(
+    'title' => 'Add new data',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_entities_add'),
+    'access arguments' => array('create chado_data'),
   );
   return $items;
 }
 
 function tripal_entities_permission(){
   return array(
-    'administer cvterms' =>  array(
-      'title' => t('Administer cvterms'),
+    'administer chado_datas' =>  array(
+      'title' => t('Administer chado_datas'),
       'restrict access' => TRUE,
     ),
     'view postsits' => array(
-      'title' => t('View cvterms'),
+      'title' => t('View chado_datas'),
     )
   );
 }
 
 
-function cvterm_info() {
+function chado_data_info() {
   return ('Welcome to the administration page for your CV Terms!');
 }
 
-function cvterm_page_title($cvterm){
-  return $cvterm->pid;
+function chado_data_page_title($chado_data){
+  return $chado_data->entity_id;
 }
 
-function cvterm_page_view($cvterm, $view_mode = 'full'){
-  $cvterm->content = array();
+function chado_data_page_view($chado_data, $view_mode = 'full'){
+  $chado_data->content = array();
 
   // Build fields content.
-  field_attach_prepare_view('cvterm', array($cvterm->pid => $cvterm), $view_mode);
-  entity_prepare_view('cvterm', array($cvterm->pid => $cvterm));
-  $cvterm->content += field_attach_view('cvterm', $cvterm, $view_mode);
+  field_attach_prepare_view('chado_data', array($chado_data->entity_id => $chado_data), $view_mode);
+  entity_prepare_view('chado_data', array($chado_data->entity_id => $chado_data));
+  $chado_data->content += field_attach_view('chado_data', $chado_data, $view_mode);
 
-  return $cvterm->content;
+  return $chado_data->content;
 }
+/**
+ *
+ */
+function tripal_entities_vocbulary_term_uri($entity) {
+  return array(
+    'path' => 'chado_data/' . $entity->entity_id,
+  );
+}
+
+/**
+ *
+ */
+function tripal_entities_add_form($form, &$form_state) {
+  // Set the defaults.
+  $cv_id = NULL;
+  $term_name = NULL;
+  if (array_key_exists('values', $form_state)) {
+    $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
+    $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
+  }
+
+  // Let the user select the vocabulary and chado_data but only if they haven't
+  // already selected a chado_data.
+  $cvs = tripal_get_cv_select_options();
+  if (!$term_name) {
+    $form['cv_id'] = array(
+      '#type' => 'select',
+      '#title' => t('Vocabulary'),
+      '#options' => $cvs,
+      '#required' => TRUE,
+      '#description' => t('Select a vocabulary that contains the term for the type of data you want to add.'),
+      '#default_value' => $cv_id,
+      '#ajax' => array(
+        'callback' => "tripal_entities_add_form_ajax_callback",
+        'wrapper' => 'tripal_entities_add_form',
+        'effect' => 'fade',
+        'method' => 'replace'
+      )
+    );
+  }
+
+  // If we have a CV ID then we want to provide an autocomplete field
+  if ($cv_id and !$term_name) {
+    $form['cvterm_select']['term_name'] = array(
+      '#title'       => t('Record Type'),
+      '#type'        => 'textfield',
+      '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."),
+      '#required'    => TRUE,
+      '#default_value' => $term_name,
+      '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
+    );
+
+    $form['cvterm_select']['select_button'] = array(
+      '#type' => 'submit',
+      '#value' => t('Use this term'),
+      '#name' => 'select_cvterm',
+    );
+  }
 
 
-function tripal_entities_field_extra_fields() {
-  $return = array();
-  $return['cvterm']['cvterm'] = array(
-    'form' => array(
-      'note' => array(
-        'label' => t('Note'),
-        'description' => t('Term Note'),
+  // Once the CV term is selected then provide the other fields.
+  if ($term_name) {
+    // Get the cvterm that matches
+    $values = array(
+      'cv_id' => $cv_id,
+      'name' => $term_name
+    );
+    $cvterm = tripal_get_cvterm($values);
+
+    $form['cv_id'] = array(
+      '#type'  => 'hidden',
+      '#value' => $cv_id,
+    );
+    $form['cvterm_id'] = array(
+      '#type'  => 'hidden',
+      '#value' => $cvterm->cvterm_id,
+    );
+    $form['bundle'] = array(
+      '#type'  => 'hidden',
+      '#value' => $term_name,
+    );
+    $form['cv_name'] = array(
+      '#type' => 'item',
+      '#title' => 'Vocabulary',
+      '#markup' => $cvterm->cv_id->name,
+      '#weight' => -100,
+    );
+    $form['term_name_shown'] = array(
+      '#type' => 'item',
+      '#title' => 'Record Type',
+      '#markup' => $cvterm->name,
+      '#weight' => -100,
+    );
+
+/*     // Drupal field types and settings:
+    // https://www.drupal.org/node/1879542
+    $field = array(
+      'field_name' => 'feature_name',
+      'type' => 'text',
+      'cardinality' => 1,
+      'storage' => array(
+        'type' => 'tripal_entities_storage'
+      ),
+    );
+    field_create_field($field);
+    $field_instance = array(
+      'field_name' => 'feature_name',
+      'label' => 'Name',
+      'widget' => array(
+        'type' => 'text_textfield'
+      ),
+      'entity_type' => 'cvterm',
+      'required' => 'true',
+      'settings' => array(
+        'max_length' => 255
+      ),
+      'bundle' => $cvterm->name,
+    );
+    field_create_instance($field_instance); */
+
+
+
+    // Create the object for this entity instance. The entity instance type
+    // is always the name of the term.
+    $entity = new stdClass();
+    $entity->entity_id = NULL;
+    $entity->bundle = $cvterm->name;
+    $form['#parents'] = array('attached');
+    field_attach_form('chado_data', $entity, $form, $form_state);
+
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => t('Add a new ' . $cvterm->name),
+      '#name' => 'add_data',
+    );
+  }
+
+  $form['#prefix'] = '<div id="tripal_entities_add_form">';
+  $form['#suffix'] = '</div>';
+  return $form;
+}
+
+/**
+ * Implements hook_field_info().
+ */
+function tripal_entities_field_info() {
+  $fields = array(
+    'chado_text' => array(
+      'label' => t('Chado Text'),
+      'description' => t('This field is used for mapping data into a Chado varchar field.'),
+      'default_widget' => 'tripal_entities_text_widget',
+      'default_formatter' => 'tripal_entities_text_formatter',
+      'storage' => array(
+        'type' => 'tripal_entities_storage',
+        'module' => 'tripal_entities',
+        'active' => TRUE
       ),
     ),
   );
-
-  return $return;
+  return $fields;
 }
-function tripal_entities_vocbulary_term_uri($cvterm) {
+
+/**
+ * Implements hook_field_widget_info().
+ */
+function tripal_entities_field_widget_info() {
   return array(
-    'path' => 'cvterm/' . $cvterm->id,
+    'tripal_entities_text_widget' => array(
+      'label' => t('Chado Text Widget'),
+      'field types' => array('chado_text')
+    ),
   );
 }
-
-function cvterm_add() {
-  $cvterm = (object) array (
-    'pid' => '',
-    'type' => 'cvterm',
-    'note' => '',
+/**
+ * Implements hook_field_formatter_info().
+ */
+function tripal_entities_field_formatter_info() {
+  return array(
+    'tripal_entities_text_formatter' => array(
+      'label' => t('Chado Text Formatter'),
+      'field types' => array('chado_text')
+    ),
   );
+}
 
-  return drupal_get_form('cvterm_add_form', $cvterm);
+/**
+ * Implements hook_field_widget_form().
+ */
+function tripal_entities_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
+  switch ($instance['widget']['type']) {
+    case 'tripal_entities_text_widget':
+      $element['tripal_entities_text_widget'] = array(
+        '#type' => 'textfield',
+        '#title' => $element['#title'],
+        '#description' => $element['#description'],
+        '#default_value' => '',
+        '#required' => $element['#required'],
+        '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
+        '#delta' => $delta,
+      );
+      break;
+  }
+  return $element;
 }
 
+/**
+ * Implements hook_field_is_empty().
+ */
+function tripal_entities_field_is_empty($item, $field) {
+  // If there's nothing in $item['textf'].
+  if (empty($item['tripal_entities_text_widget'])) {
+    // Then we return 1, i.e. we say to Drupal that everything is fine).
+    return TRUE;
+    // And if it's empty, nothing will be displayed.
+  }
+}
 
-function cvterm_add_form($form, &$form_state, $cvterm) {
-  $form['note'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Note'),
-    '#required' => TRUE,
+/**
+ * Implements hook_field_storage_info().
+ */
+function tripal_entities_field_storage_info() {
+  return array(
+    'tripal_entities_storage' => array(
+      'label' => t('Chado storage'),
+      'description' => t('Stores fields in the local Chado database.'),
+      'settings' => array(),
+    ),
   );
+}
 
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save'),
-  );
 
-  field_attach_form('cvterm', $cvterm, $form, $form_state);
+/**
+ * Implements hook_field_storage_write().
+ */
+function tripal_entities_storage_field_storage_write($entity_type, $entity, $op, $fields) {
+  dpm($entity);
+  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
+  if (!isset($vid)) {
+    $vid = $id;
+  }
+  foreach ($fields as $field_id) {
+    switch ($op) {
+      case FIELD_STORAGE_INSERT:
+        break;
+      case FIELD_STORAGE_UPDATE:
+        break;
+    }
+  }
+}
+/**
+ * Implements hook_field_storage_load().
+ *
+ * Responsible for loading the fields from the Chado database and adding
+ * their values to the entity.
+ */
+function tripal_entities_storage_field_storage_load($entity_type, $entities, $age, $fields, $options) {
 
+}
+/**
+ * An Ajax callback for the tripal_entities_add_form.
+ */
+function tripal_entities_add_form_ajax_callback($form, $form_state) {
+  // return the form so Drupal can update the content on the page
   return $form;
+
 }
-function cvterm_add_form_validate($form, &$form_state) {
-  $cvterm_submission = (object) $form_state['values'];
-  field_attach_form_validate('cvterm', $cvterm_submission, $form, $form_state);
+/**
+ * Implements hook_validate() for the tripal_entities_add_form.
+ */
+function tripal_entities_add_form_validate($form, &$form_state) {
+   if ($form_state['clicked_button']['#name'] == 'add_data') {
+     $chado_data = (object) $form_state['values'];
+     field_attach_form_validate('chado_data', $chado_data, $form, $form_state);
+   }
 }
 
-function cvterm_add_form_submit($form, &$form_state) {
-  $cvterm_submission = (object) $form_state['values'];
-  field_attach_submit('cvterm', $cvterm_submission, $form, $form_state);
-  $cvterm = cvterm_save($cvterm_submission);
-  $form_state['redirect'] = "cvterm/$cvterm->pid";
+/**
+ * Implements hook_submit() for the tripal_entities_add_form.
+ *
+ */
+function tripal_entities_add_form_submit($form, &$form_state) {
+  if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
+    // don't do anything, we just need to know what the term name is.
+    $form_state['rebuild'] = TRUE;
+  }
+  else {
+    dpm($form_state['values']);
+
+    // Build and entity instance object.
+    $entity = new stdClass();
+    $entity->entity_id = NULL;
+    $entity->bundle = $form_state['values']['bundle'];
+    $entity->cvterm_id = $form_state['values']['cvterm_id'];
+    $entity->cv_id = $form_state['values']['cv_id'];
+
+
+
+    // Perform necessary operations on field data submitted by a form.
+    // Currently, this accounts for drag-and-drop reordering of field values,
+    // and filtering of empty values.
+    field_attach_submit('chado_data', $entity, $form, $form_state);
+dpm($entity);
+    // Save the Entity.
+    $entity = entity_get_controller('chado_data')->save($entity);
+//   $form_state['redirect'] = "chado_data/$chado_data->entity_id";
+
+  }
 }
+/**
+ * Implements hook_theme().
+ */
+function tripal_entities_theme($existing, $type, $theme, $path) {
+  return array(
 
-function cvterm_save(&$cvterm) {
-  return entity_get_controller('cvterm')->save($cvterm);
+  );
 }
+
 /**
  *
  *
  */
 class TrpVocabularyTermController extends DrupalDefaultEntityController {
 
-  public function save($cvterm) {
-//     drupal_write_record('cvterm', $cvterm);
-//     field_attach_insert('cvterm', $cvterm);
-//     module_invoke_all('entity_insert', 'cvterm', $cvterm);
-    return $cvterm;
+  public function save($entity) {
+    dpm($entity);
+//     drupal_write_record('chado_data', $chado_data);
+//     field_attach_insert('chado_data', $chado_data);
+//     module_invoke_all('entity_insert', 'chado_data', $chado_data);
+    return $entity;
   }
 
 }