1

We are using the Countries module to provide country fields where editors can choose a country and it gets displayed in the frontend. Which works absolutely fine.

But we want to fill that field programmatically through a custom module. No matter what value we provide to fill the field (country name, ISO 2 code etc.) the field doesn't get populated.

We are certainly sure that there is no any error somewhere else as we are filling multiple fields in our module and all are working as expected.

We want to know in which format does the country field expect the value to be?


Clarification:

We have a field field_country in profile2 with the Country type coming through Countries module. We are getting input in our module through following code in our form:

include_once DRUPAL_ROOT . '/includes/iso.inc';
include_once DRUPAL_ROOT . '/includes/locale.inc';

$form['country'] = array(
  '#title' => t('Country'),
  '#type' => 'select',
  '#description' => t('Please Select Your Country Where Do You Belong To.'),
  '#required' => 'true',
  '#options' => array('' => t('-Select a Country-')) + country_get_list(),
  '#default_value' => isset($values['country']) ? $values['country'] : '',
);

We have also tried '#type' => 'country' and a few other things but same result.

For submitting this value we are using the code:

$profile->field_country['LANGUAGE_NONE'][0]['value'] = $data;
5
  • 1
    Give some relevant code samples with what you have tried and where it fails
    – GiorgosK
    Commented Oct 15, 2018 at 13:56
  • @GiorgosK – ..., please. (:
    – leymannx
    Commented Oct 15, 2018 at 14:22
  • Hi there, I have edited my question. now check please. Commented Oct 15, 2018 at 15:29
  • can you post the code of country_get_list() function ?
    – berramou
    Commented Oct 15, 2018 at 20:15
  • @berramou – country_get_list() is a core function.
    – leymannx
    Commented Oct 15, 2018 at 20:59

1 Answer 1

0

I played around with this now for a little while. Seems all you need is the iso2 key to be set.

global $user;

$node = new stdClass();
$node->title = 'Lorem Ipsum';
$node->type = 'article';
node_object_prepare($node);

$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->status = 1;

// In the end this was it.
$node->field_country[LANGUAGE_NONE][] = [
  'iso2' => 'FR',
];

$node = node_submit($node);
node_save($node);

Apart from that you can use the Devel module to either dump variables like that

dpm($variables);

for example in a MYMODULE_preprocess_node() hook. After you filled out a node with a country field from the UI you can use that to check which values are set. Or you can checkout node values directly by appending /devel to a node path (http://example.com/node/42/devel). Although I must admit that what I got there as output was quite misleading. All that really was needed was the iso2 key.

1
  • Worked like a charm! We were trying to make another array inside "value" but actually it should be after item number [0]. Thanks a lot! Thank you so much for your precious time and help. Commented Oct 16, 2018 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.