One more tips that are not well documented in Drupal 8 API. In this tutorial i'll go though with creating a custom module which by default create a new page that simply embeding node creation form
Create the module files
1- Create a new folder in /module directory and name it custom_module
2- Create custom_module.info.yml file with the following definition
name: 'Our custom module' description: 'However !' core: 8.x type: module
3- Add custom_module.routing.yml with the following details
custom_module_page: path: 'new/article' defaults: _controller: '\Drupal\custom_module\Controller\customModuleArticleController::content' requirements: _permission: 'access content'
4- create the following file & folders inside our module src/Controller/customModuleArticleController.php with the following code
<?php /** * @file * Contains \Drupal\custom_module\Controller\customModuleArticleController. */ namespace Drupal\custom_module\Controller ; use Drupal\Core\Controller\ControllerBase ; use Symfony\Component\HttpFoundation\Request; use Drupal\Component\Utility\Html ; class customModuleArticleController extends ControllerBase { public function content(Request $request) { $type = node_type_load("article"); // replace this with the node type in which we need to display the form for $node = $this->entityManager()->getStorage('node')->create(array( 'type' => $type->id(), )); // OPTIONAL - Set default values for node fields $node->set('field_age', "32") ; $node_create_form = $this->entityFormBuilder()->getForm($node); return array( '#type' => 'markup', '#markup' => render($node_create_form), ); } }
Now, you can create new article from
"new/article" instead of "node/add/article"
Easy right !
Comments
How to embedd EntityForms in
How to embedd EntityForms in a custom Block?
I followed your tutorial and it works like a charm. (y) However When I created a custom block, I am able to embedd custom forms to it. However this method of adding entityforms in custom block doesn't work. Kindly guide.
I did this to add my custom forms into a custom block.
$form = \Drupal::formBuilder()->getForm('\Drupal\custom\Form\MycustomForm');
You Are awsome
It worked and i am so glad with this solution just what i needed
You are saved my time
Working fine.
Add new comment