Skip to content

Commit

Permalink
Merge pull request #96 from UofS-Pulse-Binfo/phenotype-image
Browse files Browse the repository at this point in the history
Phenotype image
  • Loading branch information
laceysanderson committed Sep 27, 2019
2 parents 0e26750 + 277bfb4 commit f9a708b
Show file tree
Hide file tree
Showing 16 changed files with 1,096 additions and 1 deletion.
55 changes: 54 additions & 1 deletion includes/TripalFields/analyzedphenotypes.fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ function analyzedphenotypes_bundle_fields_info($entity_type, $bundle) {
'type' => 'field_chado_storage',
),
);

// Phenotype Image
tripal_insert_cvterm(array(
'id' => 'NCIT:C48179',
'name' => 'Image',
'cv_name' => 'NCIT',
'definition' => 'Phenotype Image.',
));
$field_name = 'ncit__image';
$field_type = 'ncit__image';
$fields[$field_name] = array(
'field_name' => $field_name,
'type' => $field_type,
'cardinality' => 1,
'locked' => FALSE,
'storage' => array(
'type' => 'field_chado_storage',
),
);
}

// IN GERMPLASM PAGE ONLY:
Expand Down Expand Up @@ -388,7 +407,6 @@ function analyzedphenotypes_bundle_instances_info($entity_type, $bundle) {
),
);


// Germplasm Phenotype Search Field.
$field_name = 'ncit__data';
$field_type = 'ncit__data';
Expand Down Expand Up @@ -484,6 +502,41 @@ function analyzedphenotypes_bundle_instances_info($entity_type, $bundle) {
),
),
);

// Phenotype Image.
$field_name = 'ncit__image';
$field_type = 'ncit__image';
$instances[$field_name] = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle->name,
'label' => 'Phenotype Image',
'description' => 'Phenotype Image.',
'required' => FALSE,
'settings' => array(
'term_vocabulary' => 'NCIT',
'term_name' => 'Image',
'term_accession' => 'C48179',
'auto_attach' => FALSE,
'chado_table' => $bundle->data_table,
'chado_column' => 'cvterm_id',
'base_table' => $bundle->data_table,
),
'widget' => array(
'type' => 'ncit__image_widget',
'settings' => array(),
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'ncit__image_formatter',
'settings' => array(
'image_stack' => 'Left to Right',
'image_position' => 'Top to Bottom',
),
),
),
);
}

