簡體   English   中英

我如何使用hook_block_view或其他東西為drupal 7中的自定義表單模塊生成塊視圖

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

我在drupal 7中使用hook_form()創建了一個表單,它可以作為我使用hook_menu()的頁面進行訪問,我需要將表單用作塊。這是我的代碼

  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;
 }

//我使用過的hook_submit()

   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);
  }

將所有獲取的數據插入.install文件中創建的drupals數據庫中

      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>');
 }

}

像這樣的東西:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM