NGINX default configuration file for Drupal 8

After a couple of hours struggle with Nginx default file to run Drupal 8 production website, we found the solutions for the following problems.

  • Image style is not generating in Drupal 8 with Nginx.
  • Images are not loading with different image styles like thumbnail, medium, etc
  • access denied for the images in Drupal 8 with Nginx
  • access denied /sites/default/files/styles/thumbnail/public/pictures/  Drupal 8 nginx 

Using following default file, we are able to solve all problems addressed above.

And also added couple of tips for following questions,

How to make access rss.xml in Drupal 8 with Nginx?

      # RSS Feed.
      location = /rss.xml {
        rewrite ^ /index.php?q=rss.xml;
      }

How to make access sitemap.xml in Drupal 8 with Nginx?

      # Sitemap Feed.
      location = /sitemap.xml {
        try_files $uri /index.php?q=sitemap.xml;
      }

Understanding Block layout in Drupal theme structure

Block system in Drupal allows us to add content into regions. A block could be text or listing of content using Views.

In Drupal 7, lock options were limited.  One block can only be assigned to a single region and have very basic control of hiding and displaying it.

To overcome these limitations in Drupal 7 we had option to use Panels to add fields to blocks.

In Drupal 8, the Block system became more flexible. Biggest  improvements are - assigning single block to multiple regions and fieldable block types.

In this article, we'll show you How to work with new Drupal 8 block architecture. We'll be adding a block that will only appear in the sidebar of Article pages. Then we'll create a custom block type which will be used to create reusable promotional content. This can be added to any blog post.

A list of new things introduced in Drupal 8 block architecture -

  • Blocks in general can be used multiple times.
  • Block title has now on/off option.
  • Easier and improved UI for new blocks.
  • Configurations of block types and layout are now in code!
  • Permissions are still limited
  • and now we have fieldable block types.

Lets’ Begin!

In Drupal 7, the Block module handles placement of a block and allow you to create static blocks. In Drupal 8, it has  been separated  out into two different modules.

The Block module handles placement of the block and  "Custom Block" module implements a new entity which allows you to create block content. We can create different block types and they're fieldable.


How to create a Static block and configure it?

Step 1: Go to  Manage >> Structure >> Block Layout >> Custom block library

Block layout

  

This page has Two tab Blocks and Types. lick on +Add custom block or custom block link visible on the same page.

 

Step 2: This page is similar to Drupal 7 block creation page which has block description with body field and Revision information.

 

adding custom block

 

Fill all the field values and click on Save button.

 

Add custom block

 

Step 4: Once the block has been created you'll be redirected back to the "Custom block library" page.

At this point the block is disabled and needs to be added to a specific region. Click on the "Block layout" tab and scroll down to "Sidebar second" and click on "Place block".

 

Place block

 

Step 5: Clicking on  “place block” will redirect you to the block configuration page. Here we have title field with new enable/ disable option, which is newly introduced in Drupal 8. We still have visibility setting based on Content type, pages, roles and region assignment as it was in Drupal 7.

 

configure block

 

we have assigned our new block “About us” to Sidebar second region. We can view our custom block to sidebar second in the site.

 

block in sidebar

 

2. Custom Block Content

In Drupal 8 "Custom block" allows you to create the actual block content and custom block types. By default you get a block type called "Basic block". This type is very basic and has a single field: the Body field.

 

Step 2.1: Go to  Manage >> Structure >> Block Layout >> Custom block library >> Types

 

custom block libraray

 

Step 2.2: Clicking on ‘+Add custom block type” will enable you to create block content type. Fill up label and description, check revision if you wish to and click on Save button.

add curom block type

 

Step 2.3: Now, we can view our new Block Content type is listing under admin/structure/block/block-content/types.

Custom block library

 

Step 2.4: Next step is to adding fields to Block content type. Click on “Manage field” and add fields. This is similar to adding fields to content type.

Manage fields

 

I have added few of the fields to provide additional data to the content.

Manage fields

 

Step 2.5: Once new Block content type has been added, it will start listing on  block add page.

 

Custom block

Click on “Contact” and fill up detail.

 

Add custom contact block

 

Step 2.6: Once the block is created, it will be listed here.

 

Custom block library

 

Step 2.7: Click on your custom block and fill up the required details and save it .

filling attributes for block

 

Step 2.8: Now, it’s time to place our new block.

