1

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?

2 Answers 2

0

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.

1
  • 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? Commented Jan 17, 2014 at 9:41
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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