I would like to store a json object in a custom fieldtype, and be able to traverse the object's attributes using twig dot syntax in my template. I am currently storing the raw json object as a string, but I'm not sure what I need to do to be able to reference the stored string as an object.
Here is a shortened example of the raw json string that is being stored:
"Stripe_Customer JSON: {
"id": "cus_4v68MrZu3za8XT",
"object": "customer",
"cards": { "object": "list", "total_count": 0, "has_more": false, "url": "/v1/customers/cus_4v68MrZu3za8XT/cards", "data": [ ] },
"subscriptions": { "object": "list", "total_count": 0, "has_more": false, "url": "/v1/customers/cus_4v68MrZu3za8XT/subscriptions", "data": [ ] },
"etc": "etcetcetc"
}"
In my template I would like to be able to reference the objects properties in some way (i.e. {{ fieldHandle.id }}
) for display only; and on the back-end within a service, I would like to set object properties before saving back to the DB (i.e fieldhandle->id = value
).
I am not sure whether defineContentAttribute
should return AttributeType
String
or Mixed
in this case.
public function defineContentAttribute()
{
return AttributeType::String;
//return AttributeType::Mixed;
}
And if it should be a string, how would one define the column type as "Text" vs the default VARCHAR(255)
?
Update: I see that there is a similar question regarding custom fieldtype with multiple values, however, in this case, the end-user will not be manipulating the data directly in any way — they will be filling in dummy fields that will be send to a third party service via javascript/ajax, a result object returned, and then forwarded (again via ajax) to a back-end service method to be processed and saved to the DB.