3

We use core drupal's Poll module where admin can add options to the polls by clicking "Add option" & at the same time they can remove the options by clicking "Remove option"

Here's our problem,

On Second Submit of AHAH Form by clicking "Add options" we are getting these warnings:

warning: array_shift() [function.array-shift]: The argument should be an array in ...

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback....

Can some one suggest the possible changes to be done to avoid these errors.

We are using Authcache module with APC . The environment consists of 3 servers. each running its own apc instance. and the servers are behind a load balancer.

Module file

function custom_apachesolr_search_init() {
  module_load_include('inc', 'node', 'node.pages');
}

function custom_apachesolr_search_menu() {
$items['admin/settings/apachesolr/urlboosting'] = array(
    'title'            => 'Url boosting',
    'page callback'    => 'custom_apachesolr_url_boosting',
    'access arguments' => array('administer search'),
    'weight'           => 2,
    'type'             => MENU_LOCAL_TASK,
    'file'             => 'custom_apachesolr_search.admin.inc',
  );
$items['custom_apachesolr_search/vocabulary/callback'] = array(
   'page callback'    => 'custom_apachesolr_search_vocabulary_callback',
   'access callback'  => TRUE,
   'file'          => 'custom_apachesolr_search.admin.inc',
   'type'              => MENU_CALLBACK,
 );
  
}
function custom_apachesolr_search_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'custom_apachesolr_search_urlboost_form':
      $form['#cache'] = FALSE;    
      ahah_helper_register($form, $form_state); 
      break;
  }
}

custom_apachesolr_search.admin.inc

function custom_apachesolr_search_synonyms_form(&$form_state) {
    
    $form['apachesolr_search_synonyms_settings']['addonemore'] = array(
      '#type' => 'submit',
      '#size' => 30,
      '#value' => t('Add one more..'),
      '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
      '#weight' => 2,
      '#submit' => array(''), // If no javascript action.
      '#ahah' => array(
        'path' => 'custom_apachesolr_search/vocabulary/callback',
        'wrapper' => 'more-textboxes',
        'method' => 'replace',
        'effect' => 'fade', 
      ),
    );
    $form['apachesolr_search_synonyms_settings']['removeone'] = array(
      '#type' => 'submit',
      '#size' => 30,
      '#value' => t('Remove one..'),
      '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
      '#weight' => 3,
      '#submit' => array(''), // If no javascript action.
      '#ahah' => array(
        'path' => 'custom_apachesolr_search/vocabulary/callback',
        'wrapper' => 'more-textboxes',
        'method' => 'replace',
        'effect' => 'fade', 
      ),
    );   
}


