0

I created a form_node_alter for a content type called "banner" and I made a custom submit handler named "mymodule_check_submit_handler()". Everything is ok, I can do what I want in the submit handler function but I don't know how to create the node of type "banner" once I executed my custom code.

Searching for a solution I found this code:

$values = $form_state->getValues();
$node = entity_create('node', $values);
$node->save();

But now I got this error: Error: Call to a member function getValues() on array in mymodule_check_submit_handler()

How can I create the node in my custom submit handler?

2
  • What is your Drupal version ?
    – guilhom
    Jul 10, 2017 at 7:17
  • I might misunderstand, but if you need to check the provided data (as your function name implies) wouldn't you be better off adding a custom validation callback and just leaving the regular submit? Would seem like a cleaner solution to me. Jul 10, 2017 at 8:08

1 Answer 1

0

After some searches I post the solution to myself!

This is the right code to use:

$values = $form_state['values'];
$node = entity_create('node', $values);
$node = node_submit($node); // Prepare node for saving
node_save($node);

Your Answer

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

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