0

In the hook_form_alter, I add 3 textfield form elements and 1 button with ajax callback:

function zwznertags_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'book_node_form') {
    $form['ner_ajax_button'] = array(
      '#type' => 'button',
      '#value' => 'Recognize named entities',
      '#limit_validation_errors' => array(),
      '#ajax' => array(
        'method' => 'replace',
        'callback' => 'zwznertags_callback',
        'wrapper' => 'div-zwz-ners',
       )
    );
    $form['ner'] = [
        '#type' => 'fieldset',
        '#title' => t('Named entities'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#prefix' => '<div id="div-zwz-ners">',
        '#suffix' => '</div>',
    ];

    $form['ner']['person_names'] = [
        '#type' => 'textfield',
        '#name' => t('Persons'),
        '#title' => 'Recognized persons',

    ];
    $form['ner']['place_names'] = [
      '#type' => 'textfield',
      '#name' => t('Places'),
      '#title' => 'Recognized places',
    ];
    $form['ner']['org_names'] = [
      '#type' => 'textfield',
      '#name' => t('Orgnizations'),
      '#title' => 'Recognized orgnizations',
    ];
  }
}

In the callback function, I set the 3 textfield values and return them:

 function zwznertags_callback(&$form, &$form_state){  
    $form['ner']['person_names']['#value'] = 'person1, person2, person3';
    $form['ner']['place_names']['#value'] = 'place1, place2, place3';
    $form['ner']['org_names']['#value'] = 'org1, org2, org3';

    return $form['ner'];
  }  

Then in hook_node_submit, I want to set values of these 3 textfield respetively to 3 term_reference fields when clicking on "Save" button. But all the 3 textfield value are empty in the $form_state.

function zwznertags_node_submit($node, $form, &$form_state) {
  dpm($form_state);
  if ($form_state['values']['person_names']) {
    $person_names = explode(', ', check_plain($form_state['values']['person_names']));
    foreach ($person_names as $person_name) {
      $tidqueryresult = db_query('select tid from taxonomy_term_data where vid=2 and name = :name', [':name'=>$person_name])->fetchObject();
      $tid = empty($tidqueryresult)? 'autocreate': $tidqueryresult->tid;
      $node->field_persons['und'][] = ['tid'=>$tid, 'vid'=>2, 'name'=>$person_name, 'vocabulary_machine_name'=>'person'];
    }
  }
  if ($form_state['values']['place_names']) {
    $place_names = explode(', ', check_plain($form_state['values']['place_names']));
    foreach ($place_names as $place_name) {
      $tidqueryresult = db_query('select tid from taxonomy_term_data where vid=3 and name = :name', [':name'=>$place_name])->fetchObject();
      $tid = empty($tidqueryresult)? 'autocreate': $tidqueryresult->tid;
      $node->field_places['und'][] = ['tid'=>$tid, 'vid'=>3, 'name'=>$place_name, 'vocabulary_machine_name'=>'place'];
    }
  }
  if ($form_state['values']['org_names']) {
    $org_names = explode(', ', check_plain($form_state['values']['org_names']));
    foreach ($org_names as $org_name) {
      $tidqueryresult = db_query('select tid from taxonomy_term_data where vid=4 and name = :name', [':name'=>$org_name])->fetchObject();
      $tid = empty($tidqueryresult)? 'autocreate': $tidqueryresult->tid;
      $node->field_orgs['und'][] = ['tid'=>$tid, 'vid'=>4, 'name'=>$org_name, 'vocabulary_machine_name'=>'org'];
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.