function custom_apachesolr_search_urlboost_form(&$form_state)
{
  cache_clear_all('form_'. $_POST['form_build_id'], 'cache_form');
  //On form load, take the values from the table in to an array.
  if(!isset($form_state['values']))
  {
    // Check for deleted content that wasn't deleted from the index.
    $result = db_query("SELECT casu.urlid,casu.url,casu.score FROM {custom_apachesolr_search_url_boost} casu");
    $urllist = array();
    $edit_content = array();
    while ($urllist = db_fetch_array($result)) {
      $edit_content[][$urllist['url']] = $urllist['score'].','.$urllist['urlid'];
    }
  }
  /*print "<pre>";
  print_r($edit_content);
  print "</pre>";*/
  drupal_add_css(drupal_get_path('module','custom_apachesolr_search').'/css/custom_apachesolr_search.css');
  try {
    $i=0;
    $num_txtboxes = 1;
    if(count($edit_content)>0)
    {
      $num_txtboxes = count($edit_content);
    }
    $txtcnt = 0;
    if(isset($form_state['values']["url$i"]))
    {
      while(isset($form_state['values']["url$i"]))
      {
        ++$i;
        ++$txtcnt;
      }
      $num_txtboxes = $txtcnt;
      if($form_state['clicked_button']['#id']=="edit-addonemore")
      {
        $num_txtboxes += 1;
      }
      if($form_state['clicked_button']['#id']=="edit-removeone")
      {
        $num_txtboxes -= 1;
      }
    }
    else if($form_state['clicked_button']['#id']=="edit-removeone")
    {
      $num_txtboxes = 0;
    }
    else if($form_state['clicked_button']['#id']=="edit-addonemore")
    {
      $edit_content = array();
      $num_txtboxes = 1;
    }
    $form['#action'] = base_path() .'custom_apachesolr_search/urlboost/callback';
    $form['apachesolr_search_urlboost_settings'] = array(
      '#type' => 'fieldset',
      '#title' => t(''),
      '#prefix' => '<div id="urlboostblk">',
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#suffix' => '</div>',
    );
    
    // Container for just the textboxes.
    $form['apachesolr_search_urlboost_settings']['more-textboxes'] = array(
      '#prefix' => '<div id="more-textboxes">',
      '#suffix' => '</div>',
      '#type' => 'fieldset',
    );
    $form['apachesolr_search_urlboost_settings']['more-textboxes']['urltitle'] = array(
      '#type' => 'item',
      '#prefix' => '<div class="container-inline"><div id="urltitle">',
      '#description' => t("URL Pattern"),
      '#suffix'  => '</div>',
    );
    $form['apachesolr_search_urlboost_settings']['more-textboxes']['scoretitle'] = array(
      '#type' => 'item',
      '#description' => t("Score"),
      '#suffix' => '</div>',
    );
    $form['apachesolr_search_urlboost_settings']['addonemore'] = array(
      '#type' => 'submit',
      '#size' => 30,
      '#value' => t('Add one more..'),
      '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
      '#weight' => 2,
      '#submit' => array(''), // If no javascript action.
      '#ahah' => array(
        'path' => 'custom_apachesolr_search/urlboost/callback',
        'wrapper' => 'more-textboxes',
        'method' => 'replace',
        'effect' => 'fade', 
      ),
    );
    $form['apachesolr_search_urlboost_settings']['removeone'] = array(
      '#type' => 'submit',
      '#size' => 30,
      '#value' => t('Remove one..'),
      '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
      '#weight' => 3,
      '#submit' => array(''), // If no javascript action.
      '#ahah' => array(
        'path' => 'custom_apachesolr_search/urlboost/callback',
        'wrapper' => 'more-textboxes',
        'method' => 'replace',
        'effect' => 'fade', 
      ),
    );   
    $weights = drupal_map_assoc(array('21.0', '13.0', '8.0', '5.0', '3.0', '2.0', '1.0', '0.8', '0.5', '0.3', '0.2', '0.1'));
    for ($i=0; $i<$num_txtboxes; $i++) {
      $key = '';
      if(isset($edit_content[$i]))
      {
        $key = key($edit_content[$i]);
        $edit = explode(',',$edit_content[$i][$key]);
      }
      
      
      $form['apachesolr_search_urlboost_settings']['more-textboxes']["url$i"] = array(
        '#type' => 'textfield',
        '#prefix' => '<div class="container-inline">',
        '#size' => 54,
        '#description' => t(""),
        '#default_value' => $key,
      );
      $edit[0] = ( strpos($edit[0],'.') == false ) ? $edit[0] = $edit[0].'.0' : $edit[0];
      $form['apachesolr_search_urlboost_settings']['more-textboxes']["score$i"] = array(
        '#type' => 'select',
        '#description' => t(""),
        '#options' => $weights,
        '#suffix' => '</div>',
        '#default_value' => $edit[0],
      );
      $form['apachesolr_search_urlboost_settings']['more-textboxes']["urlid$i"] = array(
        '#type' => 'hidden',
        '#description' => t(""),
        '#value' => $edit[1],
      );
      
      if (isset($form_state['values']["url$i"])) {
        $form['apachesolr_search_urlboost_settings']['more-textboxes']["url$i"]['#value'] = $form_state['values']["url$i"];
      }
      if (isset($form_state['values']["score$i"])) {
        $form['apachesolr_search_urlboost_settings']['more-textboxes']["score$i"]['#value'] = $form_state['values']["score$i"];
      }
      if (isset($form_state['values']["urlid$i"])) {
        $form['apachesolr_search_urlboost_settings']['more-textboxes']["urlid$i"]['#value'] = $form_state['values']["urlid$i"];
      }
    }
    
    $form['apachesolr_search_urlboost_settings']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 4,
    );
    
    $form['apachesolr_search_urlboost_settings']['submit_message'] = array(
      '#type' => 'value',
      '#value' => t('The Scores were saved.'),
    );
    
    $form['apachesolr_search_urlboost_settings']['note'] = array(
      '#type' => 'item',
      '#value' => t('Note:Please enter the url pattern like http://www.dentalproductsreport.com/subscribe.'),
      '#weight' => 6,
    );
    //ahah_helper_register($form, $form_state);
  }
  catch (Exception $e) {
    watchdog('Custom Apachesolr Search', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
    drupal_set_message(nl2br(check_plain($e->getMessage())), "warning");
  }
  return $form;
}

