1

I need to edit a node into a custom module. In order to access to the form, I have a custom text link in a view, in that view I have different articles, each row is an article and when I click to the custom text link, I can edit it.

enter image description here

My custom module is "evento". I have the next function to take the node id.

   function evento_load($example_id) {

       return $example_id;
  }

Then, this one to call to the fuction that creates the form.

function edicion_wizard($form, &$form_state, $example_id) {


    $form_state['step_information'] = evento_form($form, $form_state, $example_id);

    //$form = $form_state['step_information'];
    $form['finish'] = array(
      '#type' => 'submit',
      '#value' => t('Terminar'),
      '#submit' => array('form_evento_wizard_submit'),
    );


   $form['next']['#validate'] = array();

    return $form;
}

And this is the function to create the form.

function evento_form($form, &$form_state, $node) {
  //fill your form with data from exemple_id item


     $type = node_type_get_type($node);
     //$node = node_load($example_id);
     $form['idioma'] = array(
        '#type' => 'select',
        '#title' => t('Idioma del evento'),
        '#options' => array('es' => t('Castellano'), 'eu' => t('Euskera'), 'am'=> t('Ambos'),),
        '#default_value' => $node->language,
        '#states' => array (
            'visible' => array(
                ':input[name="Idioma"]'=>array("value"=>"Ambos"),
            ),
        ),


   );

  $query = "SELECT name, tid FROM taxonomy_term_data WHERE vid=2 AND language = '$language_url';";
  $result = db_query($query);
  foreach ($result as $row2) {
     $options2[$row2->tid] = t($row2->name);
  }
  $form['municipios'] = array(
     '#type' => 'select',
     '#title' => t('Municipio'),
     '#options' => $options2,
     '#default_value' => $node->field_municipio_simple[LANGUAGE_NONE][0]['tid'],
     '#required' => TRUE,
  );

 /*  $form['Lugar'] = array(
    '#type' => 'textfield',
    '#title' => t('Lugar'),
    '#default_value' => $node->field_municipio_simple,
    '#required' => TRUE,

  );*/

  $form['inicio'] = array(
    '#type' => 'date_popup',
    '#title' => t('Inicio evento'),
    '#date_format' => 'd/m/Y H:i',
    '#required' => TRUE,
    '#prefix' => '<table border= \'0\'><tr><td>',
    '#suffix' => '</td>',
    '#default_value' => $node->field_inicio[LANGUAGE_NONE][0]['value'],
  ); 

  $form['fin'] = array(
    '#type' => 'date_popup',
    '#title' => t('Fin evento'),
    '#date_format' => 'd/m/Y H:i',
    '#required' => TRUE,
    '#prefix' => '<td>',
    '#suffix' => '</td></tr></table>',
    '#default_value' => $node->field_fin[LANGUAGE_NONE][0]['value'],
  ); 



  $form['titulo'] = array(
    '#type' => 'textfield',
    '#title' => t('Título'),
    '#required' => TRUE,
    '#default_value' => $node->title, 
  );

  $form['texto1'] = array(
    '#type' => 'text_format',
    '#title' => t('Texto '),
    '#required' => TRUE,
    '#default_value' => $node->field_descripci_n[LANGUAGE_NONE][0],

    );  

   $form['titulo_eu'] = array(
    '#type' => 'textfield',
    '#title' => t('Título en Euskera'),
    '#states' => array (
            'visible' => array(
                ':input[name="idioma"]'=>array("value"=>"am"),
            ),
        ),  

  );    

  $form['texto2'] = array(
        '#type' => 'text_format',
        '#title' => t('Texto en Euskera'),
        '#states' => array (
            'visible' => array(
                ':input[name="idioma"]'=>array("value"=>"am"),
            ),
        ),
  );

  return $form;


}

Obviusly, I have an other one to save the new node values.

The problem that I have is that I can see the values. The form is not show. I don't know if I'm using the right functions or the correct path in the custom text link. (Is the first time I'm doing something like this).

Can anybody help me? Thanks in advance.

enter image description here

ANSWERD:

What I've done is in the hook_menu use the wildcard

$items['evento/editar/node/%'] = array(
        'title' => t('Editar evento de agenda'),
        'description' => t('Editar evento de agenda - Contribuyente'),
        'parent'=> array('evento'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('edicion_wizard',3),
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
        'menu_name' => 'main-menu',
        'customized' => 1, // Must be 1 in order to not dispaly the link in any language
            'language'  => 'es', // language is required in order to make the item translatable
  );

So in 'page arguments' I take the 3rd value that is the wildcard. That way I take the node_id. Now, I don't need evento_load function.

In edicion_wizard function there was a mistake. I need to insert the values into a form, that was the reason of not beeing show.

function edicion_wizard($form, &$form_state, $example_id) {

    $node = node_load($example_id);
    $form_state['step_information'] = edicion_form($form, $form_state, $node);
    **$form = $form_state['step_information'];**
    //$form = $form_state['step_information'];
    $form['finish'] = array(
      '#type' => 'submit',
      '#value' => t('Terminar'),
      '#submit' => array('form_evento_wizard_submit'),
    );


   $form['next']['#validate'] = array();

    return $form;
}
3

First you need to define your custom path to process incoming requests to 'evento/editar/node/%node'. This can be done by implementing hook_menu().

function evento_menu() {
  $items['evento/editar/node/%node'] = array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array('evento_form', 3),
      'access arguments' => array('access content')
  );
  return $items;
}

Note where the path contains %node. This is a wildcard loader and it will automatically run 'node_load()' for you on the node id that you display in the 3rd argument in the path. (Remember, paths start at 0)

drupal_get_form() has a required input of $form id (the function name that builds your form array) but also accepts any number of additional parameters.

What 'page arguments' => array('evento_form', 3) does, is pass the $form_id evento_form to drupal_get_form and then passes the 3rd element from the URL (the automatically loaded node object %node) as an additional argument to drupal_get_form. To access this the additional variables you've passed to your form function you will need to use $form_state['build_info']['args']

You can now do your form manipulations with your additional variables passed using hook_menu.

If I misunderstood your question, please clarify. I'll try to update my answer accordingly.

| improve this answer | |
0

That's what Devel is for. Install it, enable it, maybe configure permissions. And then let Devel print out the values you need. Like that:

MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  dpm($form_id, 'FORM ID');
}

Let's say the dpm($form_id) returned 'evento_node_form' then you'ld go on like that (and always let Devel print out the values you want):

MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'evento_node_form') {
    dpm($form, 'FORM');
    dpm($form_state, 'FORM STATE');

    // do your custom stuff here
    dpm($node->field_municipio_simple, 'MY FIELD');
  }
}

And don't forget to constantly clear cache when developing.

| improve this answer | |
  • I don't understand what is $form_id for. And also, in MYMODULE_form_alter how do you know that $node has that fields? I mean, you don't have declare that variable anywhere. – more770 Apr 28 '14 at 11:56
  • When you dpm($form) you will know. Everythings in there, all the fields. Simply create a very fundamental custom module with only my first function in it. So you get the form id. Then you change the function to look like my second function and replace 'evento_node_form' with the id you got from the dpm($form_id) and then you let dpm($form) return you the whole node edit form with all its fields. Write these lines in a module, activate it, try to add a node, and you'll see a clickable object on top of the page. Click on it to open it and see the magic :D – leymannx Apr 28 '14 at 12:08
  • 2
    You're spot on @koivo. dpm() and dd() are absolutely awesome. :) – Citricguy Apr 28 '14 at 13:37

Your Answer

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

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