[SOLVED][Drupal Errors] A function got declared inside of another function

| | 1 min read

Many Drupal programmers have received a function got declared inside of another function error when writing code for Drupal. If you are facing the same situation when coding for Drupal read on to know the fix.

Here is the solution.

  • Obviously this error occurred when a function go declared inside another function as shown in the sample code below.
    function listing_form_alter($form_id, &$form){
      if($form_id == 'listing_node_form'){
        //do stuff
      }
    
    function listing_form(){
    //do stuff
    }//end listing_form
    
    }//end listing_form_alter
  • To fix it you need to move the function outside of the container function as shown below.
    function listing_form_alter($form_id, &$form){
      if($form_id == 'listing_node_form'){
       //do stuff
      }
    }//end listing_form_alter
    
    function listing_form(){
    //do stuff
    }//end listing_form

Hope that helps.

The easiest way to solve a Drupal issue is to hand it to the Drupal experts. We can provide a wide range of Drupal services to help you maintain and manage your Drupal websites. Get in touch with us to know more.