简体   繁体   中英

Drupal 7 - Custom module: Use custom form input in node submit

this is my first question here!

Im doing a drupal 7 module that create new nodes based on a custom form in batch mode. All work ok except 1 thing that i think is very stupid but i have 2 days researching and now im frustrated.

I cannot fetch the 'nombre_del_producto' input from form to use it on my node submit function as title (lol).

Here is the form creation function:

function producto_qr_batch_form($form, &$form_state) {
  $form = array();
  $form['number_of_nodes'] = array(
    '#type' => 'textfield',
    '#title' => 'Ingresa el número de códigos a generar:',
    '#required' => TRUE,
    '#element_validate' => array('element_validate_integer_positive'),
  );
  //This is the form input i cannot get:
  $form['nombre_del_producto'] = array(
    '#type' => 'textfield',
    '#title' => 'Ingresa el nombre del producto del que vas a generar los códigos:',
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Generar',
  );
  return $form;
}

And here is the submit function:

function producto_qr_create_nodes($number_of_nodes, $form, &$form_state) {
     global $user;
  $node = new stdClass();
  $node->title =  $titulo; //Here is when i want it
  $node->type = "codigo_qr";
  node_object_prepare($node); 
  $node->language = LANGUAGE_NONE; 
  $node->uid = $user->uid; 
  $node->status = 1; 
  $node->promote = 0; 
  $node->comment = 0; 
  $node->field_qr[$node->language][]['value'] = "SOME TEXT HERE";
  $node = node_submit($node);
  node_save($node);
}

Result: The node has an empty title. How to get the 'nombre_del_producto' field from form to the node submit title?

Thank You!

EDIT:

I forgot to put the function where i get the form:

function producto_qr_batch_form_submit($form, &$form_state) {
  $number_of_nodes = $form_state['values']['number_of_nodes'];
  $titulo = $form_state['values']['nombre_del_producto']; //This is correct?

  $operations = array();
  for ($i = 1; $i <= $number_of_nodes; $i++) {

    $operations[] = array('producto_qr_create_nodes',array($i));
  }


  $batch = array(
    'title' => t('Generando Códigos'),
    'operations' => $operations,
    'finished' => 'producto_qr_batch_finished',
    'init_message' => t('La creación de códigos está empezando.'),
    'progress_message' => t('Procesados @current de @total.'),
    'error_message' => t('Ha sucedido un error. Intenta generar códigos en cantidades menores.'),    
  );


  batch_set($batch);
}

Thank you again!

I solved the issue with variable_set and variable_get:

//In function producto_qr_batch_form_submit:
variable_set('titulo', $form_state['values']['nombre_del_producto']);
//In function producto_qr_create_nodes:
$node->title =  variable_get('titulo', 'Aún No');

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM