0

I'm having problems placing a block in my form. This is what I have now:

I have a register form and a fb button block. I say the block must be visible only when page is equal to user register page.

But the problem is that I don't want to have the form and underneath the block. I would like to add the block before the submit button of my form. How can I do this?

1

You can do something like this:

  1. load the block content manually
  2. control the order of fields withj #weight option

example:

function my_form()
{
  $block = block_load(MODULENAME, BLOCKDELTA);
  $block = _block_render_blocks(array($block));
  $block_build = _block_get_renderable_array($block);

  $form['FIELD_1'] = array(
    ...
    ...
    '#weight' => 1
  );

  $form['FIELD_2'] = array(
    ...
    ...
    '#weight' => 2
  );

  $form['FIELD_3'] = array(
    '#prefix' => '<div class="SOME_CLASS">',
    '#suffix' => '<div>',
    '#markup' => render($block_build),
    '#weight' => 3
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'OK',
    '#weight' => 4
  );

  return $form;
}

if those forms are from other modules you can alter them with hook_form_alter(&$form, &$form_state, $form_id) function.

example:

function MYMODULE_OR_THEME_form_alter(&$form, &$form_state, $form_id) {
  // dpm($form_id);
  if ('user_register_form' == $form_id) {
    $form['actions']['#weight'] = 50;

    $block = block_load(MODULENAME, BLOCKDELTA);
    $block = _block_render_blocks(array($block));
    $block_build = _block_get_renderable_array($block);

    $form['fb_field'] = array(
      '#prefix' => '<div class="SOME_CLASS">',
      '#suffix' => '<div>',
      '#markup' => render($block_build),
      '#weight' => 49
    );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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