123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- /**
- * Implement hook_entity_info().
- *
- * See the following for documentaiton of this type setup for Entities:
- *
- * 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
- */
- function tripal_entity_info() {
- $entities = array();
- // The TripalVocab entity is meant to house vocabularies. It is these
- // vocabs that are used by the TripalTerm entities. The storage backend
- // is responsible for setting the values of this entity.
- //
- $entities['TripalVocab'] = array(
- // A human readable label to identify our entity.
- 'label' => 'Controlled Vocabulary',
- 'plural label' => 'Controlled Vocabularies',
- // The entity class and controller class extend the classes provided by the
- // Entity API.
- 'entity class' => 'TripalVocab',
- 'controller class' => 'TripalVocabController',
- // Adds Views integration for this entity.
- 'views controller class' => 'TripalVocabViewsController',
- // The table for this entity defined in hook_schema()
- 'base table' => 'tripal_vocab',
- // If fieldable == FALSE, we can't attach fields.
- 'fieldable' => TRUE,
- // entity_keys tells the controller what database fields are used for key
- // functions. It is not required if we don't have bundles or revisions.
- // Here we do not support a revision, so that entity key is omitted.
- 'entity keys' => array (
- 'id' => 'id',
- ),
- // Callback function for access to this entity.
- 'access callback' => 'tripal_entity_access',
- // FALSE disables caching. Caching functionality is handled by Drupal core.
- 'static cache' => TRUE,
- // Caching of fields
- 'field cache' => TRUE,
- // This entity doesn't support bundles.
- 'bundles' => array (),
- 'view modes' => array (
- 'full' => array (
- 'label' => t ('Full content'),
- 'custom settings' => FALSE
- ),
- 'teaser' => array (
- 'label' => t ('Teaser'),
- 'custom settings' => TRUE
- ),
- ),
- );
- //
- // The TripalTerm entity is meant to house vocabulary terms. It is these
- // terms that are used by the TripalEntity entities. The storage backend
- // is responsible for setting the values of this entity.
- //
- $entities['TripalTerm'] = array(
- // A human readable label to identify our entity.
- 'label' => 'Controlled Vocabulary Term',
- 'plural label' => 'Controlled Vocabulary Terms',
- // The entity class and controller class extend the classes provided by the
- // Entity API.
- 'entity class' => 'TripalTerm',
- 'controller class' => 'TripalTermController',
- // Adds Views integration for this entity.
- 'views controller class' => 'TripalTermViewsController',
- // The table for this entity defined in hook_schema()
- 'base table' => 'tripal_term',
- // If fieldable == FALSE, we can't attach fields.
- 'fieldable' => TRUE,
- // entity_keys tells the controller what database fields are used for key
- // functions. It is not required if we don't have bundles or revisions.
- // Here we do not support a revision, so that entity key is omitted.
- 'entity keys' => array (
- 'id' => 'id',
- ),
- // Callback function for access to this entity.
- 'access callback' => 'tripal_entity_access',
- // FALSE disables caching. Caching functionality is handled by Drupal core.
- 'static cache' => FALSE,
- // This entity doesn't support bundles.
- 'bundles' => array (),
- 'view modes' => array (
- 'full' => array (
- 'label' => t ('Full content'),
- 'custom settings' => FALSE
- ),
- 'teaser' => array (
- 'label' => t ('Teaser'),
- 'custom settings' => TRUE
- ),
- ),
- );
- //
- // The TripalEntity is used for all data. It links data from a storage
- // back-end to a TripalTerm entity.
- //
- $entities['TripalEntity'] = array (
- // A human readable label to identify our entity.
- 'label' => 'Tripal Content',
- 'plural label' => 'Tripal Content',
- // The entity class and controller class extend the classes provided by the
- // Entity API.
- 'entity class' => 'TripalEntity',
- 'controller class' => 'TripalEntityController',
- // Adds Views integration for this entity.
- 'views controller class' => 'TripalEntityViewsController',
- // The table for this entity defined in hook_schema()
- 'base table' => 'tripal_entity',
- // Returns the uri elements of an entity.
- 'uri callback' => 'tripal_vocbulary_term_uri',
- // IF fieldable == FALSE, we can't attach fields.
- 'fieldable' => TRUE,
- // entity_keys tells the controller what database fields are used for key
- // functions. It is not required if we don't have bundles or revisions.
- // Here we do not support a revision, so that entity key is omitted.
- 'entity keys' => array (
- 'id' => 'id',
- 'bundle' => 'bundle'
- ),
- 'bundle keys' => array (
- 'bundle' => 'name'
- ),
- // Callback function for access to this entity.
- 'access callback' => 'tripal_entity_access',
- // FALSE disables caching. Caching functionality is handled by Drupal core.
- 'static cache' => TRUE,
- // Caching of fields
- 'field cache' => TRUE,
- // Bundles are added dynamically below.
- 'bundles' => array (),
- 'label callback' => 'tripal_entity_label',
- // The information below is used by the TripalEntityUIController
- // (which extends the EntityDefaultUIController). The admin_ui
- // key here is mean to appear on the 'Find Content' page of the
- // administrative menu.
- 'admin ui' => array (
- 'path' => 'admin/content/bio_data',
- 'controller class' => 'TripalEntityUIController',
- 'menu wildcard' => '%TripalEntity',
- 'file' => 'includes/TripalEntityUIController.inc'
- ),
- 'view modes' => array (
- 'full' => array (
- 'label' => t ('Full content'),
- 'custom settings' => FALSE
- ),
- 'teaser' => array (
- 'label' => t ('Teaser'),
- 'custom settings' => TRUE
- )
- )
- );
- //
- // The TripalBundle entity is used manage the bundle types. The 'bundle of'
- // attribute links this to the TripalEntity and allows the UI provided
- // by the entity module to work for each TripalEntity bundle.
- //
- $entities['TripalBundle'] = array (
- 'label' => 'Tripal Content Type',
- 'entity class' => 'TripalBundle',
- 'controller class' => 'TripalBundleController',
- 'base table' => 'tripal_bundle',
- 'fieldable' => FALSE,
- 'bundle of' => 'TripalEntity',
- 'exportable' => FALSE,
- 'entity keys' => array (
- 'id' => 'id',
- 'name' => 'name',
- 'label' => 'label'
- ),
- 'access callback' => 'tripal_bundle_access',
- 'module' => 'tripal',
- // Enable the entity API's admin UI.
- 'admin ui' => array (
- 'path' => 'admin/structure/bio_data',
- 'controller class' => 'TripalBundleUIController',
- 'file' => 'includes/TripalBundleUIController.inc',
- 'menu wildcard' => '%TripalBundle',
- )
- );
- return $entities;
- }
- /**
- * Implements hook_entities_info_alter().
- *
- * Add in the bundles (entity types) to the TripalEntity entity.
- */
- function tripal_entity_info_alter(&$entity_info){
- if (array_key_exists('TripalEntity', $entity_info)) {
- // Dynamically add in the bundles. Bundles are alternative groups of fields
- // or configuration associated with an entity type .We want to dynamically
- // add the bundles to the entity.
- $bundles = db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->execute();
- while ($bundle = $bundles->fetchObject()) {
- $bundle_name = $bundle->name;
- $term_id = $bundle->term_id;
- $term = entity_load('TripalTerm', array('id' => $term_id));
- $term = reset($term);
- $label = preg_replace('/_/', ' ', ucwords($term->name));
- $entity_info['TripalEntity']['bundles'][$bundle_name] = array (
- 'label' => $label,
- 'admin' => array (
- 'path' => 'admin/structure/bio_data/manage/%TripalBundle',
- 'real path' => 'admin/structure/bio_data/manage/' . $bundle_name,
- 'bundle argument' => 4,
- 'access arguments' => array (
- 'administer tripal data types'
- )
- )
- );
- }
- }
- }
- /**
- * Checks access permissions for a given entity.
- */
- function tripal_entity_access($entity) {
- // TODO: need to implement this function.
- return TRUE;
- }
- function tripal_form_tripal_entity_form_alter(&$form, &$form_state, $form_id) {
- }
- /**
- * Implements hook_entity_view.
- *
- * Here we want to overwite unattached fields with a div box that will be
- * recognized by JavaScript that will then use AJAX to load the field.
- *
- * We also want to add tooltips to each field that describes to the user
- * the meaning of the field.
- *
- * The tripal_ajax_attach_field() function is called by an AJAX call to
- * retrieve the field.
- */
- function tripal_entity_view($entity, $type, $view_mode, $langcode) {
- if ($type == 'TripalEntity') {
- foreach (element_children($entity->content) as $child_name) {
- // Initailize the prefix and suffix for this field.
- if (!array_key_exists('#prefix', $entity->content[$child_name])) {
- $entity->content[$child_name]['#prefix'] = '';
- }
- if (!array_key_exists('#suffix', $entity->content[$child_name])) {
- $entity->content[$child_name]['#suffix'] = '';
- }
- // Surround the field with a <div> box for AJAX loading if this
- // field is unattached. this will allow JS code to automatically load
- // the field.
- $child = $entity->content[$child_name];
- if (array_key_exists('#items', $child) and
- array_key_exists('0', $child['#items']) and
- is_array($child['#items'][0]) and
- array_key_exists('#unattached', $child['#items'][0])
- and $child['#items'][0]['#unattached'] === TRUE) {
- $entity->content[$child_name]['#prefix'] .= '<div id="tripal-entity-' . $entity->id . '--' . $child_name . '" class="tripal-entity-unattached">';
- $entity->content[$child_name]['#suffix'] .= '</div>';
- }
- // Add a tooltip to the field for the term.
- $field = field_info_field($child_name);
- if (array_key_exists('settings', $field) and
- array_key_exists('semantic_web', $field['settings'])) {
- $matches = array();
- if (preg_match('/^(.+):(.+)$/', $field['settings']['semantic_web'], $matches)) {
- $vocabulary = $matches[1];
- $accession = $matches[2];
- $term = tripal_get_term_details($vocabulary, $accession);
- if ($term) {
- $tooltip_img = url(drupal_get_path('module', 'tripal') . '/theme/images/info.gif');
- $tooltip_text = $term['definition'] ? $term['definition'] : $term['name'];
- $entity->content[$child_name]['#prefix'] .= '' .
- '<div class="tripal-tooltip">' .
- '<img src="' . $tooltip_img . '">' .
- '<div class="tripal-tooltiptext">' . $tooltip_text . '</div>' .
- '</div>';
- }
- }
- }
- }
- }
- }
- /**
- * Responds to an AJAX call for populating a field.
- *
- * @param $id
- * The ID of the HTML div box where the field is housed. The ID contains the
- * entity ID and field name.
- */
- function tripal_ajax_attach_field($id) {
- $matches = array();
- if (preg_match('/^tripal-entity-(\d+)--(.+)$/', $id, $matches)) {
- $entity_id = $matches[1];
- $field_name = $matches[2];
- $field = field_info_fields($field_name);
- $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, array($field['id']));
- reset($result);
- $entity = $result[$entity_id];
- $content = drupal_render(field_view_field('TripalEntity', $entity, $field_name));
- return drupal_json_output(array(
- 'id' => $id,
- 'content' => $content
- ));
- }
- }
|