Skip to content

Commit

Permalink
Merge pull request #1388 from tripal/tv4g1-292-sequenceField
Browse files Browse the repository at this point in the history
Tripal 4 Field: Sequence Residues
  • Loading branch information
spficklin committed Mar 28, 2023
2 parents f2d9410 + d102742 commit 687343a
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#tripal-chado-sequence-format {
font-family: Consolas, monaco, monospace;
font-size: 12px;
height: 195px; /* height set to bisect characters in last row as visible scroll feedback */
max-width: 400px;
overflow: scroll;
line-height: normal !important;
border: 1px solid #DDDDDD;
padding: 10px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldFormatter;

use Drupal\tripal\TripalField\TripalFormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tripal_chado\TripalField\ChadoFormatterBase;

/**
* Plugin implementation of Default Tripal field formatter for sequence data
*
* @FieldFormatter(
* id = "chado_sequence_formatter_default",
* label = @Translation("Chado Sequence Residues Display"),
* description = @Translation("Displays chado sequence residues from the feature table on the page."),
* field_types = {
* "chado_sequence_default"
* }
* )
*/
class ChadoSequenceFormatterDefault extends ChadoFormatterBase {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$elements['#attached']['library'][] = 'tripal_chado/tripal_chado.field.ChadoSequenceFormatterDefault';

foreach($items as $delta => $item) {
$residues = $item->get('residues')->getString();
if (!empty($residues)) {
$elements[$delta] = [
"#markup" => "<pre id='tripal-chado-sequence-format'>" . wordwrap($residues,50,'<br>',TRUE) . "</pre>",
];
}
}

return $elements;
}
}
82 changes: 82 additions & 0 deletions tripal_chado/src/Plugin/Field/FieldType/ChadoSequenceDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldType;

use Drupal\tripal_chado\TripalField\ChadoFieldItemBase;
use Drupal\tripal_chado\TripalStorage\ChadoIntStoragePropertyType;
use Drupal\tripal_chado\TripalStorage\ChadoTextStoragePropertyType;
use Drupal\core\Form\FormStateInterface;
use Drupal\core\Field\FieldDefinitionInterface;

/**
* Plugin implementation of Default Tripal field for sequence data.
*
* @FieldType(
* id = "chado_sequence_default",
* label = @Translation("Chado Sequence Residues"),
* description = @Translation("Manages sequence residues for content types storing data in the chado feature table."),
* default_widget = "chado_sequence_widget_default",
* default_formatter = "chado_sequence_formatter_default"
* )
*/
class ChadoSequenceDefault extends ChadoFieldItemBase {

public static $id = "chado_sequence_default";

/**
* {@inheritdoc}
*/
public static function mainPropertyName() {
return 'residues';
}

/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
$settings = parent::defaultFieldSettings();
$settings['termIdSpace'] = 'data';
$settings['termAccession'] = '2044';
return $settings;
}

/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
$settings['storage_plugin_settings']['base_table'] = 'feature';
return $settings;
}

/**
* {@inheritdoc}
*/
public static function tripalTypes($field_definition) {
$entity_type_id = $field_definition->getTargetEntityTypeId();

// Get the base table columns needed for this field.
$settings = $field_definition->getSetting('storage_plugin_settings');

// Get the property terms by using the Chado table columns they map to.
$storage = \Drupal::entityTypeManager()->getStorage('chado_term_mapping');
$mapping = $storage->load('core_mapping');
$record_id_term = 'SIO:000729';
$residues_term = $mapping->getColumnTermId('feature', 'residues');

// Return the properties for this field.
$properties = [];
$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
'chado_table' => 'feature',
'chado_column' => 'feature_id'
]);
$properties[] = new ChadoTextStoragePropertyType($entity_type_id, self::$id, 'residues', $residues_term, [
'action' => 'store',
'chado_column' => 'residues',
'chado_table' => 'feature'
]);
return $properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldWidget;

use Drupal\tripal\Plugin\Field\FieldWidget\TripalTextTypeWidget;
use Drupal\tripal\TripalField\TripalWidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tripal_chado\TripalField\ChadoWidgetBase;

/**
* Plugin implementation of default Chado Sequence widget.
*
* @FieldWidget(
* id = "chado_sequence_widget_default",
* label = @Translation("Chado Sequence Residues Widget"),
* description = @Translation("The default chado sequence widget which allows curators to manually enter sequence residues on the content edit page."),
* field_types = {
* "chado_sequence_default"
* }
* )
*/
class ChadoSequenceWidgetDefault extends ChadoWidgetBase {

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

$item_vals = $items[$delta]->getValue();
$elements = [];
$elements['record_id'] = [
'#type' => 'value',
'#default_value' => $item_vals['record_id'] ?? 0,
];

$elements['residues'] = $element + [
'#type' => 'textarea',
'#default_value' => $item_vals['residues'] ?? '',
'#placeholder' => $this->getSetting('placeholder'),
'#attributes' => ['class' => ['js-text-full', 'text-full']],
];

return $elements;
}


/**
* {@inheritDoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

// Remove any empty values that aren't mapped to a record id.
foreach ($values as $val_key => $value) {
$values[$val_key]['residues'] = preg_replace('/\s/', '', $value['residues']);
}
return $values;
}

}
5 changes: 5 additions & 0 deletions tripal_chado/tripal_chado.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ tripal_chado.chado_table:
css/chado_table.css: {}
js:
js/chado_table.js: {}

tripal_chado.field.ChadoSequenceFormatterDefault:
css:
component:
src/Plugin/Field/FieldFormatter/ChadoSequenceFormatterDefault.css: {}

0 comments on commit 687343a

Please sign in to comment.