0

I have an AJAX form that replaces a section of itself successfully when the user is logged in. But when logged out, the demo-area section comes back with the #prefix and #suffix but no item-header or item-display. $files is an array of my objects that create the custom-themed sections successfully. I can't figure out what is causing them to return empty in AJAX when logged out.

I think permissions are fine. When logged in, the initial form load and AJAX replacements work as intended. When logged out, the demo-area loads correctly with the page, but is then replaced with only #prefix/#suffix on AJAX calls.

Here is the part of my form builder function in question:

$files = array();
foreach ($node->field_file[$node->language] as $field_file) {
  $files[] = new ClientDemoItem($field_file);
}

$form['file-index'] = array(
  '#type' => 'select',
  '#options' => array_keys($files),
  '#default_value' => 0,
  '#ajax' => array(
    'callback' => 'client_demo_load_item',
    'wrapper' => 'client-demo-area',
  ),
);

$file_index = isset($form_state['values']['file-index'])? $form_state['values']['file-index'] : 0;

$form['demo-area'] = array(
  '#prefix' => '<div id="client-demo-area">',
  'item-header' => $files[$file_index]->buildHeader(),
  'item-display' => $files[$file_index]->buildDisplay(),
  '#suffix' => '</div>',
);

Here is the simple AJAX callback:

/*
 * Replace the demo area with the requested file display
 */
function client_demo_load_item($form, $form_state) {
  return $form['demo-area'];
}

Here is what the buildHeader() function does:

  public function buildheader() {

    $item = array(
      '#theme' => 'client_demo_item_header',
      '#url' => file_create_url($this->uri),
      '#description' => $this->description,
      '#filename' => $this->filename,
      '#filesize' => filesize($path),
      '#timestamp' => $this->timestamp,
      '#fid' => $this->fid,
      '#item_file' => $this,
    );

    return $item;
  }
  • 1
    Please provide code where $files are initialized. – Eugene Fidelin Oct 23 '12 at 9:48
  • Please also provide the code for your ajax callback (client_demo_load_item), and maybe even the buildHeader() and buildDisplay() methods, or at least sample return values. – Chaulky Oct 23 '12 at 20:47
  • Use X-Debug. I guess your $files array is empty during the ajax request but can't tell without more information. – jantimon Oct 23 '12 at 21:29
  • Thanks for looking! I have added the $files code and client_demo_load_item() callback. buildHeader() and buildDisplay() return render arrays with a custom template based on the file type. They must be okay because the initial page load always works. I also think that something is preventing $files from being built during AJAX when logged out. I'll trace it. – Joe Beuckman Oct 24 '12 at 1:21
  • I traced $files during AJAX and it looks very similar logged in or out. It might be relevant that the admin theme (Seven) shows when logged in with the toolbar showing. When logged out, I might be missing the Javascript necessary to complete the AJAX. – Joe Beuckman Oct 24 '12 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.