function custom_apachesolr_search_urlboost_form_submit($form_id, &$form_state)
{
  $content = '';
  drupal_set_message($form_state['values']['submit_message'], 'status');
  $terms = $form_state['values'];
  $i=0;
  if (!empty($terms)) {
    db_query("DELETE FROM {custom_apachesolr_search_url_boost} ");
  }
    
  foreach($terms as $key => $value)
  {
    if($key == "url$i")
    {
      $urls[] = trim($value);
    }
    if($key == 'score'.$i)
    {
      if($i+1==count($urls))
      {
        $scores[] = trim($value);        
      }
    }
    if($key == 'urlid'.$i)
    {
      if($i+1==count($scores))
      {
        $result = db_result(db_query("SELECT casu.urlid FROM {custom_apachesolr_search_url_boost} casu WHERE casu.urlid=%d",$value));
        if($urls[$i]!=''){
          if($result>0)
          {
            db_query("UPDATE {custom_apachesolr_search_url_boost} SET url = '%s', score = %f, changed = %d WHERE urlid = %d", $urls[$i], $scores[$i], time(),$value);        
          }
          else
          {
            db_query("INSERT INTO {custom_apachesolr_search_url_boost} (url, score, changed) VALUES  ('%s', %f, %d)", $urls[$i], $scores[$i], time());
          }
        }
        /*else
        {
          db_query("DELETE FROM {custom_apachesolr_search_url_boost} WHERE urlid = %d",$value);
          echo "DELETE FROM {custom_apachesolr_search_url_boost} WHERE urlid =".$value;
        }*/
        $i++;
      }
    }
  }
}

/**
 * Callback for autocheckboxes. Process the form with the number of checkboxes
 * we want to provide.
 */
function custom_apachesolr_search_urlboost_callback() {
  include_once 'modules/node/node.pages.inc';
  $form = custom_apachesolr_search_urlboost_callback_helper();
    
  $checkboxes = $form['apachesolr_search_urlboost_settings']['more-textboxes'];
  
  // Remove the wrapper so we don't double it up.
  unset($checkboxes['#prefix'], $checkboxes['#suffix']);

  $output = theme('status_messages');  
  $output .= drupal_render($checkboxes);
 
  // Final rendering callback.
  print drupal_json(array('status' => TRUE, 'data' => $output));
  exit();
}



/**
 * Does the very standard things that must be done in any normal callback.ahah_helper_path
 * Used by each callback in this example module.
 */
function custom_apachesolr_search_urlboost_callback_helper() {
  $form_state = array('storage' => NULL, 'submitted' => FALSE);
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);
  $args = (array) $form['#parameters'];
  $form_id = array_shift($args);
  $form_state['post'] = $form['#post'] = $_POST;
  $form['#programmed'] = $form['#redirect'] = FALSE;
  drupal_process_form($form_id, $form, $form_state);
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  return $form;
}
4
  • Error messages always provide line numbers, aren't they? Could you post a full message with file and line number and somehow mark that line in code? (I prefer commented out number at the beginning, like /*#78*/, but anything will do)
    – Mołot
    Oct 17, 2013 at 10:49
  • warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /var/www/html/drupal/includes/form.inc on line 377.
    – Littleman
    Oct 17, 2013 at 10:53
  • There is no menu item 'custom_apachesolr_search/vocabulary/callback' which is attached to your ahah. Please update your question with updated code. Oct 17, 2013 at 12:07
  • @JayendraKainthola : missed to add the menu item.. added them now.
    – Littleman
    Oct 18, 2013 at 6:17

1 Answer 1

0

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback

The warning above basically means you've defined callback function, but it doesn't exist.

For example you should have the functions associated with these page callbacks:

  • 'page callback' => 'custom_apachesolr_url_boosting',
  • 'page callback' => 'custom_apachesolr_search_vocabulary_callback',

but based on your provided information, these callbacks does not exist.

So make sure these functions does exist and file which have them defined is included. The function name should be named exactly as you have defined in page callback parameter in hook_menu.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.