Skip to content

Commit

Permalink
Merge pull request #1555 from tripal/tv4g1-issue1551-Add-chado-boolea…
Browse files Browse the repository at this point in the history
…n-field

Tripal 4 add a chado boolean field
  • Loading branch information
dsenalik committed Jul 5, 2023
2 parents e72299a + 2504244 commit 1f8c71a
Show file tree
Hide file tree
Showing 14 changed files with 862 additions and 10 deletions.
4 changes: 2 additions & 2 deletions tripal/src/Entity/TripalEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
* @param TripalEntity $entity
*
* @return array
* The returned array has two elements: an an array of values as described
* The returned array has two elements: an array of values as described
* above, and an array of TripalStorage objects,
*/
public static function getValuesArray($entity) {
Expand Down Expand Up @@ -674,7 +674,7 @@ public static function postLoad(EntityStorageInterface $storage, array &$entitie
$field_defs = $field_manager->getFieldDefinitions($entity_type_id, $bundle);
foreach ($field_defs as $field_name => $field_def) {

// Createa fieldItemlist and iterate through it.
// Create a fieldItemlist and iterate through it.
$items = $field_type_manager->createFieldItemList($entity, $field_name, $entity->get($field_name)->getValue());
foreach($items as $item) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\tripal\Plugin\Field\FieldFormatter;

use Drupal\tripal\TripalField\TripalFormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of default Tripal boolean type formatter.
*
* @FieldFormatter(
* id = "default_tripal_boolean_type_formatter",
* label = @Translation("Default Boolean Type Formatter"),
* description = @Translation("The default boolean type formatter."),
* field_types = {
* "tripal_boolean_type"
* }
* )
*/
class DefaultTripalBooleanTypeFormatter extends TripalFormatterBase {

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

foreach($items as $delta => $item) {
$elements[$delta] = [
"#markup" => $item->get("value")->getValue() ? 'True' : 'False',
];
}

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

namespace Drupal\tripal\Plugin\Field\FieldType;

use Drupal\tripal\TripalField\TripalFieldItemBase;
use Drupal\tripal\TripalStorage\BoolStoragePropertyType;
use Drupal\tripal\TripalStorage\StoragePropertyValue;
use Drupal\core\Form\FormStateInterface;
use Drupal\core\Field\FieldDefinitionInterface;

/**
* Plugin implementation of the 'boolean' field type.
*
* @FieldType(
* id = "tripal_boolean_type",
* label = @Translation("Tripal Boolean Field Type"),
* description = @Translation("A boolean field."),
* default_widget = "default_tripal_boolean_type_widget",
* default_formatter = "default_tripal_boolean_type_formatter"
* )
*/
class TripalBooleanTypeItem extends TripalFieldItemBase {

public static $id = "tripal_boolean_type";

/**
* {@inheritdoc}
*/
public static function tripalTypes($field_definition) {
$entity_type_id = $field_definition->getTargetEntityTypeId();
$storage_settings = $field_definition->getSettings();
$termIdSpace = $storage_settings['termIdSpace'];
$termAccession = $storage_settings['termAccession'];

return [
new BoolStoragePropertyType($entity_type_id, self::$id, "value", $termIdSpace . ':' . $termAccession),
];
}
}
35 changes: 35 additions & 0 deletions tripal/src/Plugin/Field/FieldWidget/TripalBooleanTypeWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\tripal\Plugin\Field\FieldWidget;

use Drupal\tripal\TripalField\TripalWidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of default Tripal boolean type widget.
*
* @FieldWidget(
* id = "default_tripal_boolean_type_widget",
* label = @Translation("Tripal Boolean Widget"),
* description = @Translation("The default boolean type widget."),
* field_types = {
* "tripal_boolean_type"
* }
* )
*/
class TripalBooleanTypeWidget extends TripalWidgetBase {


/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['value'] = $element + [
'#type' => 'checkbox',
'#default_value' => $items[$delta]->value ?? NULL,
'#placeholder' => $this->getSetting('placeholder'),
];
return $element;
}
}
2 changes: 1 addition & 1 deletion tripal/src/Services/TripalFieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function validate($field_def) : bool {
else {
$field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions();
if (!in_array($field_def['type'], array_keys($field_types))) {
$this->logger->error('The field type, ' . $field_def['type'] . ' is not a valide field type.');
$this->logger->error('The field type, "' . $field_def['type'] . '", is not a valid field type.');
return FALSE;
}
}
Expand Down
16 changes: 14 additions & 2 deletions tripal/src/TripalField/TripalFieldItemBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\tripal\TripalStorage\IntStoragePropertyType;
use Drupal\tripal\TripalStorage\VarCharStoragePropertyType;
use Drupal\tripal\TripalStorage\TextStoragePropertyType;
use Drupal\tripal\TripalStorage\BoolStoragePropertyType;
use Drupal\tripal\TripalStorage\StoragePropertyValue;
use Drupal\Core\TypedData\DataDefinition;
use \RuntimeException;
Expand Down Expand Up @@ -277,8 +278,11 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
else if ($type instanceof TextStoragePropertyType) {
$properties[$type->getKey()] = DataDefinition::create("string");
}
else if ($type instanceof BoolStoragePropertyType) {
$properties[$type->getKey()] = DataDefinition::create("boolean");
}
else {
throw new RuntimeException("Unknown Tripal Property Type class.");
throw new RuntimeException('Unknown Tripal Property Type class "' . get_class($type) . '"');
}
}

Expand Down Expand Up @@ -314,8 +318,16 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
];
$schema["columns"][$type->getKey()] = $column;
}
else if ($type instanceof BoolStoragePropertyType) {
$column = [
"type" => "int",
"size" => "tiny",
"pgsql_type" => "boolean",
];
$schema["columns"][$type->getKey()] = $column;
}
else {
throw new RuntimeException("Unknown Tripal Property Type class.");
throw new RuntimeException('Unknown Tripal Property Type class "' . get_class($type) . '"');
}
}

Expand Down

0 comments on commit 1f8c71a

Please sign in to comment.