Hi, i´m developing a module like node reference but different. My problem is that I get an array with de values, but when using that values with default_values on the select, it doesn´t work. I try EVERITHING.
Could anyone get an idea, of wht'a happend ? Thank´s a lot and excuse for the english.

<?php
function miartrel_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    // ...
  } else {
    $items[] = array(
       'path'     => 'node/' .arg(1). '/miartrel',
       'title'    => t('MiArtRel'),
       'callback' => 'miartrel_nodeapi',
	   'callback arguments' => array($node),
       'type'     => MENU_LOCAL_TASK,
    );
  }
  return $items;
}

function  miartrel_nodeapi(&$node) {
	$imprimir .= drupal_get_form('miartrel_form', $node);
	return $imprimir;	
}
//////////////////////////////////////////
/**
* Defines a form.
*/
function traerArtVinc ($node) {
	$opciones = db_query("SELECT field_artculos_vinculados_nid FROM {content_field_artculos_vinculados} WHERE nid = %d", $node->nid);
	$z = 0;
	while ($op = db_fetch_object($opciones)) {
		$opciones_default[$z] = (int) $op->field_artculos_vinculados_nid;
		$z++;
	}
	return $opciones_default;
}

function miartrel_form($node) {
	$opciones = db_query("SELECT field_artculos_vinculados_nid FROM {content_field_artculos_vinculados} WHERE nid = %d", $node->nid);
	$z = 0;
	while ($op = db_fetch_object($opciones)) {
		$opciones_default[$z] = (int) $op->field_artculos_vinculados_nid;
		$z++;
	}
	//$teto = $opciones_default;
	print_r($opciones_default);
	//$teto = traerArtVinc($node);
	//$_SESSION['zaraza'] = $teto;
	$teta = array(19,20,21,22,23);
	//print_r($_SESSION['zaraza']);
	
	$result = db_query("SELECT nid, vid, title FROM {node} WHERE type = '%s' AND status = %d", "articulo", "1");
	$articulos_array = array();
	$articulos_array[0] = "<none>";
	while ($data = db_fetch_object($result)) {
		$articulos_array[$data->nid] = $data->title;
	}	
	$form['articulos_titulos'] = array(
		'#type' => 'value',
		'#value' => $articulos_array,
	);
	$form['articulos_relacionados'] = array(
		'#title' => t('Articulos relacionados'),
		'#type' => 'select',
		'#default_value' => $opciones_default,
		'#multiple' => TRUE,
		'#description' => t('Seleccione el/los articulo/s relacionado/s.'),
		'#options' => $form['articulos_titulos']['#value']
	);	
	$form['quenodo'] = array(
		'#type' => 'value',
		'#value' => $node->nid
	);	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Enviar')
	);
	return $form;
}
/**
* Handle post-validation form submission.
*/
function miartrel_form_submit($form_id, $form_values) {
	$valores = $form_values['articulos_relacionados'];
	$i = 0;
	if ($valores[0]=='0') {
		db_query("DELETE FROM {content_field_artculos_vinculados} WHERE vid = %d", $form_values['quenodo']);
		drupal_set_message("Se desvincularon correctamente los articulos");
	} else {
		foreach ($valores as $valor) {
		   db_query("INSERT INTO {content_field_artculos_vinculados} (vid, delta, nid, field_artculos_vinculados_nid) VALUES (%d, %d, %d, %d)", $form_values['quenodo'], $i, $form_values['quenodo'], $valor);
		   $i++;
		}	
		drupal_set_message("Se vincularon correctamente los articulos");
	}
	drupal_set_message($form_values['quenodo']);
}
?>

Comments

merkurio’s picture

one thing to take note, the default values that I need are $opciones_default. $teta is an expample static array that works fine, but $opciones_default that takes values from the database, doesn't work. I even compare the type of values, and all of that is fine.

Thank's in advance.

caligari’s picture

Try this and tell us if it went ok:

<?php

   $form['articulos_relacionados'] = array(
        '#title' => t('Articulos relacionados'),
        '#type' => 'select',
        '#default_value' => $opciones_default,
        '#multiple' => TRUE,
        '#description' => t('Seleccione el/los articulo/s relacionado/s.'),
        '#options' => $articulos_array

?>
merkurio’s picture

Rafa, thank's for the tip, I actually solved it declaring as GLOBAL the array that I use.

Gracias de nuevo por la ayuda.