Skip to content

Commit

Permalink
Merge pull request #1504 from pdtouch/tv4g1-issue1414-local-data-sour…
Browse files Browse the repository at this point in the history
…ce-field

Tv4g1 issue1414 local data source field
  • Loading branch information
dsenalik committed May 26, 2023
2 parents 327e367 + 0d52630 commit e9a2ea0
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldFormatter;

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

/**
* Plugin implementation of default Tripal string type formatter.
*
* @FieldFormatter(
* id = "chado_source_data_formatter_default",
* label = @Translation("Chado Source Data Formatter"),
* description = @Translation("The default source data widget which allows curators to manually enter analysis source data information on the content edit page."),
* field_types = {
* "chado_source_data_default"
* }
* )
*/
class ChadoSourceDataFormatterDefault extends ChadoFormatterBase {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$content = '';

foreach ($items as $delta => $item) {
$sourcename_val = $item->get('sourcename')->getString();
if (!empty($sourcename_val)) {
$content .= "<dt>Source Name:</dt><dd>" . $sourcename_val . "</dd>";
}
$sourceversion_val = $item->get('sourceversion')->getString();
if (!empty($sourceversion_val)) {
$content .= "<dt>Source Version:</dt><dd>" . $sourceversion_val . "</dd>";
}
$sourceuri_val = $item->get('sourceuri')->getString();
if (!empty($sourceuri_val)) {
$url = $sourceuri_val;
if (preg_match('|://|', $sourceuri_val)) {
$url = Link::fromTextAndUrl($sourceuri_val, Url::fromUri($sourceuri_val, []))->toString();
}
$content .= "<dt>Source URI:</dt><dd>" . $url . "</dd>";
}
}

if ($content) {
$content = "<dl class=\"tripal-dl\">" . $content . "</dl>";
} else {
$content = 'The data source is not provided.';
}

// The cardinality of this field is always 1, so only create element for $delta of zero.
$elements[0] = [
'#type' => 'markup',
'#markup' => $content,
];

return $elements;
}
}
100 changes: 100 additions & 0 deletions tripal_chado/src/Plugin/Field/FieldType/ChadoSourceDataDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldType;

use Drupal\Core\Form\FormStateInterface;
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 sequence data.
*
* @FieldType(
* id = "chado_source_data_default",
* label = @Translation("Chado Data Source"),
* description = @Translation("The source and version of data used for this analysis"),
* default_widget = "chado_source_data_widget_default",
* default_formatter = "chado_source_data_formatter_default",
* cardinality = 1,
* )
*/
class ChadoSourceDataDefault extends ChadoFieldItemBase {

public static $id = "chado_source_data_default";

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

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

/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
$settings['storage_plugin_settings']['base_table'] = 'analysis';
return $settings;
}

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

// Create variables for easy access to settings.
$entity_type_id = $field_definition->getTargetEntityTypeId();

// Get the property terms by using the Chado table columns they map to.
$storage = \Drupal::entityTypeManager()->getStorage('chado_term_mapping');
$mapping = $storage->load('core_mapping');

$record_id_term = $mapping->getColumnTermId('analysis', 'analysis_id');

$src_uri_term = $mapping->getColumnTermId('analysis', 'sourceuri');
$src_name_term = $mapping->getColumnTermId('analysis', 'sourcename');
$src_vers_term = $mapping->getColumnTermId('analysis', 'sourceversion');

// Get property terms using Chado table columns they map to. Return the properties for this field.
$properties = [];

$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
'chado_table' => 'analysis',
'chado_column' => 'analysis_id',
]);
$properties[] = new ChadoVarCharStoragePropertyType($entity_type_id, self::$id, 'sourceuri', $src_uri_term, 100, [
'action' => 'store',
'chado_table' => 'analysis',
'chado_column' => 'sourceuri',
]);
$properties[] = new ChadoVarCharStoragePropertyType($entity_type_id, self::$id, 'sourcename', $src_name_term, 200, [
'action' => 'store',
'chado_table' => 'analysis',
'chado_column' => 'sourcename',
'delete_if_empty' => TRUE,
'empty_value' => '',
]);
$properties[] = new ChadoVarCharStoragePropertyType($entity_type_id, self::$id, 'sourceversion', $src_vers_term, 100, [
'action' => 'store',
'chado_table' => 'analysis',
'chado_column' => 'sourceversion',
]);

return ($properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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_source_data_widget_default",
* label = @Translation("Chado Data Source Widget Default"),
* description = @Translation("The default source data widget which allows curators to manually enter analysis source data information on the content edit page."),
* field_types = {
* "chado_source_data_default"
* }
* )
*/
class ChadoSourceDataWidgetDefault extends ChadoWidgetBase {

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

$item_vals = $items[$delta]->getValue();

$elements = [];

$elements['datasourcegroup'] = [
'#type' => 'details',
'#title' => t("Data Source"),
'#description' => t('The source where data was obtained for this analysis.'),
'#open' => TRUE, // Controls the HTML5 'open' attribute. Defaults to FALSE.
];

$elements['record_id'] = [
'#type' => 'value',
'#default_value' => $item_vals['record_id'] ?? 0,
];
$elements['datasourcegroup']['sourcename'] = [
'#title' => t("Name"),
'#type' => 'textfield',
'#description' => t('The name of the source where data was obtained for this analysis.'),
'#default_value' => $item_vals['sourcename'] ?? '',
];
$elements['datasourcegroup']['sourceversion'] = [
'#title' => t('Version'),
'#type' => 'textfield',
'#description' => t('The version number of the data source (if applicable) for this analysis.'),
'#default_value' => $item_vals['sourceversion'] ?? '',
];
$elements['datasourcegroup']['sourceuri'] = [
'#title' => t("URI"),
'#type' => 'textfield',
'#description' => t('The URI (e.g. web URL) where the source data can be obtained.'),
'#default_value' => $item_vals['sourceuri'] ?? '',
];

return $elements;
}

/**
* {@inheritDoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

// Remove any empty values that aren't mapped to a record id.
foreach ($values as $val_key => $value) {
$values[$val_key]['sourceuri'] = $value['datasourcegroup']['sourceuri'];
$values[$val_key]['sourcename'] = $value['datasourcegroup']['sourcename'];
$values[$val_key]['sourceversion'] = $value['datasourcegroup']['sourceversion'];
}
return $values;
}
}

0 comments on commit e9a2ea0

Please sign in to comment.