Go to Block layout. and click on “place block” under any of the region. In below screenshot i am assigning the custom block to “Sidebar first”

 

About us for site

 

 

Step 2.9: Proceed with basic block configuration and click on save block.

 

About us for site

 

we have completed configuration with placement of new custom block. We can view this in our web site.

 

block showcase in front page

 

How to customize the appearance of your new Drupal 8 Website

Drupal comes with inbuilt Drupal themes. Once you have installed Drupal 8 you might want to customize the website appearance. This is one of the initial processes that needs to be set while setting up Drupal. Deciding about your design early will go long way to save time and effort iterations later. Drupal 8 provides few built in Drupal themes that come up with the same package like Bartik, Stable, Seven, Stark, Classy.

 

If you don’t find any of the above in the given list then go to Drupal directory core/themes. Open any theme info.yml and change hidden: true to hidden: false.

 

Drupal 8 has a mobile-first approach. All built-in themes in Drupal 8 are responsive. It comes with an admin theme that adapts to different screen sizes, and a ‘Back To Site’ button to go back to the front page. Tables fit into any screen size, and the new admin toolbar works well on mobile devices.

 

 

Drupal 8 Appearance page

There are quite a few of Drupal themes available for Drupal 8 at https://www.drupal.org/project/project_theme . We will see more of themes being migrated from Drupal 7 to 8 within next few months.

Installing a new theme is quite simple.

1.Download a new theme.
2. Follow README or Drupal INSTALL Instruction files in the package to check theme specific instruction and steps if any.
3. Upload theme package to a new directory in the themes directory in your Drupal site.

drupal 8 installing new theme

4. Clicking appearance tab will show all themes including recently installed theme localhost/drupal-8.0.2/admin/appearance Edit your user preferences and select the new theme. If you want your new theme  to be default theme for all users, check the default box in the theme administration page.

Drupal 8 listing new theme under uninstalled section

5. Once theme is enabled, it will be listed under installed theme section.

Drupal 8 installed theme

6. To change theme setting click on  “settings” link. You will be redirected to
localhost/drupal-8.0.2/admin/appearance/settings/new_themes

In this page we have option for

TOGGLE DISPLAY
       user picture in posts (check to show user picture in post)
       user picture in comments(check to show user picture in comments)
       user verification in comments(check anonymous / authenticated user before posting any comment)
       shortcut icon()

LOGO IMAGE SETTINGS
        use the default logo supplied by the theme(admin can change the site logo)

SHORTCUT ICON SETTINGS
         use the default shortcut icon supplied by the theme (admin can change the favicon)

 

Drupal 8 theme configuration page

 

Drupal 8 newly installed theme view

 

 

 

Using the Content Type to manage content consistently

In previous blog post we have installed Drupal 8 on our system manually as well as using Drush. Drupal 8 Provides two built in content type Article and Basic page. We can use this to create pages. But most of time we need to either add fields to these content types or we just need to add new content type to organize the content better.

Probably the first change site builders will notice in Drupal 8 are the changes to content types and fields. The field changes affect not only content types, but any entity that can have fields e.g. taxonomy or user profile.

When we edit any content type, there are  some differences in the tabs compared to Drupal 7. No more comment fields and display tab. And now we have a new tab: "Manage form display."

Drupal 7 Article “edit” page

Druapl 7 article edit page

Drupal 8 Article “edit” page

Druapl 8 article edit page

 

In this article i’ll explain followings -

New fields / Adding a new field
Managing fields
Form modes in Drupal 8
Comment form configuration

New fields in Drupal 8

Let's add following five new Fields available in Article content type.

Date, E-mail, Reference, Link & Telephone

We will add these fields step by step.

Step 1.1:  Select “Date” in Add a new field. Let's add the Label as “published Date”.

Druapl 8 adding new field

Step 1.2: Add some value to Allowing no. of values under field settings tabs for limited or unlimited option. Click on Save field settings.

Drupal 8 field setting

Step 1.3: Add the help text and Default value if require. You can select either Current date or  Relative date and hit Save settings

Drupal 8 field configuration page

Now “date” field is available  under Manage Fields on Article

Drupal 8 new filed listing in Content type

Managing fields in Drupal 8

Manage fields only controls how data is structured, where we can configure each fields for our content type.
But under managed field we have an option to hide a specific field, change the widget type, perform inline field setting and save the configuration. In Drupal 8 these two actions are separate.

