0

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.

enter image description here

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.

enter image description here

Below you can see the basic Drupal node edit form.

enter image description here

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.

enter image description here

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;
    }
7
  • $items['cms/content/add/*'] should be $items['cms/content/add/%']
    – Emil Orol
    Jun 22, 2016 at 22:33
  • also check how this module drupal.org/project/subpathauto does it
    – Emil Orol
    Jun 22, 2016 at 22:38
  • Thanks for the answer, unfortunately this didn't work. I will investigate the subpathauto and check if I can fin a solution. I think it's more related with translation module. All fields work perfectly, except file based fields and the issue only occurs when pressing upload button. The error arrives after ajax callback finished.
    – 118218
    Jun 23, 2016 at 7:19
  • I somewhat fail to see what you are trying to achieve here. Drupal is already as cms, so why create a cms module to wrap the node edit form? If it is the looks of the form, you can install a new administration theme, and if you need additional fields on the form you can hook_form_alter them in. Can you share what you are attempting to achieve? There might be a better (less intrusive) solution. Jun 23, 2016 at 11:24
  • Hi @Neograph734, thanks for the answer. Actually I didn't suspect this approach was not good. I thought it was pretty easy and straight to do this like that. It works pretty well with all fields I tried, expect file based fields... If you have another approach to achieve what I am trying to do, I am of course opened to any proposition! :) Thank you
    – 118218
    Jun 23, 2016 at 14:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.