0

I try to input several forms into one page via Controller. With code which is under, I see those forms but after submit one of them, nothing happens. Any of data is send to database. Has anyone idea how to fix this?

<?php

namespace Drupal\Core\Entity\ContentEntityForm;
namespace Drupal\student\Controller;
use Drupal\node\Entity\Node;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\node\NodeInterface;
use Drupal\node\Entity;
use Drupal\node\NodeForm;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Routing;
use Drupal\Core\Entity\ContentEntityForm;

class SFormController extends ControllerBase {

  function new_form() { 
    
    $build = [];
  
    foreach ($stud as $id => $name) {
      
      $node = \Drupal\node\Entity\Node::create(['type' => 'student']);
      $form = \Drupal::service('entity.form_builder')->getForm($node, 'student_form');
      
      $build[] = $form;
    }   
    return $build;
  }
}

?>
2
  • Do you mean multiple versions of the same form on a single page?
    – Jaypan
    Feb 24, 2021 at 18:27
  • yes, exactly. After page inspect i see they have different id
    – newbe
    Feb 24, 2021 at 18:45

1 Answer 1

1

You need to make the form ID dynamic, so that it is different for each instance on the page. As you're extending EntityForm, you'll have to extend it, then register your form on the node class, and finally call your form class.

  1. Extend the entity form:

    class MyEntityForm extends EntityForm {
      public function getFormId() {
        // The dynamic form ID allows for the form to be
        // reused on a page.
        return parent::getFormId() . '-' . $this->getEntity()->id();
      }
    }
    
  1. Register your form class on the node entity type:

    /**
     * Implements hook_entity_type_alter().
     */
    function HOOK_entity_type_alter(array &$entity_types) {
      $node = &$entity_types['node'];
      $node->setFormClass('my_form_type', 'Drupal\[MODULE]\Form\MyEntityForm');
    }
    
  2. Call your form from your controller:

      $form = \Drupal::service('entity.form_builder')->getForm($node, 'my_form_type');
    
15
  • And can i add this to Controller? Or should I change controller to form and create function build() instead of custom newForm() like above?
    – newbe
    Feb 24, 2021 at 18:58
  • You add this to your custom form. It's not related to the controller.
    – Jaypan
    Feb 24, 2021 at 19:00
  • Looking at your code, I can see you are passing an unsaved node to the form. This means that the entity ID will return null, and the code above as it stands will not work as all instance of the form will still have the same form ID. You will need to decide how the forms can be uniquely identified on your site, and return a dynamic form ID accordingly.
    – Jaypan
    Feb 24, 2021 at 19:03
  • Are You able to show the most correct way? And I am not sure if i understood you correctly with "You add this to your custom form. It's not related to the controller". Should I change everything to be a Form, and not controller?
    – newbe
    Feb 24, 2021 at 19:11
  • Ahh, I see, it's not a form you've created. I've updated my response accordingly.
    – Jaypan
    Feb 24, 2021 at 19:27

Your Answer

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

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