Its a wonderful api. However, I am looking for full state names like "California" instead "CA" or "Texas" instead of "TX". How do I get this?
If the platform cannot do it for you, I suggest to create an associative array, if you plan to use it frequently.
In PHP would be something as:
$states = array(
"CA" => "California",
"TX" => "Texas"
);
Thus, if you have
$aState = "TX";
You can get "Texas" by calling:
$longNameState = $states[$aState];
Array should have all the states.
Related
I'm trying to make a custom request in my repository with a WHERE clause inside an array field. I tried something like that, not working, but can better show my problem :
$qb ->andWhere( "p.addresses[:index] = :address" )
->setParameter( "index" , $p_idLang )
->setParameter( "address" , $p_address );
Extracted from the documentation about array type:
Maps and converts array data based on PHP serialization. If you need
to store an exact representation of your array data, you should
consider using this type as it uses serialization to represent an
exact copy of your array as string in the database. Values retrieved
from the database are always converted to PHP’s array type using
deserialization or null if no data is present.
Your query doesn't make sense. You have a few options though:
Retrieve p.adresses and check using php if p.adresses[$index] = $address
Try something much less reliable but that could work:
$val_length = strlen($p_address);
$qb ->andWhere( "p.addresses LIKE :indexAddress" )
->setParameter( "indexAddress" , "%i:$p_idLang;s:$val_length:$p_address%" );
Create a new entity and a relation oneToMany between this entity and the new one.
I'd definetely try option 3. Option 1 isn't an option if the array is big or will become big in the future. I wouldn't go for option 2, but as an experiment could be worth trying.
I'm working on a custom theme and googled the whole day but can't find an answer to my question:
how to print out a cck list field's key instead of its label?
I think it's the right way to access fields via the field api, right?
so I try
$output = field_view_field('node', $node, 'field_list');
print render($output);
that seems to be the way to get the label value of the key|label pair. but even if i set the display options format to 'key' - it only prints the label value. what am I doing wrong?
I know there are other options to render the key but how is it possible using field api?
You can get the field value itself (which will be the allowed values array key) with field_get_items():
$items = field_get_items('node', $node, 'field_list');
$key = $items[0]['value'];
If you need to match those up again at any point you can get the full key/value list from the field metadata:
$info = field_info_field('field_list');
$values = $info['settings']['allowed_values'];
$label = $values[$key];
Here's a similar way. This example works for a Country list with ISO codes.
The idea is to render the Name of the Country that was selected because the dump returns only the key ( iso code in this case)
Format of the select list on the backend:
AR|Argentina
US|United States
Assuming you have selected United States on the backend and you want to print the country name on the node template:
$country_field = field_info_field('field_country_iso');
$country_iso = $node->field_country_iso[LANGUAGE_NONE][0]['value'];
$country_name = $country_field['settings']['allowed_values'][$country_iso];
then
print $country_name; // This will return United States, not US code
I hope this helps.
I have the following string of serialized data in a Wordpress custom field:
$first_string = 'a:9:{s:5:"email";s:13:"test#test.com";s:4:"name";s:15:"Werner
Etsebeth";s:8:"address1";s:17:"1 Giligans
Island";s:8:"address2";s:1:"5";s:4:"city";s:9:"Cape
Town";s:5:"state";s:2:"AL";s:3:"zip";s:4:"7460";s:7:"
country";s:2:"US";s:5:"phone";s:0:"";}
$second_string = 'a:1:{i:4;a:1:{i:0;a:6:
{s:3:"SKU";s:0:"";s:4:"name";s:12:"Hypnotherapy";s:3:"url";s:72:"http://localhost
/mindworksa.co.za/wordpress/store/products/hypnotherapy
/";s:5:"price";s:5:"50.00";s:8:"quantity";s:1:"1";s:8:"download";s:0:"";}}}'
How do I assign the info to variables so I can access individually eg $SKU = "", $name = etc.
I've never worked with serialized data before and any help would be greatly appreciated.
Many thanks
I am unable to unserialize your example (did you paste it in correctly?) but Wordpress uses serialize() to serialize objects to store them in the database.
You can unserialize them using unserialize().
A quick example:
$serialized = 'a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}';
var_dump(unserialize($serialized));
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
When using the followin code to define a cck location:
$location = array(
'latitude' => "0.000000",
'longitude' => "0.000000",
'street' => "street is set",
'city' => "city is set",
'postal_code' => "post code set"
);
$newLocationID = location_save( $location );
All the information is created with the exception of the longitude and the latitude.
Can anyone tell me why and how I can solve this problem?
I had the same problem and this is how I solved it.
As the best documentation is a source code, I had a look into the location_save function and found out that location module tries to get the latitude and longitude by geocoding, using function _location_geo_logic.
This is default setting and when you have a look into the _location_geo_logic function you will notice, that this geocoding overwrites latitude and longitude keys in input array. However, this geocoding is only being done if the $inhibit_geocode variable is set to FALSE, so the only thing you have to do is add this key to the $location array:
$location['inhibit_geocode'] = TRUE;
For me it worked great, I hope so will for you.
My question is about: "adding field data to referenced node without replacing existing data".
For example, I have a project node with team members referencing the project. Each team member has on its node a location, ie, 'United Kingdom', 'United States', 'Australia'.
On the project node I have those exact same fields. I need to create a rule so that when a 'team member' node is created, its location is added to the project node without replacing existing content.
So for instance, a project node with a team member from the United Kingdom would also have on its location field, 'United Kingdom'. When a team member from 'United States' is added, the project's location field would have 'United Kingdom' and 'United States'. When a team member who's location is both Canada AND France is added, the project's location becomes United Kingdom, United States, Canada, and France.
Thanks!
Doing something like:
return array(
0 => array('value' => 'United Kingdom')
);
Just wouldn't work! It would replace the existing values. How do I make it so that it adds on to the existing values. Thanks!
Is it important to actually have the reference on the node or just to display the location.
If you are just worried about displaying the location then I think you can do this quite easily with a view.
There is a node reference reverse option I believe, but this would only display the team members and not the location.
If it is important to actually have the location information in the project node, then you will have to use hook_nodeapi op = save with code similar to Matts' answer.
Will something like this work? Basically we capture the current nodes cck location field (change the field names below to fit), load the reference node, and add the location data to it, and save it away. I've not added the code to check to see if the location already exists, but that's something for another day. - Hope it helps.
#some debug data below
#krumo ($node);
#print "<pre>". print_r($node,true) . "</pre>";
#$node is our current data set
# save the current $node nid into a variable
$nid = $node->nid;
#get the reference nid
$refnid = $node->field_refnid[0][nid];
#get the location
$currentlocation = $node->field_team_location[0][value];
# nowload the reference node
$refnode = node_load ($refnid);
# some debug data below
#krumo ($refnode);
#print "<pre>". print_r($refnode,true) . "</pre>";
$newlocation = array ("value"=>$currentlocation);
$refnode->field_loacations[] = $newlocation;
#now save the reference node
node_save ($refnode);
#drupal_goto ("node/$nid");
Have you tried:
return array(
array('value' => 'United Kingdom'),
array('value' => 'United States'),
);