1

While trying to creating a node programmatically using 'drupal_form_submit' i've come across a php error in the form.inc code

Here's my code

function vl_sample_data_generate_yvbct1() {

  module_load_include('inc', 'node', 'node.pages');

  global $user;

  $content = 'Generating YVB CT1';
  $form_state = array();
  $data = array();
  $node = (object) array(
    'uid' => $user->uid,
    'name' => $user->name,
    'type' => 'yvb_ct1',
    'language' => LANGUAGE_NONE,
  );
  node_object_prepare($node);

  $data['uid'] = $user->uid;
  $data['created'] = time();
  $data['type'] = 'yvb_ct1';
  $data['language'] = LANGUAGE_NONE;
  $data['title'] = 'Node created on ' . time();
  $data['status'] = 1;
  $data['op'] = 'Save';

  $data['field_middle_name'][LANGUAGE_NONE][0]['value'] = 'Alex';
  $data['field_gender'][LANGUAGE_NONE][0]['tid'] = 24;

  $form_state['values'] = $data;

  drupal_form_submit('yvb_ct1_node_form', $form_state, $node);

  return $content;
}

The error i get when i run the function (as a menu callback) is

Warning: Illegal offset type in isset or empty in _form_validate() (line 1333 of /opt/httpd/htdocs/www/staging/jns/trunk/includes/form.inc).
An illegal choice has been detected. Please contact the site administrator.

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of /opt/httpd/htdocs/www/staging/jns/trunk/includes/entity.inc).

Warning: Illegal offset type in taxonomy_field_validate() (line 1519 of /opt/httpd/htdocs/www/staging/jns/trunk/modules/taxonomy/taxonomy.module).

Notice: Trying to get property of non-object in taxonomy_field_validate() (line 1519 of /opt/httpd/htdocs/www/staging/jns/trunk/modules/taxonomy/taxonomy.module).

However, if i comment out the line

  $data['field_gender'][LANGUAGE_NONE][0]['tid'] = 24;

It works fine. Am using Drupal 7.26. Also, the term tid does exist and belongs to the valid Vocabulary which is assigned in the content type.

Any help regarding the above problem is appreciated.

Thanks

Yashesh

2 Answers 2

1

It is possible that 24 is not a valid taxonomy term ID in the system, but I'm not sure if that's it. So I would check that using taxonomy_get_term_by_name or something. Also, and perhaps first, make sure to verify the field_gender field exists, as it might not always be there in the system and you don't want nasty bugs down the road.

// Check if field_gender field exists for node type yvb_ct1
$field_instances = field_info_instance('node', 'field_gender', 'yvb_ct1');
if (isset($field_instances['field_gender'])) :

  // Now check if the taxonomy ID is correct
  // I assume you are looking for a term name of 'female'
  if (!empty(taxonomy_get_term_by_name('female', 'gender')))
    // now try setting term reference field value

endif;

This is untested but I hope it might help get you closer.

1
  • Thanks @mavame. I checked the value for field_instances and it does not contain a key field_gender, instead it returned $field_instances = Array ( [label] => Gender [widget] => Array [settings] => Array [display] => Array [required] => 0 [description] => [default_value] => [id] => 26 [field_id] => 8 [field_name] => field_gender [entity_type] => node [bundle] => yvb_ct1 [deleted] => 0 ) does that look correct to you ?. Feb 6, 2014 at 4:29
0
// For autocomplete term reference fields 
$tax = taxonomy_term_load(4)->name;
$data['field_test_tax'][LANGUAGE_NONE] = $tax;

//For widgets select list or radio buttons term reference fields 
$data['field_gender_tax'][LANGUAGE_NONE] = 8; //tid value
$data['field_gender_tax_radio'][LANGUAGE_NONE] = 11; //tid value

Your Answer

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

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