Skip to content

Commit

Permalink
Merge pull request tripal#1861 from tripal/tv4g1-issue1859-sequence-c…
Browse files Browse the repository at this point in the history
…oordinates-field

Fix the sequence coordinates field
  • Loading branch information
laceysanderson authored Apr 28, 2024
2 parents 85d19d9 + 39f4c5e commit 193e72a
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Drupal\tripal_chado\Plugin\Field\FieldType;

use Drupal\tripal_chado\TripalField\ChadoFieldItemBase;
use Drupal\tripal_chado\TripalStorage\ChadoBoolStoragePropertyType;
use Drupal\tripal_chado\TripalStorage\ChadoIntStoragePropertyType;
use Drupal\tripal_chado\TripalStorage\ChadoTextStoragePropertyType;
use Drupal\tripal\Entity\TripalEntityType;

/**
Expand Down Expand Up @@ -60,14 +62,10 @@ public static function tripalTypes($field_definition) {
$field_settings = $field_definition->getSetting('storage_plugin_settings');
$base_table = $field_settings['base_table'];

// If we don't have a base table then we're not ready to specify the
// properties for this field.
if (!$base_table) {
$record_id_term = 'data:3002';
return [
new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
])
];
return;
}

// Get the property terms by using the Chado table columns they map to.
Expand All @@ -77,10 +75,16 @@ public static function tripalTypes($field_definition) {
$record_id_term = $mapping->getColumnTermId( 'feature', 'feature_id' );
$ft_uniqname_term = $mapping->getColumnTermId( 'feature', 'name' );

$srcfeature_id_term = $mapping->getColumnTermId('featureloc', 'srcfeature_id');
$fmin_term = $mapping->getColumnTermId('featureloc', 'fmin');
$is_fmin_partial_term = $mapping->getColumnTermId('featureloc', 'is_fmin_partial');
$fmax_term = $mapping->getColumnTermId('featureloc', 'fmax');
$is_fmax_partial_term = $mapping->getColumnTermId('featureloc', 'is_fmax_partial');
$strand_term = $mapping->getColumnTermId('featureloc', 'strand');
$phase_term = $mapping->getColumnTermId('featureloc', 'phase');
$residue_info_term = $mapping->getColumnTermId('featureloc', 'residue_info');
$locgroup_term = $mapping->getColumnTermId('featureloc', 'locgroup');
$rank_term = $mapping->getColumnTermId('featureloc', 'rank');

// Get property terms using Chado table columns they map to. Return the properties for this field.
$properties = [];
Expand All @@ -103,28 +107,61 @@ public static function tripalTypes($field_definition) {
'path' => 'feature.feature_id>featureloc.feature_id',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'uniquename', $ft_uniqname_term, [
$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'srcfeature_id', $srcfeature_id_term, [
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;srcfeature_id',
]);

$properties[] = new ChadoTextStoragePropertyType($entity_type_id, self::$id, 'uniquename', $ft_uniqname_term, [
'action' => 'read_value',
'path' => 'feature.feature_id>featureloc.feature_id;featureloc.srcfeature_id>feature.feature_id;uniquename',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'fmin', $fmin_term, [
'action' => 'read_value',
'path' => 'feature.feature_id>featureloc.feature_id;fmin',
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;fmin',
]);

$properties[] = new ChadoBoolStoragePropertyType($entity_type_id, self::$id, 'is_fmin_partial', $is_fmin_partial_term, [
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;is_fmin_partial',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'fmax', $fmax_term, [
'action' => 'read_value',
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;fmax',
]);

$properties[] = new ChadoBoolStoragePropertyType($entity_type_id, self::$id, 'is_fmax_partial', $is_fmax_partial_term, [
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;is_fmax_partial',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'strand', $strand_term, [
'action' => 'read_value',
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;strand',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'phase', $phase_term, [
'action' => 'read_value',
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;phase',
]);

$properties[] = new ChadoTextStoragePropertyType($entity_type_id, self::$id, 'residue_info', $residue_info_term, [
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;residue_info',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'locgroup', $locgroup_term, [
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;locgroup',
]);

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'rank', $rank_term, [
'action' => 'store',
'path' => 'feature.feature_id>featureloc.feature_id;rank',
]);

return($properties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,58 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#type' => 'value',
'#default_value' => $item_vals['record_id'] ?? 0,
];
$elements['featureloc_id'] = [
'#type' => 'value',
'#default_value' => $item_vals['featureloc_id'] ?? 0,
];
$elements['fkey'] = [
'#type' => 'value',
'#default_value' => $item_vals['fkey'] ?? 0,
];
$elements['srcfeature_id'] = [
'#type' => 'value',
'#default_value' => $item_vals['srcfeature_id'] ?? 0,
];
$elements['uniquename'] = [
'#type' => 'value',
'#default_value' => $item_vals['uniquename'] ?? '',
];
$elements['fmin'] = [
'#type' => 'value',
'#default_value' => $item_vals['fmin'] ?? 0,
];
$elements['is_fmin_partial'] = [
'#type' => 'value',
'#default_value' => $item_vals['is_fmin_partial'] ?? 0,
];
$elements['fmax'] = [
'#type' => 'value',
'#default_value' => $item_vals['fmax'] ?? 0,
];
$elements['is_fmax_partial'] = [
'#type' => 'value',
'#default_value' => $item_vals['is_fmax_partial'] ?? 0,
];
$elements['strand'] = [
'#type' => 'value',
'#default_value' => $item_vals['strand'] ?? 0,
];
$elements['phase'] = [
'#type' => 'value',
'#default_value' => $item_vals['phase'] ?? 0,
];
$elements['residue_info'] = [
'#type' => 'value',
'#default_value' => $item_vals['residue_info'] ?? '',
];
$elements['locgroup'] = [
'#type' => 'value',
'#default_value' => $item_vals['locgroup'] ?? 0,
];
$elements['rank'] = [
'#type' => 'value',
'#default_value' => $item_vals['rank'] ?? 0,
];

return $elements;
}
Expand Down
63 changes: 63 additions & 0 deletions tripal_chado/tripal_chado.install
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,66 @@ function tripal_chado_update_10403() {
throw new UpdateException('Could not add entity id to fields: ' . $e->getMessage());
}
}

/**
* Adds or updates several properties added to the
* chado_sequence_coordinates_default field in PR 1861
*/
function tripal_chado_update_10404() {
try {
$messenger = \Drupal::messenger();

// This update only modifies a single field type.
$upgradable_types = ['chado_sequence_coordinates_default'];
$upgradable_properties = ['uniquename', 'fkey', 'srcfeature_id',
'fmin', 'is_fmin_partial', 'fmax', 'is_fmax_partial',
'strand', 'phase', 'residue_info', 'locgroup', 'rank'];
$entity_type = 'tripal_entity';

$manager = \Drupal::entityDefinitionUpdateManager();
$schema = \Drupal::database()->schema();
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$field_map = \Drupal::service('entity_field.manager')->getFieldMap();
$fields = $field_map[$entity_type];
$n_added = 0;
$n_updated = 0;

foreach ($fields as $field_name => $field_def) {
$id = $field_def['type'];
if (in_array($id, $upgradable_types, TRUE)) {
$field_storage_definition = $manager->getFieldStorageDefinition($field_name, 'tripal_entity');
if ($field_storage_definition) {
$field_schema = $field_storage_definition->getSchema();
$table_mapping = $storage->getTableMapping([$field_name => $field_storage_definition]);
$table_names = $table_mapping->getDedicatedTableNames();
$columns = $table_mapping->getColumnNames($field_name);

foreach ($table_names as $table_name) {
$table_exists = $schema->tableExists($table_name);
if ($table_exists) {
$messenger->addMessage(t("Adding and updating properties for Drupal table \"@table_name\", field \"@field_name\"",
['@table_name' => $table_name, '@field_name' => $field_name]));
foreach ($upgradable_properties as $property) {
$field_exists = $schema->fieldExists($table_name, $columns[$property]);
if ($field_exists) {
$schema->changeField($table_name, $columns[$property], $columns[$property], $field_schema['columns'][$property]);
$n_updated++;
}
else {
$schema->addField($table_name, $columns[$property], $field_schema['columns'][$property]);
$n_added++;
}
}
$manager->updateFieldStorageDefinition($field_storage_definition);
}
}
}
}
}
$messenger->addMessage(t("Added @n_added properties, updated @n_updated properties",
['@n_added' => $n_added, '@n_updated' => $n_updated]));
}
catch (\Exception $e) {
throw new UpdateException('Could not update property: ' . $e->getMessage());
}
}

0 comments on commit 193e72a

Please sign in to comment.