Allowing content tagging on Profile using the Settings Form (SocialTaggingSettingsForm) incorrectly clears cache tags

Created on 25 January 2023, almost 2 years ago
Updated 27 January 2023, almost 2 years ago

Problem/Motivation

In the Submit of the form the code checks:

public function submitForm(array &$form, FormStateInterface $form_state): void {
...
    if ($form_state->hasValue('tag_type_profile')) {
      $result = $this->database->select('cachetags', 'ct')
        ->fields('ct', ['tag'])
        ->condition('ct.tag', 'profile:%', 'LIKE')
        ->execute();

      if ($result !== NULL) {
        // Clear cache tags of profiles.
        $this->cacheTagsInvalidator->invalidateTags($result->fetchCol());
      }
    }
....

There are a number of issues with this:
1. hasValue('tag_type_profile' is always true, as the form state has this value even if set
2.

 $result = $this->database->select('cachetags', 'ct')
        ->fields('ct', ['tag'])
        ->condition('ct.tag', 'profile:%', 'LIKE')
        ->execute(); 

Might return

Uncaught PHP Exception Drupal\Core\Database\DatabaseExceptionWrapper: "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'main.cachetags' doesn't exist: SELECT "ct"."tag" AS "tag"
FROM
"cachetags" "ct"
WHERE "ct"."tag" LIKE :db_condition_placeholder_0 ESCAPE '\\'; Array
(
    [:db_condition_placeholder_0] => profile:%
)
" at /app/html/core/modules/mysql/src/Driver/Database/mysql/ExceptionHandler.php line 53

The CacheTag table is only created on the first invalidation, so it might not exist.
3. The Cache Tag table is only there to keep track of invalidated tags, it's not there to determine which tags to invalidate. So this code doesn't actually clear all profiles.

Proposed resolution

We can remove the code for now as it doesn't clear what it suppose to do, and because we use a preprocess to determine whether or not we show the profile content tag information based on the actual setting it seems to toggle the information correctly already.

🐛 Bug report
Status

Fixed

Version

11.7

Component

Code (back-end)

Created by

🇳🇱Netherlands ronaldtebrake

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

Comments & Activities

Production build 0.71.5 2024