这是我的第一个问题!
我正在做一个drupal 7模块,该模块在批处理模式下基于自定义表单创建新节点。一切正常,除了我认为非常愚蠢的一件事,但我花了两天的研究时间,现在感到沮丧。
我无法从表单中获取“ nombre_del_producto”输入,无法在我的节点提交功能中将其用作标题(lol)。
这是表单创建功能:
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;
}
这是提交功能:
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);
}
结果:该节点的标题为空。如何从表单获取“ nombre_del_producto”字段到节点提交标题?
谢谢!
编辑:
我忘了把函数放到表格上:
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);
}
再次感谢您!
答案 0 :(得分:0)
我用variable_set和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');
:)