How can I insert fields from custom content to custom form?

I have a problem with form. I had to create a custom content with fields. User can put variables into those fields and save to database, for example “Anna” into column field_student_name in table node__field_student_name. I see this connection where every field from this content type is saved under particular node.

Now I need to create a form with fields, which values will go to the same database tables, for example in this form I have a form field where I put “Tom”, and this value is saved in table node__field_student_name in column field_student_name (the same as Anna in custom content).

At this moment I see two main potential ways to accomplish this task:

  1. insert form from custom content to my new form with entity.form_builder

I added this two lines info code:

$node = DrupalnodeEntityNode::create(['type' => 'my_students']);
$form = Drupal::service('entity.form_builder')->getForm($node);
return $form

And this works fine, but I need only part of those fields 🙁 And I don’t wanna see section for admin like “add new revision” etc. Moreover, “Submit” button is on the top of the page

  1. Create a form and in submit function do something like

    $node->set(‘title’, ‘titile1’)

but I prefer way #1. Besides that, I don’t know how to do it with way #2. I don’t know how can i put variables from $form[‘student]’ into field_student_name column in node__field_student_name table. Moreover i am not sure if it is ok and if nodes id etc will be added in correct way.

To sum up, I think way #1 is better and easier but I don’t have such a big knowledge to do this and maybe here are more advanced, helpful people who can help me.


Go to Source of this post
Author Of this post: newbe
Title Of post: How can I insert fields from custom content to custom form?
Author Link: {authorlink}