// IN GERMPLASM PAGE ONLY:
Expand Down
122 changes: 122 additions & 0 deletions includes/TripalFields/ncit__c48179/ncit__c48179.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* @class
* Purpose: Provide quick browse functionality for entity pages
*
* Data: No data.
* Assumptions:
*/
class ncit__c48179 extends ChadoField {
// --------------------------------------------------------------------------
// EDITABLE STATIC CONSTANTS
//
// The following constants SHOULD be set for each descendant class. They are
// used by the static functions to provide information to Drupal about
// the field and it's default widget and formatter.
// --------------------------------------------------------------------------
// The default label for this field.
public static $default_label = 'Phenotype Image';
// The default description for this field.
public static $default_description = 'Phenotype Image';
// The default widget for this field.
public static $default_widget = 'ncit__c48179_widget';
// The default formatter for this field.
public static $default_formatter = 'ncit__c48179_formatter';
// The module that manages this field.
public static $module = 'analyzedphenotypes';

// A list of global settings. These can be accessed within the
// globalSettingsForm. When the globalSettingsForm is submitted then
// Drupal will automatically change these settings for all fields.
// Once instances exist for a field type then these settings cannot be
// changed.
public static $default_settings = array(
'storage' => 'tripal_no_storage',
// It is expected that all fields set a 'value' in the load() function.
// In many cases, the value may be an associative array of key/value pairs.
// In order for Tripal to provide context for all data, the keys should
// be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
// function that are supported by the query() function should be
// listed here.
'browseable_keys' => array(),
);

// Provide a list of instance specific settings. These can be access within
// the instanceSettingsForm. When the instanceSettingsForm is submitted
// then Drupal with automatically change these settings for the instance.
// It is recommended to put settings at the instance level whenever possible.
// If you override this variable in a child class be sure to replicate the
// term_name, term_vocab, term_accession and term_fixed keys as these are
// required for all TripalFields.
public static $default_instance_settings = array(
// The short name for the vocabulary (e.g. schema, SO, GO, PATO, etc.).
'term_vocabulary' => 'ncit',
// The name of the term.
'term_name' => 'Image',
// The unique ID (i.e. accession) of the term.
'term_accession' => 'c48179',
// Set to TRUE if the site admin is not allowed to change the term
// type, otherwise the admin can change the term mapped to a field.
'term_fixed' => FALSE,
// Indicates if this field should be automatically attached to display
// or web services or if this field should be loaded separately. This
// is convenient for speed. Fields that are slow should for loading
// should have auto_attach set to FALSE so tha their values can be
// attached asynchronously.
'auto_attach' => FALSE,
// The table where the options for this specific field are stored.
// This can be one of trpfancy_browse_options or trpfancy_browse_options_per_entity
// based on admin configuration. Default: trpfancy_browse_options.
'option_storage' => '',
// A list of browser types this field intends to provide.
'browser_types' => '',
);

// A boolean specifying that users should not be allowed to create
// fields and instances of this field type through the UI. Such
// fields can only be created programmatically with field_create_field()
// and field_create_instance().
public static $no_ui = FALSE;
// A boolean specifying that the field will not contain any data. This
// should exclude the field from web services or downloads. An example
// could be a quick browse field that appears on the page that redirects
// the user but otherwise provides no data.
public static $no_data = TRUE;

/**
* Loads the field values from the underlying data store.
*
* @param $entity
*
* @return
* An array of the following format:
* $entity->{$field_name}['und'][0]['value'] = $value;
* where:
* - $entity is the entity object to which this field is attached.
* - $field_name is the name of this field
* - 'und' is the language code (in this case 'und' == undefined)
* - 0 is the cardinality. Increment by 1 when more than one item is
* available.
* - 'value' is the key indicating the value of this field. It should
* always be set. The value of the 'value' key will be the contents
* used for web services and for downloadable content. The value
* should be of the follow format types: 1) A single value (text,
* numeric, etc.) 2) An array of key value pair. 3) If multiple entries
* then cardinality should incremented and format types 1 and 2 should
* be used for each item.
* The array may contain as many other keys at the same level as 'value'
* but those keys are for internal field use and are not considered the
* value of the field.
*
*
*/
public function load($entity) {
$trait_photo = ap_download_cvtermphoto($entity->chado_record_id);
if ($trait_photo) {
$field_name = $this->instance['field_name'];
$entity->{$field_name}['und'][0]['value']['ncit:c48179'] = $trait_photo;
}
}


}
188 changes: 188 additions & 0 deletions includes/TripalFields/ncit__c48179/ncit__c48179_formatter.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php
/**
* @class
* Purpose: Provide a quick search on entity pages which submits/redirects to a full search.
*
* Display: A simple textfield search form.
* Configuration:
* - path to the full search.
* - the URL token (query parameter) the value applies to.
* - help text.
* - textfield placeholder.
* - search button text.
* - autocomplete path.
*/
class ncit__c48179_formatter extends TripalFieldFormatter {
// The default label for this field.
public static $default_label = 'Phenotype Image';

// The list of field types for which this formatter is appropriate.
public static $field_types = array('ncit__c48179');

// The list of default settings for this formatter.
// Due to the different charts available and the dependance upon the data,
// it is too difficult to make a good default for the title and legend.
// As such I am making a horrible one in the hopes of making admin change it ;-p.
public static $default_settings = array(
'image_stack' => 'Left to Right',
'image_position' => 'Top to Bottom',
);


/**
* Provides the display for a field
*
* This function corresponds to the hook_field_formatter_view()
* function of the Drupal Field API.
*
* This function provides the display for a field when it is viewed on
* the web page. The content returned by the formatter should only include
* what is present in the $items[$delta]['values] array. This way, the
* contents that are displayed on the page, via webservices and downloaded
* into a CSV file will always be identical. The view need not show all
* of the data in the 'values' array.
*
* @param $element
* @param $entity_type
* @param $entity
* @param $langcode
* @param $items
* @param $display
*
* @return
* An element array compatible with that returned by the
* hook_field_formatter_view() function.
*/
public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
drupal_add_css(drupal_get_path('module', 'analyzedphenotypes') . '/includes/TripalFields/ncit__c48179/theme/style_phenotype_image.css');
drupal_add_js(drupal_get_path('module', 'analyzedphenotypes') . '/includes/TripalFields/ncit__c48179/theme/script.js');

$trait_photo = array_values($items[0]['value']['ncit:c48179']);

// Refer to this ID for CSS styling.
$id = 'ap-field-phenotype-image-wrapper';

$element_photo = array();
foreach($trait_photo as $img) {
if ($img && file_exists($img['uri'])) {
$scale = ap_scale_photo($img['height'], $img['width'], 300);

$element_photo[] = theme('image', array(
'path' => $img['uri'],
'attributes' => array('style' => array('width' => 0)),
));
}
}

// From the settings: apply class.
$class_stack = 'ap-' . str_replace(' ', '-', strtolower($display['settings']['image_stack']));
$class_position = 'ap-' . str_replace(' ', '-', strtolower($display['settings']['image_position']));


// Render Number of Experimients.
$element[0] = array(
'#type' => 'markup',
'#markup' => '<div id="' . $id . '" class="' . $class_stack . ' ' . $class_position . '">'
. implode('', $element_photo) . '</div><div class="ap-clear-float">&nbsp;</div>'
);

return $element;
}


/**
* Provides a form for the 'Field Settings' of an instance of this field.
*
* This function corresponds to the hook_field_instance_settings_form()
* function of the Drupal Field API.
*
* Validation of the instance settings form is not supported by Drupal, but
* the TripalField class does provide a mechanism for supporting validation.
* To allow for validation of your setting form you must call the parent
* in your child class:
*
* @code
* $element = parent::instanceSettingsForm();
* @endcode
*
* Please note, the form generated with this function does not easily
* support AJAX calls in the same way that other Drupal forms do. If you
* need to use AJAX you must manually alter the $form in your ajax call.
* The typical way to handle updating the form via an AJAX call is to make
* the changes in the form function itself but that doesn't work here.
*/
public function settingsForm($view_mode, $form, &$form_state) {
// Illustrations showing configurations available.
$src = drupal_get_path('module', 'analyzedphenotypes') . '/includes/TripalFields/ncit__c48179/theme/configuration.gif';
$element['configuration'] = array(
'#type' => 'markup',
'#markup' => theme('image', array(
'path' => $src,
'height' => 150,
'width' => 480,
))
);

$display = $this->instance['display'][$view_mode];
$settings = $display['settings'];

// FIELD: Stacking order.
$element['image_stack'] = array(
'#type' => 'radios',
'#title' => 'Image stacking',
'#options' => array('Left to Right' => 'Left to Right', 'Top to Bottom' => 'Top to Bottom'),
'#default_value' => array_key_exists('image_stack', $settings) ? $settings['image_stack'] : 'Left to Right',
);

// FIELD: Float position.
$element['image_position'] = array(
'#type' => 'radios',
'#title' => 'Image position',
'#options' => array('Float Left' => 'Float Left', 'Float Right' => 'Float Right', 'None' => 'None - Use field order/weight'),
'#default_value' => array_key_exists('image_position', $settings) ? $settings['image_position'] : 'Float Right',
);


return $element;
}

/**
* Provides a summary of the formatter settings.
*
* This function corresponds to the hook_field_formatter_settings_summary()
* function of the Drupal Field API.
*
* On the 'Manage Display' page of the content type administration page,
* fields are allowed to provide a settings form. This settings form can
* be used to allow the site admin to define how the field should be
* formatted. The settings are then available for the formatter()
* function of this class. This function provides a text-based description
* of the settings for the site developer to see. It appears on the manage
* display page inline with the field. A field must always return a
* value in this function if the settings form gear button is to appear.
*
* See the hook_field_formatter_settings_summary() function for more
* information.
*
* @param $field
* @param $instance
* @param $view_mode
*
* @return string
* A string that provides a very brief summary of the field settings
* to the user.
*
*/
public function settingsSummary($view_mode) {
$display = $this->instance['display'][$view_mode];
$settings = $display['settings'];

$summary = t('Stack: @image_stack<br>Position: @image_position',
array(
'@image_stack' => $settings['image_stack'] ?? '',
'@image_position' => $settings['image_position'] ?? '')
);

return $summary;
}
}
Loading

0 comments on commit f9a708b

Please sign in to comment.