R
R
Roman2019-04-10 00:29:58
Drupal
Roman, 2019-04-10 00:29:58

Drupal 8, how to get user ID?

Programmatically created a form.
After filling out and validating the form, you need to find out the user ID in order to add data to the database.
I don't understand how to do this.
global $user doesn't work. Writes that there is no such object
global $user called in

public function submitForm(array &$form, FormStateInterface $form_state) {
        global $user;
        $account = $user->uid;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
afi13, 2019-04-10
@gagablik

You can use
But it's better to use DI rather than a direct call to the Drupal class, you can read more about this in the documentation:
https://www.drupal.org/docs/8/api/services-and-dep...
https: //www.drupal.org/docs/8/api/services-and-dep...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question