Hello
I'm building my first module and I'm stuck with my 'edit' form.
I create a Holidays period with 2 dates (beginning day-finishing day). It looks like this:
------------------------------------------
Holidays management

Starting day Ending day Operations
1/10/2011 5/10/2011 edit delete
2/11/2011 5/11/2011 edit delete

Add a Holidays period
------------------------------------------

I've succeeded to create my 'add' form.
Looking through the uc_attributes module I similarly built a menu item

function cart_scheduler_menu() {
...
// menu edit Holiday period
 $items['admin/store/Cart_scheduler/Holidays/%id/edit'] = array(
    'title' => 'Edit Holiday Period',
    'description' => 'cart_scheduler settings',
    'page callback' => 'drupal_get_form',
	'page arguments' => array('cart_scheduler_holidays_add_form'),
    'access arguments' => array('administer Cart Scheduler'),
    'type' => MENU_CALLBACK,
  );
// menu add Holiday period
  $items['admin/store/Cart_scheduler/Holidays/add'] = array(
    'title' => 'Add a new Holidays Period',
    'description' => 'cart_scheduler settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('cart_scheduler_holidays_add_form'),
    'access arguments' => array('administer Cart Scheduler'),
    'type' => MENU_NORMAL_ITEM,
  );
...
}

and defined those 2 links 'edit' - 'delete' on each row of my table like this:

