Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I trying to do add more functionality for adding video urls which I am able to do by using Ajax callback.I am referring a drupal examples for it.It has one remove one button which removes last created text field from form. But I want a remove button for each text-field to remove it.

I am creating textfields and remove button in form hook like

for ($i=0; $i < $form_state['num_names']; $i++) {
        $form['names_fieldset'][$i]['title'][$i] = array(
            '#type' => 'textfield',
            '#title' => t('Video Title'),
            '#default_value' => $webinar_videos[$i]->title,
        );
        $form['names_fieldset'][$i]['url'][$i] = array(
            '#type' => 'textfield',
            '#title' => t('Video Url'),
            '#default_value' => $webinar_videos[$i]->video_url,
        );
        $form['names_fieldset'][$i]['remove'][$i] = array(
            '#type' => 'submit',
            '#value' => t('Remove'),
            '#submit' => array('webinar_add_more_remove_one'),
            '#validate' =>false,
            '#ajax' => array(
                'callback' => 'webinar_add_more_callback',
                'wrapper' => 'names-fieldset-wrapper',
            ),
        );
}

Now a 'webinar_add_more_remove_one' submit callback is like

function webinar_add_more_remove_one($form, &$form_state) {

    if ($form_state['num_names'] > 1) {
        $form_state['num_names']--;
    }
    $form_state['rebuild'] = TRUE;
}

and 'webinar_add_more_callback' this callback returns a wrapper element like

function webinar_add_more_callback($form, $form_state) {
    return $form['names_fieldset'];
}

My question is How I can pass a video id to particular remove submit button so that I can delete particular video in 'webinar_add_more_remove_one' callback?

Thank you in advance

Any help appreciated.

share|improve this question
add comment

1 Answer 1

Last time I had to do this I used this method:

When creating the form create an array to hold deleted ids:

$form_state['deleted_items'] = array();

And give each remove button an ID relating to that items index:

$form['names_fieldset'][$i]['remove'][$i] = array(
    '#type' => 'submit',
    '#value' => t('Remove'),
    '#submit' => array('webinar_add_more_remove_one'),
    '#validate' =>false,
    '#ajax' => array(
        'callback' => 'webinar_add_more_callback',
        'wrapper' => 'names-fieldset-wrapper',
    ),
    '#attributes' => array(
        'id' => 'item-'.$i,
    ),
);

Then in webinar_add_more_remove_one check the id of the button that was pressed:

function webinar_add_more_remove_one($form, &$form_state) {
    $form_state['deleted_items'][] = str_replace('item-', '', $form_state['clicked_button']['#attributes']['id']);
    $form_state['rebuild'] = TRUE;
}

And then when building the form check the array of deleted items:

for ($i=0; $i < $form_state['num_names']; $i++) {

    if(in_array($i, $form_state['deleted_items']) continue;

    // rest of code....
}

You will also need to check the deleted item array in the submit and validate functions as we don't actually removed the deleted item from the $form_state array.

share|improve this answer
    
Hey thank you for your answer,I did exactly same as you mentioned,but form is get refresh when I am clicking on any remove button and if print $form_state['deleted_items'] I am getting blank. –  Swapnil Patil Apr 22 at 10:42
    
Make sure you are always passing $form_state by reference (with an & before it). –  Felix Eve Apr 22 at 10:48
    
pass by reference was missing only for 'webinar_add_more_callback' so i given it but still whole page is get refreshed means not behaving like ajax –  Swapnil Patil Apr 22 at 10:57
    
Yes, this will only work with ajax requests. If you refresh the whole page then you will lose the whole of $form_state. –  Felix Eve Apr 22 at 11:00
    
Yes,I mean when I am clicking on any remove button then page get refreshed.I am not refreshing it manually. –  Swapnil Patil Apr 22 at 11:05
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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