Whenever a required field is left out (such as the Title field), neither the .js file or the .css file is loaded. I'm not terribly experienced with hook_form_alter... I don't think this function runs during a validation error... maybe the files should be attached using a different hook?

Any ideas?

Comments

jessehs’s picture

I found a workaround here by adding a validation function. See http://drupal.org/node/322290 for a discussion on this topic. I'm hacking the module here and adding a validation function. Just paste these two functions in place of the current taxonomy_tree_select_form_alter() function.

/**
 * Implementation of hook_form_alter().
 */
function taxonomy_tree_select_form_alter(&$form, &$form_state, $form_id) {
  global $theme;
  // Work on node form...
  if ($form['#id'] == 'node-form' && $form['taxonomy']) {
    foreach ($form['taxonomy'] as $key => $vocabulary) {
      $tree_select = variable_get('vocabulary_' . $key . '_tree_select', 0);
      if (is_array($vocabulary) && $tree_select) {
        $safe_terms = _taxonomy_tree_select_taxonomy_format_tree(taxonomy_get_tree($key), 0);

        // Prepare one vocabulary to be passed to javascript
        $treeded_vocabularies[$key] = array(
          'id' => $key,
          'terms' => $safe_terms,
          'multiple' => $vocabulary['#multiple']
        );

        // Add an custom class based on theme in use
        $treeded_vocabularies[$key]['classes'][] = 'taxonomy-tree-' . $theme;
      }
    }

    // If is there some vocabulary to be added into javascript setting
    if (count($treeded_vocabularies)) {
      $javascript_settings['taxonomyTreeSelect'] = $treeded_vocabularies;
	  // add the validation function here, after we've already determined that we need the js files
 	  $form['#validate'][] = 'taxonomy_tree_form_validate';
      drupal_add_js($javascript_settings, 'setting');
      drupal_add_js(drupal_get_path('module', 'taxonomy_tree_select') . '/taxonomy_tree_select.js');
      drupal_add_css(drupal_get_path('module', 'taxonomy_tree_select') . '/taxonomy_tree_select.css');
    }

  }
}

function taxonomy_tree_form_validate($form, &$form_state) {
  if ($form['#id'] == 'node-form' && $form['taxonomy']) {
    foreach ($form['taxonomy'] as $key => $vocabulary) {
      $tree_select = variable_get('vocabulary_' . $key . '_tree_select', 0);
      if (is_array($vocabulary) && $tree_select) {
        $safe_terms = _taxonomy_tree_select_taxonomy_format_tree(taxonomy_get_tree($key), 0);

        // Prepare one vocabulary to be passed to javascript
        $treeded_vocabularies[$key] = array(
          'id' => $key,
          'terms' => $safe_terms,
          'multiple' => $vocabulary['#multiple']
        );

        // Add an custom class based on theme in use
        $treeded_vocabularies[$key]['classes'][] = 'taxonomy-tree-' . $theme;
      }
    }

    // If is there some vocabulary to be added into javascript setting
    if (count($treeded_vocabularies)) {
      $javascript_settings['taxonomyTreeSelect'] = $treeded_vocabularies;
      drupal_add_js($javascript_settings, 'setting');
      drupal_add_js(drupal_get_path('module', 'taxonomy_tree_select') . '/taxonomy_tree_select.js');
      drupal_add_css(drupal_get_path('module', 'taxonomy_tree_select') . '/taxonomy_tree_select.css');
    }

  }
}

I realize this is a bit redundant. I'd like to know if there's a more concise way of doing this...

danillonunes’s picture

Status: Active » Fixed

jessehs, thanks for the report.

I fix it by using an #after_build function that is also loaded with an validation error page.

Just wait for the next release :-)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

cableman0408’s picture

I used the #after_build method to add js/css and it worked perfect (thanks). Just remember to return the form from the after build function.

pshahmumbai’s picture

I readded the same javascript code to the form validation function itself.

function someform_form_validate($form, &$form_state)
{
.....
drupal_add_js($some_js_code, 'inline', 'header');
}

jphelan’s picture

Using form attach also solves this.

$form['#attached']['js'] = array(
  drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);

danillonunes’s picture

jphelan, this method works for Drupal 7 only, this module is for Drupal 6.

Also this issue was already fixed by the commit: http://drupalcode.org/project/taxonomy_tree_select.git/commit/780ba62. If you’re are still facing the bug with the latest version of the module, feel free to reopen this issue. :)

merlin06’s picture

Issue summary: View changes

Don't use the code in #6 as it will replace all other js attached to $form.

You want to use something like this instead:
$form['#attached']['js'][] = drupal_get_path('module', 'ajax_example') . '/ajax_example.js';

graham leach’s picture

Drupal 7 Ubercart 3 site.

hook_init() - NOPE
hook_form_alter() - NOPE

This worked:

// Add CSS & JS directly into form 
// Because hook_init() and hook_form_alter() DO NOT HELP
// If there is a form validation error -> form rebuild
$form['#attached'] = array(
    'css' => array(
      drupal_get_path('module', 'pets') . '/css/pets.css',
      drupal_get_path('module', 'pets') . '/css/jquery-ui.css',
    ),
    'js' => array(
      drupal_get_path('module', 'pets') . '/js/jquery.min.js',
      drupal_get_path('module', 'pets') . '/js/jquery-ui.min.js',
      drupal_get_path('module', 'pets') . '/js/datepicker-init.js',
      drupal_get_path('module', 'pets') . '/js/confirm-image-button-label.js',
      drupal_get_path('module', 'pets') . '/js/image-preview.js',
    ),
  );

  // Add heading
  $form['heading'] = array(
    '#markup' => '<h2>' . t('New Pet') . '</h2>',
  );