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:
- insert form from custom content to my new form with entity.form_builder
I added this two lines info code:
$node = \Drupal\node\Entity\Node::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
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.
EDIT
I've create new mode student_user in student content type:
core.entity_view_mode.node.student_user.yml
langcode: en
status: true
dependencies:
module:
- node
id: node.student_user
label: student
targetEntityType: node
cache: true
core.entity_view_display.node.student.student_user.yml
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.node.student_user
- field.field.node.student.field_name
- node.type.student
module:
- options
- user
id: node.student.student_user
targetEntityType: node
bundle: student
mode: student_user
content:
field_name:
weight: 103
label: above
settings:
thousand_separator: ''
prefix_suffix: true
third_party_settings: { }
type: list_default
region: content
hidden:
links: true
and here i tried with routing
student.student_user: path: '/test' defaults: _entity_view: 'node.student_user' _title: 'Students' requirements: _permission: 'access content' _entity_access: 'node.view'
I received an error
RuntimeException: Failed to find entity of type node in route named student.student_user
And then i tried with form
i added core.entity_form_display.node.student.student_form.yml and core.entity_form_mode.node.student_form.yml
and then i tried return it in controller
<?php
namespace Drupal\student\Controller;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Routing;
class TestController extends ControllerBase {
function test() {
$node = \Drupal\node\Entity\Node::create(['type' => 'student']);
$form = \Drupal::service('entity.form_builder')->getForm($node, 'student_form');
return $form;
}
}
?>
error
Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: The "node" entity type did not specify a "student_form" form class.