I created a custom "node add" & "node edit" forms, everything work well except with ajax fields (I noticed the issue with file fields and image fields).
When I try to upload files on my custom forms (either add or edit ones), I get several errors and the file is not uploaded.
My form is declared this way (in hook_menu):
$items['cms/content/add'] = array(
'title' => 'Content Add',
'page callback' => 'cms_content_add',
'access callback' => TRUE,
'file' => 'krown_admin_cms_content_add.inc',
'type' => MENU_CALLBACK,
);
$items['cms/content/add/*'] = array(
'title' => 'Content Add',
'page callback' => 'cms_content_add',
'access callback' => TRUE,
'file' => 'krown_admin_cms_content_add.inc',
'type' => MENU_CALLBACK,
);
And the function that is responsable for the output:
function cms_content_add(){
$type = arg(3);
module_load_include('inc', 'node', 'node.pages');
$node_form = new stdClass;
$node_form->type = $type;
$form = drupal_get_form($type.'_node_form',$node_form);
$form['#attributes']['class'] = 'form-horizontal form-row-seperated row';
$output = drupal_render($form);
return $output;
}
Is there something I am missing? Everything works well, except these ajax file (image) fields. I mean, I can save and/or update any content except files.
Please help!!
/***************** EDIT *****************/
I just noticed actually the issue is not completely related with file fields. Actually when I just attach a file (without pressing upload), and just press "save" to save the node, everything works like a charm.
The issue occurs only when clicking on "upload" button. So, I guess this must be related with "ajax" feature more than "file fields".
/***************** EDIT 2 *****************/
To answer the @Neograph734 question, I want to improve Drupal node edit/add forms, because I want a better experience for my clients. I built a CMS module which abstracts all the complexity of Drupal to let the clients just modify the editorial content easily without pain.
My goal is to be able to configure easily in which block the fields will be placed (I have 4 different blocks, as you can see below)
This is what my custom node edit form looks like.
Below you can see the basic Drupal node edit form.
For that purpose, I set up customs node edit/add forms, so I can easily configure where node fields will be placed on this layout (in 4 block I set up).
Each of them has a specific admin interface where I can set up which fields will go in which block. Here is what this interface look like for the first block for example.
Then, in my custom node edit form, I will check against this config where the field must go. For example the image fields "Image (Listing)" & "Image (Détail)" will go to the block number one. I managed that this way:
function cms_content_edit_form(){
$before = microtime(true);
$krown_admin_settings_fields_block1 = variable_get('krown_admin_settings_fields_block1',1);
$krown_admin_settings_fields_block2 = variable_get('krown_admin_settings_fields_block2',1);
$krown_admin_settings_fields_block3 = variable_get('krown_admin_settings_fields_block3',1);
$krown_admin_settings_fields_block4 = variable_get('krown_admin_settings_fields_block4',1);
$nid = arg(3);
$node = node_load($nid);
module_load_include('inc', 'node', 'node.pages');
$form = drupal_get_form($node->type.'_node_form', $node);
$column1 = array(
'#type' => 'fieldset',
'#title' => t('Column 1'),
'#collapsible' => false,
'#collapsed' => false,
'#tree' => TRUE,
'#children' => '',
'#attributes' => array('class' => array('col-md-8')),
'#theme_wrappers' => array('fieldset_custom2'),
);
$column2 = array(
'#type' => 'fieldset',
'#title' => t('Column 2'),
'#collapsible' => false,
'#collapsed' => false,
'#tree' => TRUE,
'#children' => '',
'#attributes' => array('class' => array('col-md-4')),
'#theme_wrappers' => array('fieldset_custom2'),
'#prefix' => '',
'#suffix' => '<div class="clearfix"></div>',
);
/**** Block 1 ****/
if($krown_admin_settings_fields_block1!=1){
$block1 = array(
'#type' => 'fieldset',
'#title' => t('Block 1'),
'#collapsible' => false,
'#collapsed' => false,
'#tree' => TRUE,
'#children' => '',
'#theme_wrappers' => array('fieldset_custom3'),
);
$is_there_a_field_attached_block1 = false;
foreach ($krown_admin_settings_fields_block1 as $key => $field) {
if(isset($form[$field['type']])){
if($is_there_a_field_attached_block1==false){
$is_there_a_field_attached_block1 = true;
}
$block1[$field['type']] = $form[$field['type']];
unset($form[$field['type']]);
}
}
if($is_there_a_field_attached_block1==true){
$form['block1'] = $block1;
$form['block1']['#weight'] = '-100';
}
}
/**** Block 2 ****/
if($krown_admin_settings_fields_block2!=1){
$block2 = array(
'#type' => 'fieldset',
'#title' => t('Block 2'),
'#collapsible' => false,
'#collapsed' => false,
'#tree' => TRUE,
'#children' => '',
'#theme_wrappers' => array('fieldset_custom3'),
);
$is_there_a_field_attached_block2 = false;
foreach ($krown_admin_settings_fields_block2 as $key => $field) {
if(isset($form[$field['type']])){
if($is_there_a_field_attached_block2==false){
$is_there_a_field_attached_block2 = true;
}
$block2[$field['type']] = $form[$field['type']];
unset($form[$field['type']]);
}
}
if($is_there_a_field_attached_block2==true){
$form['block2'] = $block2;
$form['block2']['#weight'] = '-99';
}
}
/**** Block 3 ****/
if($krown_admin_settings_fields_block3!=1){
$block3 = array(
'#type' => 'fieldset',
'#title' => t('Block 3'),
'#collapsible' => false,
'#collapsed' => false,
'#tree' => TRUE,
'#children' => '',
'#theme_wrappers' => array('fieldset_custom3'),
);
$is_there_a_field_attached_block3 = false;
foreach ($krown_admin_settings_fields_block3 as $key => $field) {
if(isset($form[$field['type']])){
if($is_there_a_field_attached_block3==false){
$is_there_a_field_attached_block3 = true;
}
$block3[$field['type']] = $form[$field['type']];
unset($form[$field['type']]);
}
}
if($is_there_a_field_attached_block2==true){
$form['block3'] = $block3;
$form['block3']['#weight'] = '-98';
}
}
/**** Block 4 ****/
if($krown_admin_settings_fields_block4!=1){
$block4 = array(
'#type' => 'fieldset',
'#title' => t('Block 4'),
'#collapsible' => false,
'#collapsed' => false,
'#tree' => TRUE,
'#children' => '',
'#theme_wrappers' => array('fieldset_custom3'),
);
$is_there_a_field_attached_block4 = false;
foreach ($krown_admin_settings_fields_block4 as $key => $field) {
if(isset($form[$field['type']])){
if($is_there_a_field_attached_block4==false){
$is_there_a_field_attached_block4 = true;
}
$block4[$field['type']] = $form[$field['type']];
unset($form[$field['type']]);
}
}
if($is_there_a_field_attached_block2==true){
$form['block4'] = $block4;
$form['block4']['#weight'] = '-97';
}
}
$form['column1'] = $column1;
$form['column2'] = $column2;
if(isset($form['block1'])){
$form['column1']['block1'] = $form['block1'];
unset($form['block1']);
}
if(isset($form['block2'])){
$form['column1']['block2'] = $form['block2'];
unset($form['block2']);
}
if(isset($form['block3'])){
$form['column1']['block3'] = $form['block3'];
unset($form['block3']);
}
if(isset($form['block4'])){
$form['column2']['block4'] = $form['block4'];
unset($form['block4']);
}
$block1['title'] = $form['title'];
unset($form['title']);
unset($form['translation']);
unset($form['language']);
unset($form['field_vcard']);
unset($form['field_krown_weight']);
$form['#attributes']['class'] = 'form-horizontal form-row-seperated row';
$output = drupal_render($form);
$after = microtime(true);
dpm(($after-$before) . " sec\n");
return $output;
}