Sign up ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have dynamically created some blocks depending on how many entries I have in the database:

    function my_module_block_info() {
        $result_set = db_select('my_products', 'products')
            ->fields('products', array('pid', 'name'))
            ->execute();

        $blocks = array();

        foreach($result_set as $product) {
            $blocks['my_product_' . $product->pid] = array(
              'info' => ('My Product ' . $product->name)
            );
        }

        return $blocks;
    }

    function my_module_block_view($delta = '') {
        $pieces = explode('_', $delta);
        $pid = $pieces[2];
        $product = db_select('my_products', 'products')
            ->fields('products')
            ->condition('products.pid', $pid, '=')
            ->execute()
            ->fetchObject();

        $block['subject'] = $product->name;
        $block['content'] = my_module_block_content($product);
        return $block;
    }

function my_module_block_content(&$product) {
    $form = drupal_get_form('my_module_block_form', $product);

    $output = '<p>' . $product->price . '</p>';

    $output .= drupal_render($form);

    return $output;
}

function my_module_block_form($form, &$formstate, $product) {
    $form['submit_product] = array(
      '#type' => 'image_button',
      '#button_type' => 'submit',
      '#src' => base_path() . path_to_theme() . '/images/buy.gif',
    );
    $form['product_name'] = array(
      '#type' => 'hidden',
      '#value' => $product->name,
);
    return $form;
}

My problem is that when I have 2 blocks with different products on one page, the submit function for the second block displayed has the value of $form['product_name']['#value'] equal to the first block, which is an entirely different product. What am I doing wrong?

share|improve this question

2 Answers 2

I would suggest to prefix the form array with your product ID (I guess $product->pid or something like this) :

function my_module_block_form($form, &$formstate, $product) {
    $form[$product->pid]['submit_product] = array(
      '#type' => 'image_button',
      '#button_type' => 'submit',
      '#src' => base_path() . path_to_theme() . '/images/buy.gif',
    );
    $form[$product->pid]['product_name'] = array(
      '#type' => 'hidden',
      '#value' => $product->name,
    );
    return $form;
}

Then the fields shouldn't be overwritten.

share|improve this answer
    
Didn't do it for me, sorry. I've tried to put a #value too but it still didn't do it. Any more ideas? –  Revan Darth Jan 17 '14 at 9:41

I found a solution here: Multiple forms on single page?. The problem is drupal assigns the same id to forms because they are using the same function. To overide the id, simply call to a function that ddosen't exist and have a simple hook_form redirect it like so:

function my_module_forms($form_id, $args) {
    $forms = array();

    if(substr($form_id, 0, 10) == 'my_module_') {
        $forms[$form_id] = array(
          'callback' => 'my_module_block_form',
        );
    }

    return $forms;
}
share|improve this answer

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.