3

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?

3

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'] = 'robouser@example.com';
$values['pass']['pass1'] = 'password';
$values['pass']['pass2'] = 'password';
$values['op'] = t('Create new account');
$form_state->setValues($values);
\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 '18 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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