0
我已經在屬性鏈接內容中創建了自定義窗體。 我已經創建了一個名爲「架構‘block_contents’,當此鏈接的用戶點擊,它會如果數據庫中存在的數據編輯的形式,或者它會創建一個新條目... 這裏我的代碼:如何在窗體中編輯圖像字段drupal 7
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 !!'));
}
現在我想編輯此表單..如何在編輯時管理圖像字段?它應該顯示我上傳圖片時編輯..也可以有人建議我編輯此表格正確? 任何想法?
與managed_file,在數據庫中的狀態保持爲0和URI作爲「臨時://flowers.jpg」,如何管理呢?並在形式上可以是圖像的'#default_value'? – Amb 2013-04-10 04:59:15
如果您在表單域中傳遞正確的參數圖像將不會被設置爲臨時的,請在鏈接中進一步解釋:http://pastebin.com/SGrt9aMP – arseniew 2013-04-10 08:51:15
好的謝謝.... :) – Amb 2013-04-10 09:47:57