我在上下文链接内容中创建了自定义表单。我创建了名为“block_contents”的模式,当用户单击此链接时,如果数据库中存在数据,它将编辑表单,否则它将创建一个新条目...... 这是我的代码:
function custom_links_menu() {
$menuItems = array();
$menuItems['node/%node/block_contents'] = array(
'title' => 'Block Content',
'description' => 'Block Content',
'page callback' => 'custom_links_form',
'access arguments' => array('access content'),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE
);
return $menuItems;
}
/*
* implements hook_form()
*/
function custom_links_form() {
return drupal_get_form(arg(2));
}
function block_contents($form_state) {
$form['nid'] = array('#type' => 'hidden', '#value' => arg(1));
$form['intro_title'] = array(
'#type' => 'textfield',
'#title' => 'Inro title : ',
);
$form['intro_image'] = array(
'#type' => 'file',
'#title' => t('Intro Image'),
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
$form['intro_text'] = array(
'#type' => 'textarea',
'#title' => 'Intro Text : ',
);
$form['left_free_text_1'] = array(
'#type' => 'textarea',
'#title' => 'Left Free Text 1 : ',
);
$form['use_left_free_text'] = array(
'#type' => 'checkbox',
//'#default_value' => 1,
'#title' => 'Use Left Free Text 1 : ',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
$form['#attributes']['enctype'] = 'multipart/form-data';
return $form;
}
/*
* Implements hook_validate()
*/
function block_contents_validate($form, &$form_state) {
$file = file_save_upload('intro_image', array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('png gif jpg jpeg'),
));
if ($file) {
if ($file = file_move($file, 'public://image/')) {
$fileinfo = pathinfo($file->uri);
$form_state['values']['intro_image'] = $fileinfo['basename']; //$file->filename;
} else {
form_set_error('intro_image', t('Failed to write the uploaded file the site\'s file folder.'));
}
} else {
$form_state['values']['intro_image'] = null;
//form_set_error('intro_image', t('No file was uploaded.'));
}
}
/*
* implements hook_submit()
*/
function block_contents_submit($form, &$form_state) {
db_insert('block_contents')
->fields(array(
'nid',
'intro_title',
'intro_image',
'intro_text',
'left_free_text_1',
'use_left_free_text'
))
->values($form_state['values'])
->execute();
}
drupal_set_message(t('Data saved !!'));
}
现在我想编辑这个表单。如何在编辑时管理图像字段?它应该显示我在编辑表单时上传的图像。还有人可以建议我正确编辑这个表单吗?任何想法 ?