0

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 = \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

  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.

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. 
2
  • 2
    Do you know that any entity can have multiple "form modes"? Just like in frontend, a node can have a "teaser" and a "full" view mode, in backend a node can have a default "edit" form mode showing all input fields and a custom "only_firstname" form mode with only one input field. – Hudri Feb 23 at 15:56
0

An entity can have multiple form modes, so you can exclude some fields in different form modes and leave fields that you want to display.

So, you can create form mode "custom_form_mode", go to manage fields, choose "custom_form_mode" and arrange fields.

In the custom code, you can use the code that you mention in #1, but with an extra argument in the getForm method, which is $operation, and that one is the form mode that you want to use to display form.

$node = \Drupal\node\Entity\Node::create(['type' => 'my_students']);
$form = \Drupal::service('entity.form_builder')->getForm($node, 'custom_form_mode');
return $form
4
  • Very thanks for response. Actually I am working on it right now and I see and error. I will update my question and insert my code – newbe Feb 24 at 9:15
  • It was my bad, sorry. Operation is the operation, like Edit, Delete, Register, etc. That's not the argument related to form mode. Instead, can you please try to use the approach as it's described here api.drupal.org/api/drupal/core%21includes%21entity.inc/function/…? – euphoricMV Feb 25 at 9:25
  • actually it works, i have several forms on my page, but right now i try to disable redirection because when i click submit related to one form, it's redirecting me to summary page. And i've spend few hours on that so far, so maybe this approach is not good though – newbe Feb 25 at 10:58
  • i followed drupal.stackexchange.com/questions/163626/… and stopped form redirect :) – newbe Feb 25 at 12:55

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.