Drupal 8 manage form Display

Fields which had “Hidden” property would come under Disabled section.

Drupal 8 field Disabled

If we click on gear icon for any of the field item, it will open the field setting section.

Drupal 8 inline form setting

Form modes in Drupal 8:

Drupal 8 has form nodes. so if we go to structure then display mode and select form modes, we can add new form mode. For our content type we can add a new one “generic form mode” and save it.

Drupal 8 form Display

Step 3.1: Once we click on form modes we will be redirecting to entity type. As we have assigned form modes to content type, we will selecting Content

Drupal 8 choose entity type

 

Step 3.2: Now we will go back to our content types and click Manage form display.
scrolling down to bottom of the page, you will find “CUSTOM DISPLAY SETTINGS” as collapsed fieldset. Here we can select our new display form mode “generic custom mode” and save it.

 

Drupal 8 custom form Display

Step 3.3: After saving configuration you will get the drupal message with “configuration” form link on top. If you click on that, drupal will redirect to our new form Display mode “generic custom mode

 

Drupal 8 generic custom mode

This can be used for various real world use cases.  One  use case could be , people of different roles to be able to edit different fields.  we can create a form that includes the different fields for  each user roles.

Drupal 8 generic custom mode page

Comment form configuration:

In Drupal 7 we have  option to  add fields to comments on specific content types. But there was no option to add comments to entities such as users, taxonomy terms.

In Drupal 8 we can configure Comment forms independent of a specific content type. This means we can have a one comment form type for articles and another for other entity type.This also means we can reuse a comment form settings and ensure consistency across your site. Comments are added in the same way like any other fields and there is no a separate configuration tab for “Comment fields” and “Comment display”.

if you don’t want to add a Comment field to a content type, you can’t just override that per-item.

Drupal 8 adding comment as field

 

The comment form settings include:

Default value: Open, Closed, Hidden.
Comment form settings: Threads, Comments per page, Allow title, Show reply, Preview settings.

Drupal 8 comment form settings

Just now we saw, that the comment form settings are similar to the settings for any other field.

Drupal 8 provides Sitebuilders to do more out of the box. There are many excellent enhancements you'll want to know more about. You won't have to learn any crazy new concepts. The UI in Drupal 8 for managing content types and fields has been imporved a lot with great feature.

Installing Drupal with Drush, the Basics

Drush is a command-line interface that helps us to speed up administrative and development tasks for Drupal sites. After installing this Drush, we’ll be able to perform useful action simply by typing a command into a terminal —actions that would usually take multiple steps in a web browser. Drush runs on Drupal 6, 7 well as 8.

Note:  Drupal 8, works only with Drush 8.

Couple of tasks which can be done using Drush easily are :
    Download Drupal
    Download contrib modules
    Drush Install Drupal
    Update Drupal and contrib module versions
    Run updatedb
    Clear the cache
    Run cron
    Run Drupal with a lightweight webserver
    Import, export and merge configuration
    Add users and set their roles
    Add permissions to roles
    Back up and restore Drupal
    Copy your database and files to a remote server
    Compile twig templates

The first step is to open up a terminal window. Linux & macOS users should be no stranger to the command line or terminal, but if you use Windows, you might not have used this very much.

The terminal is pre-installed on Linux and MacOS, but you will probably need to install additional software to use Drush if you are using Windows. We would recommend to install “Git Bash” for this. If you haven't had it already, download and install the Git for Windows package from Google. This is the best terminal available for the Windows platform.

Once you have your terminal window open, you will need to become familiar with a few shell commands. If you are new to the command line, try
ls to list the files in the current directory,
cd to change directory, and
pwd to show which directory you are currently in (print working directory).
The command line might feel awkward at first, but as you get used to it, it will really help to speed up your tasks.

How to Install:
Drupal 8 development as it requires Drush 8. Using Composer is far and away the easiest method to install Drush.

First, install Composer globally.

$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer

Now add Composer's directory to your path by editing your .bash_profile

$ export PATH="$HOME/.composer/vendor/bin:$PATH"

Reload your terminal or just re-source the configuration you just added.

$ source ~/.bash_profile

Now that you have Composer working, you can install the latest Drush.

$ composer global require drush/drush:dev-master

Keep Drush up to date.

$ composer global update

However, it will install the latest version 8.0.3

