1

I am using the following code include a node creation form.

namespace Drupal\fritr_register_friterie\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class RegisterFriterieForm.
 *
 * @package Drupal\fritr_register_friterie\Form
 */
class RegisterFriterieForm extends FormBase {

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'register_friterie_form';
    }

    /**
     * {@inheritDoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $entity = \Drupal::entityManager()->getStorage('node')->create(array(
            'type' => node_type_load('friterie')->id(),
        ));
        // OPTIONAL - Set default values for node fields
        // $entity->set('field_machine_name', "value");
        $node_create_form = \Drupal::service('entity.form_builder')->getForm($entity);

        // I can display it with this method, but I don't know how to delete
        // some fields.
//        $form['friterie'] = [
//            '#type' => 'container',
//            'form' => $node_create_form
//        ];
        $form['friterie'] = array(
            '#type' => 'markup',
            '#markup' => render($node_create_form),
            '#prefix' => '<div id="friterie-form">',
            '#suffix' => '</div>'
        );
        return $form;
    }

    /**
     * {@inheritDoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {}

}

I get the following error.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Drupal\fritr_register_friterie\Form\RegisterFriterieForm' does not have a method 'processForm' in Drupal\Core\Form\FormBuilder->doBuildForm() (line 979 of core/lib/Drupal/Core/Form/FormBuilder.php).

I can fix it by changing $form['friterie'] to a markup type; with the markup I then use render($node_create_form). When I want to alter my form with hook_form_alter() it's a string and I cannot modify the form.

  • That is only one method; we need to see the full class you are using, and that contains that method. – kiamlaluno Jun 9 '16 at 15:23
  • i updated my post but the error is that i have no processForm function in my class but i dont know what is this method and what to write in... I started drupal 8 tuesday (there is a big lack of tutorials on the net...) In D7 i used field_form_attach() method to include node and user form and this was a great function. Why in D8 it's so hard... – Kpone Jun 9 '16 at 15:28
  • I addressed this issue in the answer for your original question. The short answer is to unset the #process attribute from the subform. – Eyal Jun 9 '16 at 19:01
  • 1
    The long answer is that when the form builder renders a form element it assumes it represents the main form object. In my answer I moved the #process array to the form state object and added a #process callback of my own that calls the ones saves in the form state. – Eyal Jun 9 '16 at 19:06
  • Thanks, it's very interesting, i would like know more. How did you get so much knowlegde with D8 ? Have you follow some web lessons ? – Kpone Jun 10 '16 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.