我已经在一个区块中创建了一个表单,第一次出现但它没有再次出现,我不知道为什么会发生这种情况,因为我对drupal很新。
<?php
function my_module_block_info() {
$blocks = array();
$blocks['my_block'] = array(
'info' => t('My Custom Block'),
);
return $blocks;
}
function hook_block_view($delta = '') {
switch ($delta) {
case 'my_block':
$block['subject'] = t('My form');
$block['content'] = my_module_my_form();
}
}
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'acess arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
return $items;
}
function my_module_form() {
return drupal_get_form('my_module_my_form');
}
function my_module_my_form($form) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('username'),
'#required' => TRUE,
'#description' => "enter your username.",
'#size' => 20,
'#maxlength' => 20,
);
$form['picture'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'update your profile',
);
return $form;
}
function my_module_my_form_submit($form, &$form_state) {
global $user;
$account = user_load($user -> uid);
$image_path = 'picture://';
$validators = array(
'file_validate_is_image' => array(),
);
$file = new StdClass();
$file -> uid = $user -> uid;
$file->uri = $image_path;
$file = file_save_upload('picture', $validators);
$new = array (
'name' => $form_state['values']['name'],
'picture' => $file,
);
user_save($account,$new );
$message = 'You have updated your profile';
drupal_set_message(t($message));
}
此表单正在更新用户名和用户图片,但是当我尝试在块中调用此表单作为其内容但在第一次之后它无法正常工作时。请帮助我。
答案 0 :(得分:3)
功能
存在许多问题function hook_block_view($delta = '') {
switch ($delta) {
case 'my_block':
$block['subject'] = t('My form');
$block['content'] = my_module_my_form();
}
}
my_module_block_view()
; $block['content']
应填充渲染表单,因此您需要调用my_module_form()
而不是表单定义函数(就像您在菜单挂钩中的页面回调中所做的那样); return $block;
。所以你的功能应该更像是
function my_module_block_view($delta = '') {
switch ($delta) {
case 'my_block':
$block['subject'] = t('My form');
$block['content'] = my_module_form();
break;
}
return $block;
}