1

Here's my dilemma, I built a custom form-based module that allows a user to vote on a future conference location but I'm having a issue printing or rendering my form fields to have them display in my custom block template.

eventvoting.module

<?php
/**
 * @file
 * Title        : Drupal 7 Event Voting form in a block pattern with template file
 * Author       : Kenyon M. Johnston
 *
 * Web application form that allows site visitors to vote on what city they
 * would like to see host their favorite event next year. 
 */


 /**
 * Implmentation of hook_permission()
 */
function eventvoting_permission(){
  return array (
    'Administer event voting form' => array(
      'title' => t('Administer event voting form'),
      'description' => t('Allows users to implement custom css class for the Event Voting Form module'),
    )
  );
}

 /**
 * Implements hook_block_info().
 */
function eventvoting_block_info() {
  $block['eventvoting_block'] = array(
    'info' => t('Event Voting Form Block'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );  
  return $block;
}

/**
 * Implementation of hook_block_view()
 */
function eventvoting_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'eventvoting':
      $block['subject'] = t('');
      $block['content'] = array(
        '#theme' => 'eventvoting',
        '#vote_event_name' => variable_get('event_name'),
        '#vote_city' => variable_get('city'),
        '#vote_name' => variable_get('name'),
        '#vote_email' => variable_get('email'),
        '#vote_submit' => variable_get('submit'),
      );
    break;
  }
  return $block;
}

/*
 * Implements hook_theme
 */
function eventvoting_theme(){
  return array (
    'eventvoting' => array (
      'arguments' => array(
        'vote_event_name' => NULL,
        'vote_city' => NULL,
        'vote_name' => NULL,
        'vote_email' => NULL,
        'vote_submit' => NULL,
      ),
      // Use a template file called eventtheme.tpl.php
      'template' => 'theme/eventtheme',
    ),
  );
}

/**
 * Implements hook_menu().
*/
function eventvoting_menu() {
  $items['event-voting-form'] = array(
    'title' => 'Event Voting Form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('eventvoting_form'),
    // Use the default/standard site configuration permission.
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;    
}


/**
 * Creates/returns a form to configure the block's variables.
 *
 * @param array $form
 * @param array $form_state
 * @return array
 */
function eventvoting_form($form, &$form_state) {

  $form['event_name'] = array(
        '#title' => t('Select an event:'),
        '#type' => 'textfield',
        '#default_value' => variable_get('event_name'),
        '#required' => TRUE,
        '#weight' => 0,
    );

    $form['city'] = array(
        '#title' => t('Vote for a city:'),
        '#type' => 'textfield',
        '#default_value' => variable_get('city'),
        '#required' => TRUE,
        '#weight' => 0,
    );

    $form['name'] = array(
        '#title' => t('Your name:'),
        '#type' => 'textfield',
        '#default_value' => variable_get('name'),
        '#required' => TRUE,
        '#weight' => 0,
    );

    $form['email'] = array(
        '#title' => t('Your email:'),
        '#type' => 'textfield',
        '#default_value' => variable_get('email'),
        '#required' => TRUE,
        '#weight' => 0,
    );

    $form['save'] = array(
        '#type' => 'submit',
        '#value' => t('Vote'),
      );

    return $form;
}

**<h2><strong>eventheme.tpl.php (inside the "theme" directory)</strong></h2>**

<?php

/*
 * File         : eventtheme.tpl.php
 * Title        : Drupal 7 Event Voting Location form in a block pattern with template file
 * Author       : Kenyon M. Johnston
 *
 */

?>

<div class="event-voting-form">
    <form>  
        <h2>Vote for Next Year's Event</h2>

        <div id="vote_event_name">
            <?php print $vote_event_name; ?>
        </div>

        <div id="data-fieldset">
            <div id="vote_city">
                <?php print $vote_city ?>
            </div>
            <div id="vote_name">
                <?php print $name ?>
            </div>
            <div id="vote_email">
                <?php print $email ?>
            </div>
            <div id="vote_submit">
                <?php print $submit ?>
            </div>
        </div>

    <?php print drupal_render_children($form); ?>
    </form>
</div>

My goal is to render my fields of choice. The theme renders in the block but not the fields,

This has been racking my brain for a few hours now, any help and assistance would be GREATLY appreciated.

0

function hook_myform_form ($form, &$form_state,$user_id) {

}

function hook_BLOCK_myform_CONTENT() {
  return drupal_get_form('hook_myform_form');
}

function hook_block_info() {
  $blocks = array();

  $blocks['custom_myform_for_homepage'] = array(
    'info' => t('Details'),
  );
  return $blocks;
}

function hook_block_view($delta='') {
  $block = array();
  switch($delta) {
    case 'custom_myform_for_homepage':
      $block['subject'] = t("Details"); 
      $block['content'] = hook_BLOCK_myform_CONTENT();
      break; 
  }
  return $block;
}

Then in your theme template.php add

function yourthemename_theme($existing, $type, $theme, $path) {
    $items['hook_myform_form'] = array(
        'render element' => 'form',
        'template' => 'filename_withoutextension',
        'path' => drupal_get_path('theme', 'yourthemename') . '/nameoffolder',
    );
  return $items;
}
Now in filename_withoutextension tpl file render fields like this.
print render($form['checkboxes']);

0

You can try the code:

/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
  $block['eventvoting_block'] = array(
    'info' => t('Event Voting Form Block'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $block;
}

/**
 * Implementation of hook_block_view()
 */
function mymodule_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'eventvoting_block':
      $block['subject'] = t("Details");
      $block['content'] = mymodule_block_form();
      break;
  }
  return $block;
}

function mymodule_block_form() {
  return drupal_get_form('mymodule_build_block_form');
}

function mymodule_build_block_form($form, &$form_submit) {
  $form['name'] = array(
    '#title' => t('Your name:'),
    '#type' => 'textfield',
    '#default_value' => variable_get('name'),
    '#required' => TRUE,
    '#weight' => 0,
  );

  $form['email'] = array(
    '#title' => t('Your email:'),
    '#type' => 'textfield',
    '#default_value' => variable_get('email'),
    '#required' => TRUE,
    '#weight' => 0,
  );

  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Vote'),
  );
  $form['#theme']='eventvoting';
  return $form;
}

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'eventvoting' => [
      'render element' => 'form',
      'path' => $path . '/templates',
      'template' => 'eventvoting_form',
    ]
  ];
}

create file eventvoting_form.tpl.php in the module mymodule/templates/eventvoting_form.tpl.php print form field:

<?php print render($form['form_id'])?>
<?php print render($form['form_token'])?>
<?php print render($form['name'])?>
<?php print render($form['email'])?>
<?php print render($form['save'])?>

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.