function cart_scheduler_holidays_page(){
$holidays= db_query("SELECT a.* ....
while ($data = db_fetch_object($holidays)){
$ops = array(
      l(t('edit'), 'admin/store/Cart_scheduler/Holidays/'. $data->id .'/edit'),
      l(t('delete'), 'admin/store/Cart_scheduler/Holidays/'. $data->id .'/delete')
   }
}...

and my add-edit form is:

function cart_scheduler_holidays_add_form(&$form_state,$holidays =NULL) {
if (!empty($holidays)) {
    $form['id'] = array('#type' => 'hidden', '#value' => $holidays->id);
    drupal_set_title(t('Edit holidays: %ide', array('%ide' => $holidays->id)));
}
$form['starting_day'] = array(
    '#type' => 'textfield',
    '#title' => t('Beginning day'),
    '#description' => t('Beginning day'),
    '#default_value' => !empty($holidays)? $holidays->starting_day : 1,
    '#required' => TRUE,
  );
etc
  }

But when I'm clicking on edit it sucks ! It stays on same page .
I'm pretty newbie in Drupal Dev and I dont understand how 'page arguments' works.
in uc_attribute module, this params displays :

$items['admin/store/attributes/add'] = array(
   ...
    'page callback' => 'drupal_get_form',
    'page arguments' => array('uc_attribute_form'),

$items['admin/store/attributes/%uc_attribute/edit'] = array (
    'page callback' => 'drupal_get_form',  
    'page arguments' => array('uc_attribute_form', 3),

What is this little '3' ? Is it in relation with the fact that the same form called, switch in edit mode rather than add mode ? It seems that is no test against it in the
function uc_attribute_form($form_state, $attribute = NULL)
I've strictly copy their code but mine sucks!
Did I miss some param, something else ?
Didnt find any help nowhere about those '%, form, edit, add' and so on...
:-)

Comments

artatum’s picture

I've seen that the 3 stands for the % !
So it should be

$items['admin/store/attributes/%uc_attribute/edit']
   'page arguments' => array('uc_attribute_form', 4),

Well, but it still sucks.
I've also tried to add a '&' at $form_state :

function cart_scheduler_holidays_add_form(&$form_state,
but nada from rien of nothing works.
To be more precise, my function cart_scheduler_holidays_add_form is never called, as I've got a dpm() at the entrance which is never displayed...

Drave Robber’s picture

What is this little '3' ?

Path component, counted 0,1,2,3...
0 = admin
1 = store
etc.

In case of admin/store/Cart_scheduler/Holidays/%id/edit, %id is indeed 4.

Drave Robber’s picture

Actually, I'm at the moment solving a pretty similar task in this sandbox module. It has a table of 'existing entries', 'Add' form above it (I don't think it deserves a separate page), 'Delete' buttons on every row, and right now I'm about to get some more coffee and add edit links.

artatum’s picture

Could you see any reason in my code why my form isn't called ?

Drave Robber’s picture

$items['admin/store/Cart_scheduler/Holidays/%id/edit'] = array(:
- %id wildcard assumes you have id_load() function somewhere around (see this); I usually do simply whatever/%/edit which simply passes whatever is in place of % to the page callback function.

'page arguments' => array('cart_scheduler_holidays_add_form'),
- here it will probably be:
'page arguments' => array('cart_scheduler_holidays_add_form', 4), regardless of whether you're passing a simple argument or a wildcard.

artatum’s picture

I LOVE YOU DRAVE
Gee. It's been about 8h I was suffering!
Thank you so much, you saved my life.
Do you want to marry me ?

Drave Robber’s picture

LOL, I'm old, straight and never quite understood French humour. :D

On a side note, I just uploaded new update for my Comment whitelist module – some tricks in it might be useful for you, like creating multiple buttons with one form function and passing values around behind the scenes with '#type' => 'hidden',.

artatum’s picture

I'm gonna check it out soon.
But at last could you tell me how I can get the index of a select list in a form ?
I display a list of monthes but would like to record the int index of the choice, not the string 'january'
is it
$form_state['values']['starting_month']['index']
:-(
or ...?
PS How do you know I'm French ?
Is it my accent ?
PPS I dont find Comment whitelist module ?

Drave Robber’s picture

Define the form like this:

$form['month'] = array(
  '#type' => 'select',
  '#options' => array('1' => 'January', '2' => 'February', '3' => 'March'),
  '#default_value' => date('n'), // Current month.
  '#title' => t('Month'),
  );

Strings are used for labels, but values are numeric. (well, actually both are strings, but it's PHP :)

PS: Name + timezone used in your code (the other thread)
PPS: here it is: http://drupal.org/sandbox/DraveRobber/1174150; (doesn't a link appear on my profile page?)
if you navigate to View commits -> (last commit id – 9550803 in this case) -> snapshot, you can get the module in .tar.gz without messing with git.

artatum’s picture

I see for the form. But my problem is when writing the form in db, in _submit.
How can I retrieve the index of the month selected?
is it simply $form_state['values']['month'] ?
aint this returning the string?

Drave Robber’s picture

No, this returns the key.

artatum’s picture

Cool.
I have also this request to formulate :
How display form items inline ?

Drave Robber’s picture

AFAIK it's done at CSS level, at least for labels.

artatum’s picture

Ok.
My module is pretty out now.
Thank you again for your precious helping hand ;-)
A.

artatum’s picture

Hello DR
(I dunno which one is your first name : (Drave === Dave ? || Robber === Robert ? ) ;-)

Could you give me your advice for the best IDE for Drupal ? (I need mainly breakpoints, step-by-step, syntax completion...)
I'm wondering if you use Zend Studio, or Quickstart etc ? (i've tried to set up both, but none is working :-(

And also, do you know way for searching only in the dev forum ?

Drave Robber’s picture

– none of these :)
– haven't ever been using any of these – I write in gedit, the built-in text editor of Ubuntu, and test on localhost; but then, I'm old enough to have written HTML in notepad, too;
– no, and I don't think there is a simple way.

Not much use of me on Monday morning. :D

artatum’s picture

Where do you write?

Drave Robber’s picture

This: http://en.wikipedia.org/wiki/Gedit

(I didn't know it has a Windows port, too)

artatum’s picture

I meant where do you live ?
lol

Drave Robber’s picture

Riga, Latvia (the latter part is in my profile, and this is a small country anyway).