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.
Tripal 4 files for sequence_coordinates-field: tv4g1-issue1414-data__…
…sequence_coordinates
- Loading branch information
Malladi
committed
Nov 28, 2023
1 parent
3d1f4be
commit e884fe8
Showing
3 changed files
with
196 additions
and
0 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
75 changes: 75 additions & 0 deletions
75
tripal_chado/src/Plugin/Field/FieldFormatter/ChadoSequenceCoordinatesFormatterDefault.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,75 @@ | ||
<?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_sequence_coordinates_formatter_default", | ||
* label = @Translation("Chado sequence coordinates default formatter"), | ||
* description = @Translation("The default sequence coordinates formatter allows curators to view sequence coordinates (min, max, strand and phase) of the feature."), | ||
* field_types = { | ||
* "chado_sequence_coordinates_default" | ||
* } | ||
* ) | ||
*/ | ||
class ChadoSequenceCoordinatesFormatterDefault extends ChadoFormatterBase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function viewElements(FieldItemListInterface $items, $langcode) | ||
{ | ||
$elements = []; | ||
$reference_term = 'data:3002'; | ||
|
||
$locations = []; | ||
|
||
foreach ($items as $item) { | ||
|
||
if (!empty($item['value'])) { | ||
$loc_rec = ''; | ||
$feature_ref_val = $item['value'][$reference_term]; | ||
|
||
$fmin_val = $item->get('fmin')->getString(); | ||
if (!empty($fmin_val)) { | ||
$loc_rec .= $item['value'][$feature_ref_val] . ':' .$fmin_val . ".."; | ||
} | ||
$fmax_val = $item->get('fmax')->getString(); | ||
if (!empty($fmax_val)) { | ||
$loc_rec .= $item['value'][$fmax_val].'; '; | ||
} | ||
$phase_val = $item->get('phase')->getString(); | ||
if (!empty($phase_val)) { | ||
$loc_rec .= $item['value'][$phase_val].'; '; | ||
} | ||
$strand_term = $item->get('strand')->getString(); | ||
if (!empty($strand_val)) { | ||
$strand_symb = match( $item['value'][$strand_term] ) { | ||
-1 => '-', | ||
1 => '+', | ||
default => '', | ||
}; | ||
$loc_rec .= $strand_symb; | ||
} | ||
$locations[] = $loc_rec; | ||
} | ||
} | ||
if ( !$locations ) { | ||
$content = 'This feature is not located on any sequence.'; | ||
} | ||
else { | ||
$content = implode('<br />', $locations); | ||
} | ||
// The cardinality of this field is always 1, so only create element for $delta of zero. | ||
$elements[0] = [ | ||
'#type' => 'markup', | ||
'#markup' => $content, | ||
]; | ||
return $elements; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
tripal_chado/src/Plugin/Field/FieldFormatter/ChadoSequenceCoordinatesFormatterTable.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,94 @@ | ||
<?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_sequence_coordinates_formatter_table", | ||
* label = @Translation("Chado sequence coordinates table formatter"), | ||
* description = @Translation("The sequence coordinates widget allows curators to manually enter feature sequence coordinates information on the content edit page."), | ||
* field_types = { | ||
* "chado_sequence_coordinates_default" | ||
* } | ||
* ) | ||
*/ | ||
class ChadoSequenceCoordinatesFormatterTable extends ChadoFormatterBase { | ||
|
||
public static $default_settings = [ | ||
'caption' => 'This @content_type has the following sequence coordinates:', | ||
'expand_strand' => TRUE, | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function viewElements(FieldItemListInterface $items, $langcode) { | ||
$elements = []; | ||
$reference_term = 'data:3002'; | ||
|
||
// Get the settings and set defaults. | ||
$settings = $display['settings']; | ||
foreach ($this::$default_settings as $key => $value) { | ||
if (!isset($settings[$key])) { | ||
$settings[$key] = $value; | ||
} | ||
} | ||
|
||
// Replace tokens in the caption. | ||
$settings['caption'] = t($settings['caption'], | ||
['@content_type' => $entity->rdfs__type['und'][0]['value']]); | ||
|
||
// For each location, add it to the table. | ||
$header = ['Name', 'Location', 'Strand', 'Phase']; | ||
|
||
$locations = []; | ||
|
||
foreach ($items as $item) { | ||
|
||
if (!empty($item['value'])) { | ||
$fmin_term = $item->get('fmin')->getString(); | ||
$fmax_term = $item->get('fmax')->getString(); | ||
$strand_term = $item->get('strand')->getString(); | ||
$phase_term = $item->get('phase')->getString(); | ||
|
||
$strand_val = $item['value'][$strand_term]; | ||
if ($settings['expand_strand']) { | ||
$strand_symb = match( $strand_val ) { | ||
-1 => '-', | ||
1 => '+', | ||
default => '<span style="color:#B3B3B3">unknown</span>', | ||
}; | ||
} | ||
} | ||
|
||
$locations[] = [ | ||
$item['value'][$reference_term], | ||
$item['value'][$fmin_term] . '..' . $fmax = $item['value'][$fmax_term], | ||
$strand_symb, $item['value'][$phase_term], | ||
]; | ||
} | ||
|
||
if ( !$locations ) { | ||
$content = 'This feature is not located on any sequence.'; | ||
} | ||
else { | ||
$content = $locations; | ||
} | ||
|
||
// The cardinality of this field is always 1, so only create element for $delta of zero. | ||
$elements[0] = [ | ||
'#type' => 'markup', | ||
'#theme' => 'table', | ||
'#header' => $header, | ||
'#rows' => $content, | ||
'#caption' => $settings['caption'], | ||
]; | ||
|
||
return $elements; | ||
} | ||
} |