Then you can check if the update has been successful by executing:

$ drush --version

Now, let’s begin with Drush. First, we'll use Drush to download, install and run Drupal 8:

Step 1:     To download the latest D8 branch available
               Drush dl drupal-8 --select

Drush Download D8

Select the appropriate branch for the development, as I had selected 2 and hit Enter key

To download D7 branch
drush dl drupal-7 --select

similar to d8, select branch for the development and hit Enter key.

Drush Download D7

Once we have done with branch selection we’ll get detailed information about the profile and available module on that branch.

Drupal 8 Downloaded

Step 2: Next step is to create a database. We can use the command line for login to MySQL and create the database

mysql -u root -p

Create database dbname;

Drupal 8 Database Creation

Step 3: Now we have created the database, let's install Drupal.

drush si standard --db-url=mysql://[db_user]:[db_pass]@[ip-address]/[db_name]

In this line we are passing a standard profile, name of the drupal profile to install along with MySQL username, password, and newly created database name.

Once we run the above command, we will get prompt with message

You are about to create a /var/www/html/drupal-8.0.2/sites/default/settings.php file and DROP all tables in your 'd802' database. Do you want to continue?

where d802 is a newly created database name.

Pressing  “y” will start the drupal installation with the standard profile.
Drush generate the random password and display the message -

Installation complete.  User name: admin  User password: HUoe2kei4o   [ok]                                                  
Congratulations, you installed Drupal!

Drupal 8 Successfully Downloaded with random password

Step 4: For security reasons provide 766 / 777 permission to  settings.php/files folder respectively.

chmod 755 settings.php
chmod 777 files

We completed all the steps to install Drupal using Drush. Now we can see our newly installed Drupal in browser.

Drupal 8 Home Page

 

Building Configuration Form in Drupal 8

The primary mechanism for collecting input from users is Form, without them Drupal wouldn't be so much useful. This is also one of the first things Developer should learn when they start development using Drupal. Forms are fundamental to creating Drupal modules, whether asking someone to leave a comment or Administrator has option to turn the module configuration ON/OFF.

The configuration system / state system has replaced the variable system in D8. There is no variable table and no more variable_get() / variable_set() /  variable_del() calls. Configuration is stored in the database and synced with YML files on the disk for deployment purposes.

The $config object handles CRUD (Create/Read/Update/Delete) for YML files, so you simply use ::get(), ::set(), and ::save() methods and your data will be stored in the {module}.settings.yml file.

When we defined PageExampleForm, which is our new class. extend the FormBase class; simple implementation of the FormInterface which provides an interface for a form.

Drupal 8 provides an alternate class, ConfigFormBase, which enables additional functionality at the cost of increased complexity in the method. The primary benefit of this class is, improved interaction with the configuration system.

Lets Code:

The first thing you'll need to do is create a route for your form. In our example, it looks like this:

Step 1: In your_module.info.yml file, you define the configuration route:

    configure: your_module.admin_settings
ex: xai.settings.form


Step 2: In your_module.routing.yml file, you define the route:

The only difference between this route and one that displays non-form content on a page is the _form key instead of the usual _controller key. Here _form tells Drupal the location of the class that it should use when constructing our form object.

Note: We simply specify the class name here and not the method, like SettingsForm::buildForm. Because we've defined this route as a form, Drupal will call buildForm whenever someone requests /admin/config/system/xai.

...
   your_module.admin_settings:
          path: '/admin/config/your_module'
          defaults:
      _form: '\Drupal\your_module\Form\ModuleConfigurationForm'
      _title: 'your_module configuration screen'
          requirements:
     _permission: 'administer site configuration'

Step 3: Define tform  in your_module/src/Form/ModuleConfigurationForm.php :

SettingsForm extends ConfigFormBase

Also note that we've opted to extend the Drupal\Core\Form\ConfigFormBase class which provides some additional boilerplate code for system settings forms. There is a Drupal\Core\Form\FormBase class also.

Git Hib source code for configuration page

Once you have done withSettings.php form. you can access configuration page by visiting
url: https://example.com/admin/config/xai/settings

 

Configuration page setting

Source Code: https://github.com/xaiwant/D8ConfigForm

1’st Meetup of Drupalchix in Bangalore - A Career Network for Women in Drupal

After postponing couple of times, the first Meetup of Drupalchix (Women in Drupal), in Bangalore was held on 23’ Jan 2016, and was organized by us in association with the Bangalore Drupal User group. The main aim of this innovative Meetup was to introduce Drupal as an easy to use CMS and let the participants interact and explore the various career options that Drupal has to offer. Drupalchix is a unique and active female professional network with regular meetups and events across different locations, and is intended to encourage more women to learn and work with Drupal.

The event in Bangalore was a perfect blend of fun and learning, and was also attended by 10+ Drupal developers from prominent corporate firms such as TCS, Mindtree, Valuebound amongst others from a few colleges. They gave rich insights into using Drupal and its various career options. It was designed to introduce the basics of Drupal in a well-structured and easy to understand manner, so that it gives the attendees a perfect introduction into one of the most popular, widely used and comprehensive open-source CMS (Content Management System) technologies used globally. The event was a perfect opportunity for participants from different educational and technical backgrounds to network and share their views on career prospects in Drupal.

Drupalchix

We at Valuebound are leaders in the field of Drupal consulting, and have also been using our rich domain experience in designing training programs that essentially focus on learning from a practical usage perspective, thereby adding much value and utility to the learners. The first meetup in Bangalore that was organized by us was a perfect success, and we aim to organize many such events across Bangalore so as to have maximum participation and exposure in our quest for making Drupal an excellent career choice for girls.  Our training camps and events are especially designed to share our seasoned Drupal best practices with a unique focus on real-time usage, thereby giving participants a much needed advantage in their career and networking opportunity.

We wish that our readers and participants spread the word on our training programs and the Drupalchix (Women in Drupal) community events, so that we can work towards increasing the awareness of Drupal amongst female professionals and students. Please feel free to share the word amongst your friends and colleagues and do contact us for more information.

Drupal 8 installation in Windows with XAMPP

Installation of Drupal requires a Web server. We will be employing XAMPP package for the same purpose. XAMPP is a free and open source web server solution stack. It stands for (X-cross platform), (A-Apache HTTP Server), (M-MariaDB), (P-PHP), (P-Perl).  XAMPP is hassle free and is widely used by developers all over to create a local web server.

To install XAMPP, one can download it via the below provided link
https://www.apachefriends.org/download.html

On the basis of requirement of individual system and configuration one can install the XAMPP.

XAMPP for Windows 5.5.30

To begin installation, Open/double-click .exe file

XAMPP installation

Step 1: On opening the file, a XAMPP set up wizard will initiate:

XAMPP setup wizard

Step 2: Click next, a screen will appear where you can choose the components you wish to keep, on clicking next here you will be getting default components provided by XAMPP.

XAMPP select component

Step 3: Select the location in your system where you wish to install the XAMPP.

XAMPP - choose install location

Step 4: Click on next to begin installation

Bitnami for XAMPP

Step 5: On successful completion of Installation, following screen should appear.

Completing XAMPP setup wizard

Keep the box checked (Do you want to start the Control Panel now),
Click on finish to complete the process, control panel screen will automatically launch.


XAMPP control panel

The above steps would concluded the installation of a local web server in your system, Apache and MySQL is running as we can see from the control panel. To check type 127.0.0.1/dashboard/ or localhost/dashboard/

XAMPP dashboard

Now since we have a local web server in our system, we can begin with Drupal 8 installation. We can download the latest Drupal 8 package from the below link:

Ref URL: https://www.drupal.org/news/drupal-8.0.0-released
Version: drupal 8.0.1
drupal-8.0.1.zip

Drupal 8 zip

Click on Save file.
The next step is to install drupal in the system. It includes following steps:

Step 2.1: Copy the drupal .zip file in “C:\xampp\htdocs” folder.
The path should look like this

s30

Step 2.2: Extract .zip file.
Step 2.3: On your web browser, type 127.0.0.1/drupal-8.0.1, where drupal-8.0.1 is the extracted folder name.
The step to copy and paste the default.settings.php into settings.php and similarly create a “Files” folder under sites/default and give read write execute permission for all type of user.

Following above steps will take you to the drupal installation page

Drupal installation - Choose language

One can choose the language of installation by clicking on the drop down box where default language is English.

Step 2.4: Click on next and a profile selection page is opened, Standard profile is the default profile.

Drupal installation - select installation profile

Step 2.5: Click on Save and Continue

Drupal installation - Requirements review

