Hi guys,

At this time, the webform_load_components() improperly caches a list of components the first time it is called.

The problem is that the first parameter, $return_all, may be set to TRUE or FALSE, but the result returned only depends on the first call, not whether $return_all is TRUE or FALSE on the current call.

In other words, you may be lucky and currently have TRUE in the very first call and it works, otherwise, I'm not too sure how it could work.

What I think you should have is one more static variable for when $return_all is set to TRUE (and use the existing ones when it is set to FALSE.) Also, the module_load_include() call is tricky here since you want to do it only once (although it does not hurt to do it twice, just a little waste of time...)

Thank you.
Alexis Wilke

/**
 * Load all necessary component.inc files into memory.
 */
function webform_load_components($return_all = FALSE, $reset = FALSE) {
  // ONLY TWO STATICS
  static $component_list, $enabled_list;

  if (!isset($component_list) || $reset) {
    $component_list = array();
    $enabled_list = array();
    $path = drupal_get_path('module', 'webform') .'/components';
    $files = file_scan_directory($path, '^.*\.inc$');
    foreach ($files as $filename => $file) {
      $enabled = variable_get('webform_enable_'. $file->name, 1);
      // $component_list[] DIFFER IF $return_all IS TRUE or FALSE
      if ($return_all || $enabled) {
        module_load_include('inc', 'webform', 'components/'. $file->name);
        $component_list[$file->name] = t($file->name);
      }
      if ($enabled) {
        $enabled_list[$file->name] = t($file->name);
      }
    }
  }

  // Ensure only wanted components are returned, even all are loaded.
  return $return_all ? $component_list : array_intersect_assoc($component_list, $enabled_list);
}

Comments

AlexisWilke’s picture

In case you don't understand what I'm saying the function should look like this instead (since I'm surprised it was not fixed yet...):

/**
* Load all necessary component.inc files into memory.
*/
function webform_load_components($return_all = FALSE, $reset = FALSE) {
  // ONLY TWO STATICS
  static $component_list, $enabled_list;

  if (!isset($component_list) || $reset) {
    $component_list = array();
    $enabled_list = array();
    $path = drupal_get_path('module', 'webform') .'/components';
    $files = file_scan_directory($path, '^.*\.inc$');
    foreach ($files as $filename => $file) {
      $enabled = variable_get('webform_enable_'. $file->name, 1);
      module_load_include('inc', 'webform', 'components/'. $file->name);
      $component_list[$file->name] = t($file->name);
      if ($enabled) {
        $enabled_list[$file->name] = t($file->name);
      }
    }
  }

  // Ensure only wanted components are returned, even all are loaded.
  return $return_all ? $component_list : array_intersect_assoc($component_list, $enabled_list);
}
quicksketch’s picture

Status: Active » Closed (won't fix)

Unless this causes a visible problem in Webform, this probably isn't going to get fixed. Webform 3 has much better loading of components (now provided through a hook rather than just loading from the includes directory). Please reopen if you feel this is important enough to be fixed in the legacy release.