Invoke function from module in form I'm trying to port the OpenID module from Drupal 7 to Drupal 8.

I have a form OpenIdLoginForm.php with the following code.

&lt;?php
...
namespace Drupal\openid\Form;
use Drupal\user\Form\UserLoginForm;
use Drupal\Core\Form\FormStateInterface
use Drupal\Core\Url;
use Drupal\user\UserStorageInterface
...
class OpenIdLoginForm extends UserLoginForm {
... // getFormId, buildForm methods work nicely

  public function validateForm(array &amp;$form, FormStateInterface $form_state) {
    ... // some validations
    openid_begin($claimed_id, $return_to, $form_state);
  }

... // submitForm method
}


openid_begin is defined in openid.module.

&lt;?php

use Drupal\Core\Routing\RouteMatchInterface;

... // openid_help, working nicely

function openid_begin($claimed_id, $return_to = '', $form_state = null) { 
  // some stuff
}


The folder structure is the following.

modules/
  openid/
    README.md
    openid.module
    ...
    src/
      Form/
        OpenIdLoginForm.php
        ...


Whenever I submit the form, and validateForm() is called, I get an error:


  PHP Fatal error:  Call to undefined function Drupal\openid\Form\openid_begin() in /modules/openid/src/Form/OpenIdLoginForm.php


What am I doing wrong? How would I invoke a module function from a form belonging to the same module?

Fun fact: invoking the function user_login_finalize() from submitForm() in OpenIdLoginForm.php works and I don't know why.
I was looking for a solution for hours. Right after posting the question, i found one:

calling

\Drupal::moduleHandler()-&gt;invoke('openid','begin',array($openid,$return_to,$form_state));


seems to do the trick.

Still, I wonder why I can call user_login_finalize directly.