A requirements review page opens up that informs us about the requirements on various parameters viz web server, PHP version, library, holder permission, settings.php which are needed for the drupal to operate in our system.

Click on continue anyway which is at the bottom of the installation page

Step 2.6: To install drupal 8, we need a database which can be created by visiting URL localhost/phpmyadmin/serverdatabases.php

The above URL corresponds to phpmyadmin which functions as a data administrator for MySQL from where we can create, modify or delete databases; also execute SQL statements and manage users and permissions.

Phpmyadmin - Create databse

Any human-readable name could be given as database name, here we have provided our database name as ‘drupal8’. One can provide any similar name however it is suggested that a simple name is given. Underscores (_) may also be used.
After providing the name click on ‘create’.

Drupal installation - Database configuration

Please note that the name ‘drupal8’ given by us while creating the database becomes the name of the database name while installing drupal. The database username is root as we can see in the above screenshot, which is a default database username.
The password section is to be left blank in case there is no password assigned already.


Click on save and continue. Installation should start.

Installing Drupal

After installation, configure site page will open which will need some details from us viz site name/site email address/username/password/Country/Time zone and update notification check boxes.

Details provided by us in this case are;
Site name -MyName (one can give any site name)
Site address - myname@gmail.com
username - admin(one can provide any username they like)
Password - choose a strong password.
Country - India
Timezone - Asia/Kolkata

checkbox for updates notification are ticked default.

Database configuration page

Click on save and continue.


Drupal installation completion

A window like above will appear that will notify that the Drupal has been installed in your system.

The URL for same is localhost/drupal-8.0.1  where drupal-8.0.1 is the folder name.

 

How to pass page callback arguments in Drupal 8

In earlier blog post, we have seen how to pass arguments in views pages in Drupal 8. But not all the time we have to use views. What if we have a custom page created where we have to pass arguments to a page thru’ url. We are going to explore how to do the same in this blog post. Before we get into passing argument to a page, i would like to go through small introduction of Controller

In earlier versions of Drupal (D7), there was hook_menu. Hook menu actually managed a few different features. In addition to handling incoming requests, it provided menu links, access control, action links, and a number of other features that are all very tightly coupled together.

But in Drupal 8 By using Symfony2 Routing component, we are able to split out the route handling aspect, and get a much improved and feature-rich solution. the main reason for using Symfony2 Routing was to be able to create routes on more than just the path, for example make a request for JSON, XML or HTML while still using the same path.

