7 years ago
3
Topic

Hello guys,

this are two questions in one I think:

In a content form I get distance and duration and fill them as JSON in a text field to store it in my database - it's the same thing like getting values from an API.

So in both cases (database or maybe an API) I have the following simple JSON structure:

	{"duration":"123456","distance":"481443"}

When I use a text field now with storage option JSON and Free (pointing two my database column where the JSON is stored) like this:

myfield[duration]

I get the value "123456" shown in a form with myfield.

Question 1:

I only want to display the field and don't want to store the data, is that possible?? To have field in a form, only showing, not storing??
Yes I know the option in the dropdown not to store the value but then I also don't have the option tho point my field to get the right values....

If the data would be stored my JSON-Array in the database get's corrupted because in this case he would store the complete Array now in "duration"...
So I need the option to only display the values...

Question 2:

With the simple JSON-Array from above reading the values in text fields is no problem, but If the structure get's more complex I fail in reading the data. Maybe it's not possible or I'm trying the wrong syntax. Let's say we have JSON data like this:

{
   "user":{
      "id":78046872,
      "name":"TheWhykiki",
      "profileIconId":28,
      "summonerLevel":1,
      "revisionDate":1442412202000
   },"distance":"123456"
}

Then I get the distance like before myfield[distance], but If I'm trying to retrieve data from the Array "user" I fail constantly :-)
I want to get the value "id" so I tried 

myfield[user[id]]

But I get nothing, the value stays empty but I get no error.

The usage of:

myfield[user]

gives the warning that there is an array not a string. How to get one value from this array to use it??

Thank you very much

Kiki

Get a Book for SEBLOD
4229 Posts
Kadministrator
7 years ago
2
Level 1

Hi,

in general in php you get values form array like this $myfield[user][id] - but I would not dare to say that multidimensional arrays like this are supported in Seblod JSON storage.

To have a field that only shows value you can put Value variation on it.

7 years ago
1
Level 2

Thank you. I think multidimensional arrays are ot working...but I'll try another way by building a new Field Plugin like "text" to specify the value of the field.
Maybe the solution could be worth posted here?

The second thing:
Setting "Value" as Variation has no other effect than showing it in an other way, but in fact there is a hidden input which stores the value...

In my example I have a Field in Form Step 1 which stores duration and distance as JSON. In Step 2 I build another text field which gets the value from distance like this:

myfield[distance]

But now when I submit the form in Step 2 (even with Value as variation) only the distance is saved as JSON - so the duration data will be lost...
So do I need to "rebuild" the complete JSON if I want to keep it "complete" (a textfield for every value)??

Or is there any simpler way to force Seblod not to save the new value but only show it...

7 years ago
0
Level 3

I've found a way to keep the data intact - maybe this could be interesting for someone else.
I made a copy of a simple Text Field Plugin with the Seblod Developer tool so I had the simplest structure.

So I modified my new:

my_new_cck_plugin.php

in

/plugins/cck_field/my_new_cck_plugin

and defined to different Variables for the value. Then I searched for this code:

		if ( ! $field->variation ) {
			$field->form	=	$form;
			if ( $field->script ) {
				parent::g_addScriptDeclaration( $field->script );
			}
		} else {
			parent::g_getDisplayVariation( $field, $field->variation, $valueOriginal, $value, $form, $id, $name, '<input', '', '', $config );
		}

As you can see I defined a second $valueOriginal and I have $value - so the original value gets stored if you set Variation of the field to "Value", but the searched value gets displayed.
This only makes sense if you have stored JSON Objects in your database and want to show only one of the values in your form - but want to keep the Database intact.


Maybe this is helpful.

Get a VIP membership