0

I have a specific block that has some form logic, intended for newly registered users. And I am a bit lost on what the logic would look like to render the contents for the form inside a hook_block_view. I've tried using a theme_form() call to get errors, mostly string offset and fatal errors from type comparison. I've tried using drupal_get_form, with the same results.

Here's the code below. Thoughts?

/**
 * Implements hook_block_info
 */
function example_module_block_info() {
  $block['item_select'] = array(
    'info' => t('Select Item'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );

  return $block;
}

/**
 * Implements hook_block_view()
 */
function example_module_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case "item_select":
      $block['subject'] = t("Select a book");
      $block['content'] = theme_form('example_module_form_select_item_form');
      break;
  }

  return $block;
}

/**
 * Implements an example form
 */
function example_module_form_select_item_form($form, $form_state) {
  $form['item_type'] = array(
    "#type" => 'select',
    "#title" => t("Make a selection"),
    "options" => array(
      "1" => t("This"),
      "2" => t("That"),
      "3" => t("The Other"),
      "4" => t("Banana")
    ),
  );

  $form["submit"] = array(
    '#type' => 'submit',
    '#value' => t("Save Selection"),
  );

  return $form;
}
1

2 Answers 2

0

Try making a function using hook_form() and calling it in your $block['content'] property?

$block['content'] = example_module_form($node);
0

You should be able to do something like:

/**
 * Implements hook_block_view().
 */
function example_module_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case "item_select":
      $form = drupal_get_form('example_module_form_select_item_form');
      $block['content'] = drupal_render($form);
      $block['subject'] = t("Select a book");
      break;
  }

  return $block;
}

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.