Skip to content

Commit

Permalink
Merge pull request #1598 from tripal/tv4g2-1593-chadoStorageCleanup
Browse files Browse the repository at this point in the history
Chado storage cleanup
  • Loading branch information
spficklin committed Aug 10, 2023
2 parents 0ee074f + 84b6bcd commit 7284294
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 423 deletions.
27 changes: 14 additions & 13 deletions tripal/src/Entity/TripalEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* label = @Translation("Tripal Content"),
* bundle_label = @Translation("Tripal Content type"),
* handlers = {
* "schema" = "Drupal\tripal\Entity\TripalEntityStorageSchema",
* "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\tripal\ListBuilders\TripalEntityListBuilder",
Expand Down Expand Up @@ -452,10 +451,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
* - 3rd: Delta value of the field item.
* - 4th: the property key.
* - 5th: One of the following keys:
* - 'type': The property type object.
* - 'definition': the field definition object for the field that this
* property belongs to.
* - 'value': the property value object.
* - 'operation': the operation to use when matching this value.
*
* This function also returns an array of TripalStorage objects.
*
Expand Down Expand Up @@ -490,6 +487,9 @@ public static function getValuesArray($entity) {
$tripal_storages[$tsid] = $tripal_storage;
}

// Add the field definition to the storage for this field.
$tripal_storages[$tsid]->addFieldDefinition($field_name, $item->getFieldDefinition());

// Get the empty property values for this field item and the
// property type objects.
$prop_values = $item->tripalValuesTemplate($item->getFieldDefinition());
Expand All @@ -503,15 +503,16 @@ public static function getValuesArray($entity) {
// property).
$item->tripalClear($item, $field_name, $prop_types, $prop_values, $entity);

// Add the property types to the storage plugin.
$tripal_storages[$tsid]->addTypes($field_name, $prop_types);

// Prepare the properties for the storage plugin.
// Prepare the property values for the storage plugin.
// Note: We are assuming the key for the value is the
// same as the key for the type here... This is a temporary assumption
// as soon the values array will not contain types ;-)
foreach ($prop_types as $prop_type) {
$key = $prop_type->getKey();
$values[$tsid][$field_name][$delta][$key] = [
'definition' => $item->getFieldDefinition(),
'type' => $prop_type
];
$tripal_storages[$tsid]->addTypes($prop_type);
$values[$tsid][$field_name][$delta][$key] = [];
}
foreach ($prop_values as $prop_value) {
$key = $prop_value->getKey();
Expand Down Expand Up @@ -581,8 +582,8 @@ public function preSave(EntityStorageInterface $storage) {
// Load into the entity the properties that are to be stored in Drupal.
$prop_values = [];
$prop_types = [];
foreach ($values[$tsid][$field_name][$delta] as $prop_info) {
$prop_type = $prop_info['type'];
foreach ($values[$tsid][$field_name][$delta] as $key => $prop_info) {
$prop_type = $tripal_storages[$tsid]->getPropertyType($field_name, $key);
$prop_value = $prop_info['value'];
$settings = $prop_type->getStorageSettings();
if (array_key_exists('drupal_store', $settings) and $settings['drupal_store'] == TRUE) {
Expand Down Expand Up @@ -675,7 +676,7 @@ public static function postLoad(EntityStorageInterface $storage, array &$entitie
$prop_types = [];
foreach ($values[$tsid][$field_name][$delta] as $key => $info) {
$prop_values[] = $info['value'];
$prop_types[] = $info['type'];
$prop_types[] = $tripal_storages[$tsid]->getPropertyType($bundle, $field_name, $key);
}

// Now set the entity values for this field.
Expand Down
186 changes: 0 additions & 186 deletions tripal/src/Entity/TripalEntityStorageSchema.php

This file was deleted.

54 changes: 52 additions & 2 deletions tripal/src/TripalStorage/Interfaces/TripalStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\tripal\TripalStorage\Interfaces;

use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\tripal\TripalField\TripalFieldItemBase;

/**
* Defines an interface for tripal storage plugins.
Expand All @@ -12,18 +13,22 @@ interface TripalStorageInterface extends PluginInspectionInterface {
/**
* Adds the given array of new property types to this tripal storage plugin.
*
* @param string $field_name
* The name of the field the properties belong to.
* @param array $types
* Array of \Drupal\tripal\TripalStorage\StoragePropertyTypeBase objects.
*/
public function addTypes($types);
public function addTypes(string $field_name, array $types);

/**
* Removes the given array of property types from this tripal storage plugin.
*
* @param string $field_name
* The name of the field the properties belong to.
* @param array $types
* Array of \Drupal\tripal\TripalStorage\StoragePropertyTypeBase objects.
*/
public function removeTypes($types);
public function removeTypes(string $field_name, array $types);

/**
* Returns a list of all property types added to this storage plugin type.
Expand All @@ -35,6 +40,51 @@ public function removeTypes($types);
*/
public function getTypes();

/**
* Returns a single propertyType object based on the parameters.
*
* @param string $bundle_name
* The name of the bundle on which the field is attached that the properties
* belong to.
* @param string $field_name
* The name of the field the properties belong to.
* @param string $key
* The key of the property type to return.
* @return object
* An instance of the propertyType indicated.
*/
public function getPropertyType(string $field_name, string $key);

/**
* Stores the field definition for a given field.
*
* NOTE: the definition for every field mentioned in the values array
* of an insert/update/load/find/deleteValues() method must be added
* using this function before the *Values() method can be called.
*
* @param string $field_name
* The name of the field based on it's annotation 'id'.
* @param object $field_definition
* The field configuration object. This can be an instance of:
* \Drupal\field\Entity\FieldStorageConfig or
* \Drupal\field\Entity\FieldConfig
* @return boolean
* Returns true if no errors were encountered and false otherwise.
*/
public function addFieldDefinition(string $field_name, object $field_definition);

/**
* Retrieves the stored field definition of a given field.
*
* @param string $field_name
* The name of the field based on it's annotation 'id'.
* @return object $field_definition
* The field configuration object. This can be an instance of:
* \Drupal\field\Entity\FieldStorageConfig or
* \Drupal\field\Entity\FieldConfig
*/
public function getFieldDefinition(string $field_name);

/**
* Inserts values in the field data store.
*
Expand Down
3 changes: 2 additions & 1 deletion tripal/src/TripalStorage/StoragePropertyTypeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function __construct($entityType, $fieldType, $key, $term_id, $id, $stora
$this->id = $id;
$this->cardinality = 1;
$this->searchability = TRUE;
$this->operations = array("eq","ne","contains","starts");
$this->operations = array('=','<>','>','>=','<','<=','STARTS_WITH','CONTAINS',
'ENDS_WITH','IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN');
$this->sortable = TRUE;
$this->readOnly_ = FALSE;
$this->required = FALSE;
Expand Down

0 comments on commit 7284294

Please sign in to comment.