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?