0

Fatal error: Class 'Drupal\module_name\DOMDocument' not found in /var/www/drupal/modules/module_name/src/form_xml.php on line 359

Any idea why Drupal cannot find DOMDocument class?

I want to execute a code when a form is submitted

  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Find out what was submitted.
    $form_field = $form_state->getValue('content_types');

    foreach ($form_field as $key => $value){    
      if (!empty($value)){  
        $return = File::generate(); 
      }
    }
  }

In case you are wondering, module_name is the name of my custom module.

File::generate() is a static method which contains the DOMDocument class

The code works fine when I use devel to execute it, so any idea why can't it find the DOMDocument class on form submit?

Seems to me as if the form blocks access to the DOMDocument class.

7
  • Did you check namespace?
    – Trupal
    Commented Aug 30, 2016 at 18:31
  • 2
    Because you're not referring to it properly - needs to be \DOMDocument with the leading slash, or php will look in the current namespace
    – Clive
    Commented Aug 30, 2016 at 18:34
  • @Clive thanks for the suggestion, I tried changing it to $xml = new \DOMDocument(); but no luck.
    – No Sssweat
    Commented Aug 30, 2016 at 18:58
  • Is that definitely what's on line 359? If you add another empty line above that one, does the line in the error message change to 360? And did the error message change when you added the slash?
    – Clive
    Commented Aug 30, 2016 at 19:03
  • 1
    The error message definitely didn't change at all when you added the slash? Not to Fatal error: Class \DOMDocument' not found?
    – Clive
    Commented Aug 30, 2016 at 19:34

1 Answer 1

1

You need to rename the form class file CamelCase and make a namespace declaration at the top. The namespace will be Drupal\module_name\Form. Place the form class file in directory module_name/src/Form/.

0

Your Answer

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

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