Query entity types for each token hook

Created on 29 January 2024, 5 months ago
Updated 30 January 2024, 5 months ago

Problem/Motivation

In votingapi_tokens_tokens() the second line calls _votingapi_tokens_get_entity_types() for every single token type that exists. This causes a query to the database `SELECT DISTINCT entity_type FROM {votingapi_vote}` each time. If you have a large database and receive lots of new ratings which clears mysql cache this can be very inefficient. Also in certain versions of mysql, without declaring an index on this field it can be relatively slow.

Steps to reproduce

Clear cache, watch how many times this query is run.

Proposed resolution

Simply change the logic slightly...

Change:

$entity_types = _votingapi_tokens_get_entity_types();
  foreach ($entity_types as $entity_type) {

For:

  if (strpos($type, 'votingapi_') !== 0) {
    return $replacements;
  }
  $entity_types = _votingapi_tokens_get_entity_types();
  foreach ($entity_types as $entity_type) {

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

🇪🇸Spain willeaton

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024