Skip to main content

How to create a custom module in Drupal8 PSR-4? (Hello world module in Drupal 8)

Steps to create a custom module in Drupal8. 

Step 1: File Structure.
  In Drupal8 we should keep the Custom module or contributed modules under modules folder on the root directory.

  modules/contrib/
  modules/custom/

  Note:- multisite configuration
        sites/your_site_name/modules/

  Here we have file structure, "modules/custom/example".

Step 2: Creating .info.yml 
  In Drupal8 .info files changes to .info.yml
  Here we have, example.info.yml

Step 3: Creating .routing.yml 
  Before registering the hook_menu we have to write the path in  .routing.yml file.
  In Drupal 8 we won’t need hook_menu() anymore as we make heavy use of the Symfony2 components to handle the routing. This involves defining the     routes as configuration and handling the callback in a controller (the method of a Controller class).
  Here the example.routing.yml

Step 4: Creating Route Controller Class
  We have to create our ModuleController.php according to the PSR-4 naming standard [https://www.drupal.org/node/2156625].
  Here we have file structure, "modules/custom/example/src/Controller/ExampleController.php".
ExampleController.php

Step 5: Creating .module with hook_menu()
  In Drupal 8 hook_menu() function  we have two conditions to follow.
    (1)The paths need to match with path given in ".routing.yml" exactly.
    (2)Should match the name of the route.
Here is the example.module

Finally you can go to "mypage/page", and see the "Hello world is getting printed".
Click on Download-module to download the example module in drupal 8

Comments

Popular posts from this blog

How to set the page title in Drupal 8? drupal_set_title() in Drupal 8 ? drupal_get_title() in Drupal8?

Using the following lines of code in hook_form_alter()  , able to set the title and get the title after saving the code do CLEAR CACHE drupal_set_title() in  Drupal8 $request = \ Drupal :: request (); if ( $route = $request -> attributes -> get (\ Symfony \ Cmf \ Component \ Routing \ RouteObjectInterface :: ROUTE_OBJECT )) { $route -> setDefault ( '_title' , 'New Title' ); } drupal_get_title() in Drupal8 $request = \ Drupal :: request (); if ( $route = $request -> attributes -> get (\ Symfony \ Cmf \ Component \ Routing \ RouteObjectInterface :: ROUTE_OBJECT )) { $title = \ Drupal :: service ( 'title_resolver' ) -> getTitle ( $request , $route ); }

How to create the Popup or modal in Drupal 8?

The following code will help you to create the popup in Drupal 8.  use \ Drupal \ Core \ Ajax ; function testingPopup () { $response = new AjaxResponse (); $title = $this -> t ( 'Title for the Popup.' ); $form [ '#attached' ][ 'library' ][] = 'core/drupal.dialog.ajax' ; $response -> setAttachments ( $form [ '#attached' ]); $content = '<div class="test-popup-content">' . 'Content in HTML format' . '</div>' ; $options = array ( 'dialogClass' => 'popup-dialog-class' , 'width' => '75%' , ); $modal = new OpenModalDialogCommand ( $title , $content , $options ); $response -> addCommand ( $modal ); return $response ; } You can call the function testingPopup() , you will get the popup.

How to configure Drupal7, varnish, authcache2 and ESI in Nginx server?

This is step by step procedure for, how to configure the authcache, varnih and ESI for the drupal7 project in NGINX erver. To configuring the Nginx Server use the procedure from document " http://www.geoffstratton.com/2014/03/nginx-php-fpm-apc-ssl-drupal/ " Well, make sure your nginx server is up and running. 1.Configuring the Authcache module.      Download the module from https://www.drupal.org/project/authcache    Enable only the authcache module    Go to admin/reports/status , then you can see the Authcache area is red and saying that there is no back end has been configured.   2.Configure Varnish on the back-end[nginx server].     Using the Following commands,       $ curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -     $ echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list     $ sudo apt-get update     $ sudo apt-get install varnish       Configure Varnish: