0

Here is my code

<?php
namespace Drupal\mymodule\Controller;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Controller\ControllerBase;
class HelloController extends ControllerBase
{
  public function content() {

       $form = \Drupal::formBuilder()->getForm('\Drupal\commerce_product\Form\ProductForm');
}
}

I want to render the default add to product page(product/add/default) in my custom module controller file .

Error: Call to a member function getEntityTypeId() on null in Drupal\Core\Entity\EntityForm->getBaseFormId() (line 79 of core/lib/Drupal/Core/Entity/EntityForm.php).

1
0

You need to create an empty product and use entity.form_builder service to generate the form.
Try the following.

<?php

namespace Drupal\mymodule\Controller;

use Drupal\commerce_product\Entity\Product;
use Drupal\Core\Controller\ControllerBase;

class HelloController extends ControllerBase {
  public function content() {
   $product = Product::create([
     'type' => 'default',
   ]);
  return  \Drupal::service('entity.form_builder')->getForm($product);
  }
}
3
  • Thanks @berramou – Gagan Shukla Oct 26 '19 at 15:13
  • This code work for me public function content(Request $request) { $type = node_type_load("products"); $p = \Drupal\commerce_product\Entity\Product::create([ 'type' => 'default', ]); return \Drupal::service('entity.form_builder')->getForm($p); – Gagan Shukla Oct 26 '19 at 15:14
  • You have set type to default so what is the use of $type = node_type_load("products"); – berramou Oct 27 '19 at 13:13

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.