MetatagsFieldProcessor fails unserializing data

Created on 18 June 2024, 6 months ago
Updated 3 July 2024, 6 months ago

Problem/Motivation

Drupal 10.2.6
TMGMT XTM module 8.x-6.5
Metatag module 2.0.0

Unable to extract data from meta tag fields when requesting a translation. See the below error message.

Steps to reproduce

Create a new content type.
Add the meta tag field to the content type and enable translation for the field.
Create a new content using the content type and save it.
Translate the content into a different language and request a translation. The below error message appears. Furthermore, any data in meta tags will not be sent to XTM for translation.

Proposed resolution

It appears that this XTM module tries to unserialize the meta tag data as found on line 22 of MetatagsFieldProcessor.php:

$meta_tag_values = unserialize($field->value);

However, in looking at the Metatag module, version 1 serialized the data whereas version 2 JSON encodes the data. In the meta tag module they have a function to account for both scenarios, see metatag.module

/**
 * Decode the different versions of encoded values supported by Metatag.
 *
 * Metatag v1 stored data in serialized arrays. Metatag v2 stores data in
 * JSON-encoded strings.
 *
 * @param string $string
 *   The string to decode.
 *
 * @return array
 *   A Metatag values array.
 */
function metatag_data_decode($string): array {
  $data = [];

  // Serialized arrays from Metatag v1.
  if (substr($string, 0, 2) === 'a:') {
    $data = @unserialize($string, ['allowed_classes' => FALSE]);
  }

  // Encoded JSON from Metatag v2.
  elseif (substr($string, 0, 2) === '{"') {
    // @todo Handle non-array responses.
    $data = Json::decode($string);
  }

  // This is expected to be an array, so if it isn't then convert it to one.
  if (!is_array($data)) {
    $data = [];
  }

  return $data;
}

So the XTM module needs to be updated to account for this change. I've temporarily fixed the problem with this code:

$meta_tag_values = metatag_data_decode($field->value);
Notice: unserialize(): Error at offset 0 of 28 bytes in Drupal\tmgmt_content\MetatagsFieldProcessor->extractTranslatableData() (line 22 of modules/other/tmgmt/sources/content/src/MetatagsFieldProcessor.php).
Drupal\tmgmt_content\MetatagsFieldProcessor->extractTranslatableData(Object) (Line: 275)
Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource->extractTranslatableData(Object) (Line: 184)
Drupal\tmgmt_content\Plugin\tmgmt\Source\ContentEntitySource->getData(Object) (Line: 398)
Drupal\tmgmt\Entity\JobItem->getSourceData() (Line: 1012)
Drupal\tmgmt\Entity\JobItem->recalculateStatistics() (Line: 183)
Drupal\tmgmt\Entity\JobItem->preSave(Object) (Line: 528)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 753)
Drupal\Core\Entity\ContentEntityStorageBase->doPreSave(Object) (Line: 483)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 354)
Drupal\Core\Entity\EntityBase->save() (Line: 345)
Drupal\tmgmt\Entity\Job->addItem('content', 'node', '86') (Line: 137)
Drupal\tmgmt_content\Form\ContentTranslateForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 129)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
Drupal\Core\Form\FormBuilder->processForm('tmgmt_content_translate_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm('Drupal\tmgmt_content\Form\ContentTranslateForm', Object) (Line: 224)
Drupal\Core\Form\FormBuilder->getForm('Drupal\tmgmt_content\Form\ContentTranslateForm', Array) (Line: 19)
Drupal\tmgmt_content\Controller\ContentTranslationControllerOverride->overview(Object, 'node')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
πŸ› Bug report
Status

Closed: outdated

Version

6.5

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bobthebuilder

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

Comments & Activities

Production build 0.71.5 2024