If you wants to grab more info on controller please visit here [https://www.drupal.org/node/2116767]. In order to pass the page argument using url, we need to  create one new controller under modules/custom//src/Controller with name called “ControllerName.php”     

Step 1: Created controller

addition.form:
    path: '/resume/addition/{first-arg}/{second-arg}/{third-arg}'
    defaults:
      _controller: '\Drupal\\Controller\::’'
    requirements:
      _permission: 'access content'

Where,
path:
path: '/resume/addition/{first}/{second}/{third}'
path name should be any generic name with three different argument.

Controller:
_controller: '\Drupal\resume\Controller\AdditionController::add'
To accessing a specific controller use _controller path with Controller name and accessible function name. In my case AdditionController is my controller with add function.


Step 2: Now lets create another Controller AdditionController for additional functionality, where we are going to pass three different argument & performing addition for three numbers.
Once these arguments has passed on. we perform addition of three numbers & storing the output to an array.

$total[] = $first + $second + $third;

To render the output we are using theme function.

 $render_array['addition_arguments'] = array(
      // The theme function to apply to the #items
      '#theme' => 'item_list',
      // The list itself.
      '#items' => $total ,
      '#title' => $this->t('Addition of 3 values'),
    );
return $render_array;

Code Base for Controller:

namespace Drupal\resume\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Controller routines for page example routes.
 */
class AdditionController extends ControllerBase {

  public function add($first, $second, $third) {

    $total[] = $first + $second + $third;

    $render_array['resume_arguments'] = array(
      // The theme function to apply to the #items
      '#theme' => 'item_list',
      // The list itself.
      '#items' => $total ,
      '#title' => $this->t('User Information'),
    );
    return $render_array;
  }
}

Once we have done with controller and routing system. we can visit the page by passing three argument and  we can see the data on renderable page. Below is the attached screenshot

Where i’m passing 20,30,40 as argument and i’m getting sum of those numbers: 90 on that page
URL: example-name/addition/20/30/40

addition page by passing 3 argument

What we have seen above is very simple demonstration of how to to pass arguments from URL to page and making use of the same to calculate some values.

Source code: https://github.com/xaiwant/D8-passing-arg-to-page

Power of Drupal Console in Drupal 8

Drupal Console is command line tools, help us to speed up the development tasks for Drupal websites. After installing console, you will be able to perform actions simply by typing commands into a terminal, actions that usually takes multiple steps in a web browser (Drupal Installation/ Field addition on Bundle), or Writing a basic code.
Just to add here, Drupal Console works with Drupal 8 only, whereas Drush runs on Drupal 8.

Below are the syntax for Console, With Drupal Console you can generate boilerplate code for modules, themes, controllers, forms, blocks, and much more.
        
        Node
              drupal create:nodes
        Controller
              drupal generate:controller
        Entity
        Form alter hook
        Module
              drupal generate:module
        Block
              drupal generate:plugin:block
        Field type, widget and formatter
        Image effect
        Rest resource
        Service
        Theme

              drupal generate:theme
        Switch maintenance mode on or off
              drupal site:maintenance on
        Run unit tests

There are few similar tools available e.g.

Module Builder: (Generates Drupal 6, 7, or 8 module scaffolding)
Drupal Module Upgrader: (Converts modules from Drupal 7 to Drupal 8; generates static help file with links to relevant change records)
Drupal 8 Tools: (Drupal code generator written in bash)
Drush: (Interact with Drupal installation via CLI, create aliases, create custom commands)

Why it’s Unique?

Drupal Console use modern PHP practices introduced into Drupal 8, includes object-oriented PHP. Console isn't a Drupal module, but was built with the Symfony Console Component and other libraries, such as Twig, to generate PHP, YAML, and other file types used in Drupal 8 module development.

It is a designed for anyone using or planning to use Drupal 8. At the moment, it is used via a CLI, but there are plans to make it accessible through the Drupal administration interface.

Drupal Console provides a number of commands for creating module temporary structure and boilerplate code. For any command, you will be asked a series of questions about what you want. In that case files are created and inside these files, classes — complete with namespacing and use statements — are created for you with the naming convention you specified in the command's prompts.


Let’s Have a look

For installation please refer here.
Once you have done with installation type drupal in terminal and you’ll get below list

s1

To pull out all the commands and option for console type drupal list & you will get list of options and available commands.

s2

As i’m going to show you how can we create boilerplate code “Generating Custom form in Module
Before we start with building a custom form we need to generate a module first

Step 1: Run drupal generate:module

s3

// Welcome to the Drupal module generator

 Enter the new module name:
 > Contact us

 Enter the module machine name [contact_us]:
 > [you can provide own machine name or else it will take default [contact_us]]

 Enter the module Path [/modules/custom]:
 > [you can provide own path or else it will take default [/modules/custom]]

 Enter module description [My Awesome Module]:
 > Creating custom contact us form with few fields

 Enter package name [Custom]:
 > [you can provide own choice or else it will take default [Custom]]

 Enter Drupal Core version [8.x]:
 > [you can provide own Core version or else it will take default [8.x]]

 Do you want to generate a .module file (yes/no) [no]:
 > [you can provide own choice or else it will take default [no]]

 Define module as feature (yes/no) [no]:
 > [you can provide own choice or else it will take default [no]]

 Do you want to add a composer.json file to your module (yes/no) [yes]:
 > [you can provide own choice or else it will take default [yes]]

 Would you like to add module dependencies (yes/no) [no]:
 > [you can provide own choice or else it will take default [no]]

 Do you confirm generation? (yes/no) [yes]:
 > [you can provide own choice or else it will take default [yes]]

Once we pressed yes we will get below files

Generated or updated files
 Site path: /var/www/html/drupal-8.0.2
     1. modules/custom/contact_us/contact_us.info.yml
     2. modules/custom/contact_us/contact_us.module
     3. modules/custom/contact_us/composer.json

s4

Finally we have done with module creation using Drupal Console. using Console we can reduce the time and energy to develop custom module or site configuration, which takes more when we go through step by step procedure in ui level.

Git Source: https://github.com/xaiwant/D8ConsoleForm/tree/master/contact_us

Download the Drupal Guide
Enter your email address to receive the guide.
get in touch