Search code examples
phpdrupaldrupal-themes

Theming a form in Drupal which has to be a part of an Entity's Content


I've a form in my module (mymodule)...

function mymodule_form()
{
   $form['mytext'] = array(
     '#title' => 'Password',
     '#type' => 'password',
     '#size' => 10,
   );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Cool!',
   );
   $form['cool_submit'] = array(
     '#type' => 'submit',
     '#value' => 'Cool Submit!',
   );
   return $form;
}

I've used the hook_entity_view hook to display this form under all drupal entities that are displayed.

function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
  $entity->content['myadditionalfield'] = mymodule_form();
}

When showing this form, drupal adds a DIV tag to the mytext (password field) by itself. I want to override this and provide my own DIV tags and theme to this form. How do I do it?

Thanks :-)


Solution

  • I worked further on this question to solve it. And the problem got solved. I changed the above code...

    function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
      $element = array(
        'start' => array(
            '#type' => 'button',
            '#name' => 'start', 
            '#button_type' => 'submit',
        ),
        'my_text' => array(
            '#type' => 'textfield',
            '#size' => 30, 
            '#maxlength' => 50,
        ),
        'my_submit' => array(
            '#type' => 'button',
            '#name' => 'Submit Discussion',
            '#button_type' => 'submit',
        ),
      );
    
      $entity->content['disc_bar'] = $element;
    }
    

    And the problem kept creeping up whenever a textfield or a password field was rendered. I checked the type array in systems elements info function (a elements_info hook) and found that textfield and password fields come with a default value for the #theme-wrapper. I tried to override it by this way...

    'my_text' => array(
            '#type' => 'textfield',
            '#size' => 30, 
            '#maxlength' => 50,
            '#theme-wrapper' => '',
    ),
    

    and It worked. Its not generating any additional division tags... :-)