Search code examples
phpdrupal-7drupal-modulesdrupal-hooks

how do i use hook_block_view or something else to generate block view for my custom form module in drupal 7


I have created a form using hook_form() in drupal 7, it can be accessed as a page for which i used hook_menu(), I need the form to be used as a block.Here is my code

  function contact_form_form($form, &$form_state) {  //     

 $form['name'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Name',
'#size' => 20,
'#maxlength' => 15,
'#required' => TRUE, //make this field required 
 );


 $form['mobileNo'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'Mobile Number',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required 
);

 $form['email'] = array(
'#title' => 'Email id',
'#type' => 'textfield' ,
'#required' => TRUE ,
 );

 $form['address'] = array(
'#title' => 'Address',
'#type' => 'textarea',
'#cols' => 60,
'#resizable' => TRUE,
'#rows' => 5,
'#required' => TRUE ,
  );   


 $form['image'] = array(
'#type' => 'file',
'#name' => 'custom_content_block_image',
'#title' => t('Block image'),
'#size' => 40,
'#description' => t("Upload a image.Only JPG, JPEG, PNG & GIF files are allowed."),
'#upload_location' => 'public://'
 ); 

 $form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
  );

 return $form;
 }

// the hook_submit() which i have used

   function contact_form_form_submit($form, &$form_state) {

   $target_dir = "uploads/";
   $target_file = $target_dir . basename($_FILES["custom_content_block_image"]["name"]);

   drupal_set_message('this is inside submit   
  drupal_set_message('this is inside submit 
  $fileName = $_FILES['custom_content_block_image']['name'];
  $tmpName  = $_FILES['custom_content_block_image']['tmp_name'];
  $fileSize = $_FILES['custom_content_block_image']['size'];
  $fileType = $_FILES['custom_content_block_image']['type'];

  $fp      = fopen($tmpName, 'r'); 
  $content = fread($fp, filesize($tmpName));
  $content = addslashes($content);
  fclose($fp);

  if(!get_magic_quotes_gpc())
  {
    $fileName = addslashes($fileName);
  }

inserting all the acquired data in the drupals database created inside .install file

      try {
     db_insert('contact_us')
  ->fields(array(
   'name' => $form_state['values']['name'],
   'mobile_no' => $form_state['values']['mobileNo'], 
   'email' => $form_state['values']['email'],
   'address' => $form_state['values']['address'],
   'image' => $content,
   'imageName' => $fileName,
   'imageSize' => $fileSize, 
   'imageType' => $fileType       
 ))
 ->execute();
 }
  catch (PDOException $e) {
  //print_r($e->getMessage());
  drupal_set_message('this is after db_insert <pre>'.print_r($e->getMessage(),true).'</pre>');
 }

}


Solution

  • Something like this:

    function contact_form_block_info() {
      $blocks['contact-form-block'] = array(
        'info' => t('My Block'),
        'cache' => DRUPAL_NO_CACHE
      );
    
      return $blocks;
    }
    
    function contact_form_block_view($delta = '') {
      $block = array();
    
      switch ($delta) {
        case 'contact-form-block':
          $block['subject'] = t('Contact Form Block Subject');
          $block['content'] = drupal_render(drupal_get_form('contact_form'));
      }
    
      return $block;
    }