Я создал форму с помощью hook_form () в drupal 7, к ней можно обращаться как к странице, для которой я использовал 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);
}
вставка всех полученных данных в базу данных drupals, созданную в файле .install
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;
}
Других решений пока нет …