Skip to content
Advertisement

Drupal 8 or 9 Save data to the content type from a custom module

I am using drupal 8.9.*, I want to Save data to the content type from a custom module so that i can see the data entered in Drupal’s common content list page.

I dont know how to save data to the content type, I have tried using object of $node to save it (and some other method I tried but later came to know its deprecated). I have also passed machine-name of the content type. Where I am going wrong on this, all the Drupal documentation is distorted, hard to find a correct version of 8 || 9. Here is my code.

Routing data file, birth_details.routing.yml

JavaScript

My form BirthDetails.php, (hard coded some values for testing purpose)

JavaScript

and finally machine name of the custom content type is birth_data, i have used the same for form unique id and node create type type

Advertisement

Answer

Baikho‘s answer is nice. It’s still better to avoid the use of static calls (such as Node::create()) ; we can easily do this injecting dependencies in the constructor.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement