forked from tripal/tripal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tripal#1680 from pdtouch/tv4g1-issue1414-uo__unit
Tripal 4 Chado Unit field tv4g1-issue1414-uo__unit
- Loading branch information
Showing
4 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tripal_chado/src/Plugin/Field/FieldFormatter/ChadoUnitFormatterDefault.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
tripal_chado/src/Plugin/Field/FieldType/ChadoUnitDefault.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
tripal_chado/src/Plugin/Field/FieldWidget/ChadoUnitWidgetDefault.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |