Skip to content

Commit

Permalink
Merge pull request tripal#1680 from pdtouch/tv4g1-issue1414-uo__unit
Browse files Browse the repository at this point in the history
Tripal 4 Chado Unit field tv4g1-issue1414-uo__unit
  • Loading branch information
dsenalik authored Nov 7, 2023
2 parents 8e77724 + 23caeb8 commit 3d1f4be
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ fields:
- name: physical_map_unit_type
content_type: physical_map
label: Unit Type
type: chado_additional_type_default
type: chado_unit_default
description: A unit of measurement is a standardized quantity of a physical quality.
cardinality: 1
required: false
Expand Down Expand Up @@ -2222,7 +2222,7 @@ fields:
- name: genetic_map_unit_type
content_type: genetic_map
label: Unit Type
type: chado_additional_type_default
type: chado_unit_default
description: A unit of measurement is a standardized quantity of a physical quality.
cardinality: 1
required: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\tripal_chado\TripalField\ChadoFormatterBase;

/**
* Plugin implementation of default Tripal string type formatter.
*
* @FieldFormatter(
* id = "chado_unit_formatter_default",
* label = @Translation("Chado unit type formatter"),
* description = @Translation("A Chado unit type formatter."),
* field_types = {
* "chado_unit_default"
* }
* )
*/
class ChadoUnitFormatterDefault extends ChadoFormatterBase {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {

$elements = [];
foreach($items as $delta => $item) {
$elements[$delta] = [
"#markup" => $item->get('cv_name')->getString()
];
}

return $elements;
}
}
107 changes: 107 additions & 0 deletions tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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\ChadoVarCharStoragePropertyType;

/**
* Plugin implementation of Default Tripal field for unit of measurement.
*
* @FieldType(
* id = "chado_unit_default",
* label = @Translation("Chado unit of measurement"),
* description = @Translation("Provide unit of measurement of content, for example, Genetic Map."),
* default_widget = "chado_unit_widget_default",
* default_formatter = "chado_unit_formatter_default"
* )
*/

class ChadoUnitDefault extends ChadoFieldItemBase {

public static $id = "chado_unit_default";

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

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

/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
return $settings;
}

/**
* {@inheritdoc}
*/
public static function tripalTypes($field_definition) {

$entity_type_id = $field_definition->getTargetEntityTypeId();

$chado = \Drupal::service('tripal_chado.database');
$schema = $chado->schema();

$cvterm_def = $schema->getTableDef('cvterm', ['format' => 'Drupal']);
$cv_name_len = $cvterm_def['fields']['name']['size'];

$field_settings = $field_definition->getSetting('storage_plugin_settings');
$base_table = $field_settings['base_table'];

if (!$base_table) {
$record_id_term = 'data:1280';
return [
new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
])
];
}

$storage = \Drupal::entityTypeManager()->getStorage('chado_term_mapping');
$mapping = $storage->load('core_mapping');
$record_id_term = $mapping->getColumnTermId('featuremap', 'featuremap_id');
$unittype_id_term = $mapping->getColumnTermId( 'featuremap', 'unittype_id' ) ;
$cv_name_term = $mapping->getColumnTermId('cvterm', 'name');

$properties = [];

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
'chado_table' => 'featuremap',
'chado_column' => 'featuremap_id',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'unittype_id', $unittype_id_term, [
'action' => 'store',
'chado_table' => 'featuremap',
'chado_column' => 'unittype_id',
]);

$properties[] = new ChadoVarCharStoragePropertyType($entity_type_id, self::$id, 'cv_name', $cv_name_term, $cv_name_len, [
'action' => 'read_value',
'path' => 'featuremap.unittype_id>cvterm.cvterm_id',
'chado_column' => 'name',
'as' => 'cv_name'
]);

return( $properties );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tripal_chado\TripalField\ChadoWidgetBase;

/**
* Plugin implementation of default Chado Data Source widget.
*
* @FieldWidget(
* id = "chado_unit_widget_default",
* label = @Translation("Chado Unit Widget Default"),
* description = @Translation("The default unit widget which allows curators to enter unit on the Gene Map content edit page."),
* field_types = {
* "chado_unit_default"
* }
* )
*/
class ChadoUnitWidgetDefault extends ChadoWidgetBase {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

$unit_vals = [];

$chado = \Drupal::service('tripal_chado.database');

$query = $chado->select( 'cvterm', 'cvt' );
$query->leftJoin ('cv', 'cv', 'cvt.cv_id = cv.cv_id ');
$query->addField( 'cvt', 'name', 'cvt_name' );
$query->addField( 'cvt', 'cvterm_id', 'unittype_id' );
$query->condition('cv.name', 'featuremap_units', '=');
$results = $query->execute();

while ( $unit_rec = $results->fetchObject() ) {
$unit_vals[$unit_rec->unittype_id] = $unit_rec->cvt_name;
}

$item_vals = $items[$delta]->getValue() ;
$record_id = $item_vals['record_id'] ?? 0 ;
$unittype_id = $item_vals['unittype_id'] ?? 0 ;

$elements = [];

$elements['record_id'] = [
'#default_value' => $record_id,
'#type' => 'value',
];

$elements['unittype_id'] = $element + [
'#type' => 'select',
'#description' => t("Select map unit from dropdown."),
'#options' => $unit_vals,
'#default_value' => $unittype_id,
'#empty_option' => '-- Select --',
];

return $elements;
}

/**
* {@inheritDoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// Remove any empty values that don't have a unit type
foreach ($values as $delta => $item) {
if ($item['unittype_id'] == '') {
unset($values[$delta]);
}
}
return $values;
}

}

0 comments on commit 3d1f4be

Please sign in to comment.