From 1906b279f5ca6e4c672fa0986b0f65deed926cc7 Mon Sep 17 00:00:00 2001 From: Malladi Date: Mon, 6 Nov 2023 14:47:08 -0600 Subject: [PATCH 1/3] Tripal 4 tripal collections YAML file and Chado Unit fields from : t4_10_19_2023_tv4g1-issue1414-uo__unit --- ...l.tripalfield_collection.default_chado.yml | 4 +- .../ChadoUnitFormatterDefault.php | 37 ++++++ .../Field/FieldType/ChadoUnitDefault.php | 114 ++++++++++++++++++ .../FieldWidget/ChadoUnitWidgetDefault.php | 68 +++++++++++ 4 files changed, 221 insertions(+), 2 deletions(-) create mode 100755 tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php create mode 100755 tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php create mode 100755 tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php diff --git a/tripal_chado/config/install/tripal.tripalfield_collection.default_chado.yml b/tripal_chado/config/install/tripal.tripalfield_collection.default_chado.yml index e471ba4b0..9a29a9669 100644 --- a/tripal_chado/config/install/tripal.tripalfield_collection.default_chado.yml +++ b/tripal_chado/config/install/tripal.tripalfield_collection.default_chado.yml @@ -1390,7 +1390,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 @@ -2089,7 +2089,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 diff --git a/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php b/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php new file mode 100755 index 000000000..cd9e9836f --- /dev/null +++ b/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php @@ -0,0 +1,37 @@ + $item) { + $elements[$delta] = [ + // "#markup" => $item->get('unittype_id')->getString() + "#markup" => $item->get('cv_name')->getString() + ]; + } + + return $elements; + } +} \ No newline at end of file diff --git a/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php b/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php new file mode 100755 index 000000000..eb7c00dd2 --- /dev/null +++ b/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php @@ -0,0 +1,114 @@ +getTargetEntityTypeId(); + + $chado = \Drupal::service('tripal_chado.database'); + $schema = $chado->schema(); + + $ftmap_def = $schema->getTableDef('featuremap', ['format' => 'Drupal']); + $ftmap_name_len = $ftmap_def['fields']['name']['size']; + + $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, + ]) + ]; + } + $base_schema_def = $schema->getTableDef($base_table, ['format' => 'Drupal']); + $base_pkey_col = $base_schema_def['primary key']; + + $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' ) ; + $ftmap_name_term = $mapping->getColumnTermId('featuremap', 'name'); + $cv_name_term = $mapping->getColumnTermId('cvterm', 'name'); + $cvterm_id_term = $mapping->getColumnTermId( 'cvterm', 'cvterm_id' ) ; + + $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 ); + } + +} \ No newline at end of file diff --git a/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php b/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php new file mode 100755 index 000000000..3706b2219 --- /dev/null +++ b/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php @@ -0,0 +1,68 @@ +select( 'cvterm', 'cvt' ); + $query->addField( 'cvt', 'name', 'cvt_name' ); + $query->addField( 'cvt', 'cvterm_id', 'unittype_id' ); + $query->condition('cvt.cv_id', 23, '='); + $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, + '#placeholder' => $this->getSetting('placeholder'), + '#empty_option' => '-- Select --', + ]; + + return $elements; + } + +} \ No newline at end of file From 54e00617752701892255b017952da22dff1a370a Mon Sep 17 00:00:00 2001 From: Malladi Date: Tue, 7 Nov 2023 13:02:14 -0600 Subject: [PATCH 2/3] Modified unit field code on change request in PR #1680 : tv4g1-issue1414-uo__unit --- .../ChadoUnitFormatterDefault.php | 5 ++--- .../Field/FieldType/ChadoUnitDefault.php | 7 ------ .../FieldWidget/ChadoUnitWidgetDefault.php | 22 ++++++++----------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php b/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php index cd9e9836f..de2b527b6 100755 --- a/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php +++ b/tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php @@ -10,8 +10,8 @@ * * @FieldFormatter( * id = "chado_unit_formatter_default", - * label = @Translation("Chado Unit Formatter"), - * description = @Translation("The default unit widget which allows curators to enter unit on the Gene Map content edit page."), + * label = @Translation("Chado unit type formatter"), + * description = @Translation("A Chado unit type formatter."), * field_types = { * "chado_unit_default" * } @@ -27,7 +27,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; foreach($items as $delta => $item) { $elements[$delta] = [ - // "#markup" => $item->get('unittype_id')->getString() "#markup" => $item->get('cv_name')->getString() ]; } diff --git a/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php b/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php index eb7c00dd2..05cd9cf36 100755 --- a/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php +++ b/tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php @@ -57,9 +57,6 @@ public static function tripalTypes($field_definition) { $chado = \Drupal::service('tripal_chado.database'); $schema = $chado->schema(); - $ftmap_def = $schema->getTableDef('featuremap', ['format' => 'Drupal']); - $ftmap_name_len = $ftmap_def['fields']['name']['size']; - $cvterm_def = $schema->getTableDef('cvterm', ['format' => 'Drupal']); $cv_name_len = $cvterm_def['fields']['name']['size']; @@ -75,16 +72,12 @@ public static function tripalTypes($field_definition) { ]) ]; } - $base_schema_def = $schema->getTableDef($base_table, ['format' => 'Drupal']); - $base_pkey_col = $base_schema_def['primary key']; $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' ) ; - $ftmap_name_term = $mapping->getColumnTermId('featuremap', 'name'); $cv_name_term = $mapping->getColumnTermId('cvterm', 'name'); - $cvterm_id_term = $mapping->getColumnTermId( 'cvterm', 'cvterm_id' ) ; $properties = []; diff --git a/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php b/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php index 3706b2219..d1f7292b3 100755 --- a/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php +++ b/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php @@ -25,21 +25,18 @@ class ChadoUnitWidgetDefault extends ChadoWidgetBase { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $unit_vals = []; - $unittype_ids = []; - $unittype_names = []; $chado = \Drupal::service('tripal_chado.database'); - if ( 1 == 1 ) { - $unit_vals = []; - $query = $chado->select( 'cvterm', 'cvt' ); - $query->addField( 'cvt', 'name', 'cvt_name' ); - $query->addField( 'cvt', 'cvterm_id', 'unittype_id' ); - $query->condition('cvt.cv_id', 23, '='); - $results = $query->execute(); - while ( $unit_rec = $results->fetchObject() ) { - $unit_vals[$unit_rec->unittype_id] = $unit_rec->cvt_name; - } + $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() ; @@ -58,7 +55,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#description' => t("Select map unit from dropdown."), '#options' => $unit_vals, '#default_value' => $unittype_id, - '#placeholder' => $this->getSetting('placeholder'), '#empty_option' => '-- Select --', ]; From 9c5c52516221dfed01bd6a10a95cba992c2545c5 Mon Sep 17 00:00:00 2001 From: Malladi Date: Tue, 7 Nov 2023 16:28:08 -0600 Subject: [PATCH 3/3] Modified widget on change request : tv4g1-issue1414-uo__unit --- .../Field/FieldWidget/ChadoUnitWidgetDefault.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php b/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php index d1f7292b3..b3e15b4ca 100755 --- a/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php +++ b/tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php @@ -60,5 +60,18 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen 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; + } } \ No newline at end of file