diff --git a/sources/tech/20191114 Creating Custom Themes in Drupal 8.md b/sources/tech/20191114 Creating Custom Themes in Drupal 8.md new file mode 100644 index 0000000000..f2ebf73568 --- /dev/null +++ b/sources/tech/20191114 Creating Custom Themes in Drupal 8.md @@ -0,0 +1,229 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Creating Custom Themes in Drupal 8) +[#]: via: (https://opensourceforu.com/2019/11/creating-custom-themes-in-drupal-8/) +[#]: author: (Bhanu Prakash Poluparthi https://opensourceforu.com/author/bhanu-poluparthi/) + +Creating Custom Themes in Drupal 8 +====== + +[![][1]][2] + +_A theme in a website is a set of files that defines the overall look and the user experience of a website. It usually comprises all the graphical elements such as colours and window decorations that help the user to customise the website. Drupal provides the user with a bunch of basic themes for a website that are very generic. However, these default themes do not suit all types of users. So there is a need to build themes that meet one’s requirements._ + +Creating and customising themes in Drupal 8 is easy because of a modern template engine for PHP named Twig, which is a part of the Symfony 2 framework. Moving from a PHP template to Twig, and from the INI format to YAML, are some of the main changes in Drupal 8 theming. These changes in Drupal 8 have improved the security and inheritance, making theming more distinguished. + +With reference to Figure 1, + + * _.info_ provides information about your theme. + * _html.tpl.php_ displays the basic HTML structure of a single Drupal page. + * _page.tpl.php_ is the main template that defines the contents on most of the pages. + * _style.css_ is the CSS file that sets the CSS rules for the template. + * _node.tpl.php_ defines the contents of the nodes. + * _block.tpl.php_ defines the contents in the blocks. + * _comment.tpl.php_ defines the contents in the comments. + * _Template.php_ is used to hold preprocessors for generating variables before they are merged with the markup inside .tpl.php files. + * _Theme-settings.php_ is used to modify the entire theme settings form. + * _.libraries.yml_ defines your libraries (mostly your JS, CSS files). + * _.breakpoints.yml_ defines the points to fit different screen devices. + * _.theme_ is the PHP file that stores conditional logic and data preprocessing of the variables before they are merged with markup inside the .html.twig file. + * _/includes_ is where third-party libraries (like Bootstrap, Foundation, Font Awesome, etc) are put. It is a standard convention to store them in this folder. + + + +The basic requirement to create a new Drupal theme is to have Drupal localhost installed on your system. + +![Figure 1: Drupal 7 theme structure \(https://www.drupal.org/docs/7/theming/ overview-of-theme-files\)][3] + +**Drupal 8 theme structure** +A custom theme can be made by following the steps mentioned below. + +_**Step 1: Creating the custom themes folder**_ +Go to the Drupal folder in which you can find a folder named Theme. + + * Enter the folder ‘theme’. + * Create a folder ‘custom’. + * Enter the folder ‘custom’. + * Create a folder ‘osfy’. + + + +Start creating your theme files over here. The theme name taken here is osfy. + +_**Step 2: Creating a YML file**_ +To inform the website about the existence of this theme, we use _.yml_ files. The basic details required in the YML are mentioned below: +1\. Name +2\. Description +3\. Type +4\. Core + +``` +name: osfy +description: My first responsive custom theme. +type: theme +package: custom +base theme: classy +core: 8.x + +regions: +head: head +header: header +content: content +sidebar: sidebar +footer: Footer + +Stylesheets-remove: +-”Remove Stylesheets” +``` + +We can proceed once the theme appears in the uninstalled section of your website’s _Appearance_ tab. +Open the Drupal website and check for the new theme in the _Appearance_ section. It will be under the uninstalled list of themes in the _Appearance_ tab. + +**Note:** 1\. Base theme indicates which base theme your custom theme is going to inherit. The default base theme provided by Drupal is ‘Stable’. + +2\. Regions defines the regions in which your blocks are to be placed in your theme. If not declared, Drupal uses default regions from the core. + +--- + +_**Step 3: Adding the .libraries.yml file:**_ +We have indicated all the libraries comprising JavaScript and CSS styling, and now we will define them in the _libraries.yml_ file. + +``` +global-components: +version: 1.x +css: +theme: +css/style.css: {} +includes/bootstrap/css/bootstrap.css: {} +``` + +We will use _style.css_ for the theme styling and bootstrap.css for responsive display using Bootstrap libraries. Style.css resides in the core/css folder, whereas bootstrap.css resides in the _includes/bootstrap/css_ folder. + +![Figure 2: Drupal 8 theme structure][4] + +_**Step 4: Creating theme regions**_ +To better understand how Twig has made things easier, use the following code: + +``` + + +