Problem/Motivation

The logic in taxonomy_entity_index_entity_update() contains a heap of great code and logic that can't be re-used.
Similarly there is no OO equivalent of taxonomy_entity_index_get_taxonomy_field_names

Proposed resolution

Provide an OO version of taxonomy_entity_index_get_taxonomy_field_names
Extract the logic out of taxonomy_entity_index_entity_update so that it can be reused by other modules.

Remaining tasks

Replace taxonomy_entity_index_get_taxonomy_field_names() with a service.
Move taxonomy_entity_index_entity_update() logic into a service.

User interface changes

n/a

API changes

taxonomy_entity_index_get_taxonomy_field_names() replaced with the taxonomy_entity_index.field_information service.

Data model changes

n/a

Release notes snippet

TBD

Comments

larowlan created an issue. See original summary.

larowlan’s picture

Title: Consider moving some of the module logic into services so or OO code so that it can be easily tested and other modules can make use of it » Consider moving some of the module logic into services or OO code so that it can be easily tested and other modules can make use of it
larowlan’s picture

Status: Active » Needs review
StatusFileSize
new5.4 KB
new33.96 KB

e.g something like this for the field info

Could do similar for extracting the terms from an entity.

And then another for the db operations.

larowlan’s picture

tests paying for themself - here's the fix

damienmckenna’s picture

StatusFileSize
new5.46 KB

Rerolled. It seems like most of this was committed in #3104318: Create tests and a config schema for taxonomy_entity_index.settings.

Status: Needs review » Needs work

The last submitted patch, 5: taxonomy_entity_index-n3109723-5.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

damienmckenna’s picture

Issue summary: View changes

taxonomy_entity_index_entity_update() needs to be replaced next.

I updated the issue summary.

And then another for the db operations.

Which ones specifically?

damienmckenna’s picture

The test fails because the static cache in the FieldInformation service is not invalidated when the field is added.

damienmckenna’s picture

Is this the problem?

damienmckenna’s picture

Nope, #9 is not the correct approach.

larowlan’s picture

Great work 🙌

I'd be keen to also add something to extract TID values from an entity too. Been running custom code for that on a few projects.

  1. +++ b/src/FieldInformation.php
    @@ -0,0 +1,91 @@
    +    $cache_id = 'entity-taxonomy-fields_' . $entity_type_id;
    

    do we need the entity-taxonomy-fields_ prefix? we're only storing one thing in the cache and its per entity-type

  2. +++ b/src/FieldInformation.php
    @@ -0,0 +1,91 @@
    +          if ($field_definition->getSetting('target_type') === 'taxonomy_term') {
    

    We can read this from the field storage definition as the target-type is a storage level setting, will likely mean we can avoid the ::getFieldDefinitions call inside the two inner for each loops.

    \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions can be called outside both loops, and then if a $field_name key exits, we can call

    ::getSetting</code on that.
    
    Should be dramatically more performant.
    
    I realise we're just moving existing code, but we may as well speed it up while we're at it.
    </li>
    
    <li>
    <code>
    +++ b/taxonomy_entity_index.module
    @@ -157,44 +157,14 @@ function taxonomy_entity_index_entity_update(EntityInterface $entity) {
    +  @trigger_error('taxonomy_entity_index_get_taxonomy_field_names() is deprecated in taxonomy_entity_index:8.x-1.13. Use the \Drupal\taxonomy_entity_index\FieldInformation service instead.', E_USER_DEPRECATED);
    

    this will be 8.x-1.15 now (at least)

larowlan’s picture

Oh, right, I wrote the first version of the patch and said the same thing 🤦 three year ago 😂

damienmckenna’s picture

do we need the entity-taxonomy-fields_ prefix? we're only storing one thing in the cache and its per entity-type

The module includes optional support for per-field index instead of per-entity-type. I have not dug into the logic behind it yet, but I suspect that's part of it.