Drupal Answers Asked by johnventions on November 21, 2021
I’m attempting to get MetaTag info for a decoupled site but the JSON API is not converting the object to JSON correctly. I’m seeing other fields show up fine – the issue is isolated to the MetaTags.
The output appears like this:
field_meta_tags: "a:2:{s:5:"title";s:10:"Test Title";s:11:"description";s:21:"Test Body Description";}",
From what I can tell, s:5 represents an string of 5 characters (“title”), s10 represents 10 characters (“Test Title”), etc etc. Has anyone seen this before?
Drupal 8
Metatag version: ‘8.x-1.5’
JSON API version: ‘8.x-1.16’
So you can create an own custom module providing a ResourceFieldEnhancer plugin for the metatags field, which could do your desired unserialization.
This won't work as entities that don't have customized overrides of the default metatags do have a value NULL and thus they're not processed by an enhancer at all.
The patch here works nicely and adds an unserialized output in the jsonapi resource: https://www.drupal.org/project/metatag/issues/2945817
The output the patch produces in the resource can be found in the attribute key metatag_normalized.
Answered by adrum99 on November 21, 2021
In fact, the metatag field IS serialized. As stated by Monkeybrain, the result you get as field value is a serialized PHP array.
This is due to the nature of the metatag field, which internally stores its value as a string (serialized PHP array). By using the JSON API module alone, you'll get this value only.
If you need the serialized PHP array to be an unserialized JSON object, you will have to do some custom programming.
Either implement an own REST API endpoint, or - probably easier to do - use the JSON API extras module. It introduces an additional resource field enhancer plugin type to process the values of JSON API entity fields. So you can create an own custom module providing a ResourceFieldEnhancer plugin for the metatags field, which could do your desired unserialization.
Bear in mind, that the value saved within the metatag field(s) does not contain entity defaults. If these are required, you should also use the Metatag module's metatag.manager
service to additionally fetch this information.
Answered by Mario Steinitz on November 21, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP