I would like to set cron function in my custom module. I have created one form which is called with submit button from UI. Now I would like to submit/call that form from another page (.module file) where I have written my hook_cron function, how it would be possible?

In Drupal 7, I use

$form_state['values'] = "";
drupal_form_submit('customfeedimport_main_pagecallback_submit', $form_state);

But I am new to Drupal 8, so how to perform this cron thing in Drupal 8?

Also what should be included in the namespace, use?

1 Answer 1


You can do it with the FormBuilder::submitForm() function.

To register a new user (taken from the docs):

// register a new user
$form_state = new FormState();
$values['name'] = 'robo-user';
$values['mail'] = '[email protected]';
$values['pass']['pass1'] = 'password';
$values['pass']['pass2'] = 'password';
$values['op'] = t('Create new account');
\Drupal::formBuilder()->submitForm('user_register_form', $form_state);

You might have to include the namespace for the FormState class before this code if not done already.

  • Hello AjitS, I have tried but it's giving me an error. Can you please provide me the full code with a namespace, so I can get know better.
    – Jaydeep
    Jan 5, 2018 at 6:35

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.