Hello - I am trying to create an address by concatenating the province and city titles from existing 'province' and 'city' nodes and store the compiled address in the database. The computed field example code that I am using gives me the node ids but I cannot work out how to access and display the node titles. I'm new to Drupal but have some php knowledge. I'd be very grateful for any assistance. Here is the code I am using:

$entity_field[0]['value'] = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_town'))) . ", " . array_pop(array_pop(field_get_items($entity_type, $entity, 'field_state')));

Thanks in advance.

Comments

Stefan Lehmann’s picture

Why would you want to store that again, if you already got all the necessary data somewhere in the database?

Also I'm pretty sure, there are more elegant ways than a computed field for this issue.

Maybe we should start at the beginning with the question: What do you actually want to achieve? :-)

I like cookies!

RampionMyChampion’s picture

Thank you for taking the time to read my post.

What I am trying to achieve is to create an address string to geocode with Geocoder, store the lat/long in the database and display it on a map. The cities and provinces already exist in the database and are themselves nodes that can be accessed with their own fields and data. I want the user to choose the City and Provinces from an autocomplete field. I also want the display of the City and Province to be links back to their own nodes.

I have tried the Location module and Addressfield module but I can't link the relevant fields to the existing nodes.

I hope this clarifies my intention. If you could recommend an alternative that achieves the outcome I would again be very grateful.

Stefan Lehmann’s picture

Ah ok, yes - that indeed sounds a bit tricky and I think your approach sounds pretty good.

I'm not 100% sure how you will do the geo-coding though. As far as I know, that's also triggered on saving the node. But well .. I guess you have an idea for that already.

Anyway, normally the node reference field should give you the node nid of the linked node(s), which you then can load via node_load(nid);

So for example, it should be something like this-ish:

$city_nid = $entity->field_city[LANGUAGE_NONE][0]['nid'];
$city_node = node_load($city_nid);
$city_title = $city_node->title;

A better way would be to load all this field data via Entity Metadata Wrappers. If you google for that you will find a lot of examples. Not quite sure if it works with node reference fields though.

I like cookies!