Thursday 28 February 2019

How to migrate content by admin in drupal 8?

It is easy way to import content in any content type in drupal 8 by admin. We just have to download module and install in our project that is `CSV Importer`. The link of `csv importer` module on drupal Org site is..

CSV Importer Download Link: https://www.drupal.org/project/csv_importer

Description: It supports Taxonomy, Users, Content migration service and this module is also available for Drupal 8.
We are going to attach demo CSV file format as screenshot in this article for better understanding.
Now going to add some screenshots for understanding module.
CSV Importer Module
Install Module process
Module Configuration
Choose entity for migrate
Now we can Choose here Entity type[Content type, Taxonomy, Users] and then content type dropdown will come automatically and in case of taxonomy then taxonomy's dropdown list will come.
Then Upload CSV file same as attached under file screenshot and upload content with easy way.

Demo CSV file Format:
Demo CSV file format

Note: If have any suggestions or issue regarding 'How to migrate content by admin in drupal 8?' then you can ask by comments.  

Thursday 7 February 2019

How to dynamically clear cache in drupal 8?

It's a easy way to clear or remove cache with using this simple code.

Code: \Drupal::service('page_cache_kill_switch')->trigger();

Description: Simple write this code on your function and when your code will hit or run on any page this page cache will automatically triggered and clear.


Note: If have any suggestions or issue regarding 'How to dynamically clear cache in drupal 8?' then you can ask by comments.  

Thursday 24 January 2019

How to render template for custom module in drupal 8?

It is easy way to render/call template(.html.twig) file for custom module in drupal 8, But here concept is slightly different from drupal 7.
First we will go to the Controller file:
Like our controller name is ContactController.php so we will write there code like.
//Go to the ContactController.php file..
/**
 * @file
 * Contains \Drupal\contact\Controller\ContactController.
 */

namespace Drupal\contact\Controller;
use Drupal\node\Entity\Node;
use Drupal\Core\Controller\ControllerBase;


class Contactv4Controller extends ControllerBase {  
    public function indexv2() {
     
      $data = "<div>Testing</div>";
     
      return [
       '#theme'  => 'specific_contact',
       '#contact'   => $data,
    ];
  }
}

// Then go to the contact.module file.
/**
* Implements hook_theme() to add the template definition..
**/
function contact_theme($existing, $type, $theme, $path) {
  return array(
       'specific_contact' => [
        'variables' => [
            'contact' => NULL,
       ]
    ]
  );
}
 
// Now in the create template folder and the file with name of 'specific_contact.html.twig'.
And render the variable there like: <p> {{ contact }} </p>
And clear the cache after this.
Now Front end result will be 'Testing' on new template page.


Note: If have any suggestions or issue regarding 'How to render template for custom module in drupal 8?' then you can ask by comments.   

Friday 11 January 2019

How to optimize the website in drupal 8 ?

Sometimes we create the website but that is not quickly responded and we don't get proper users response on site. Sometime our client want to Speed Up the website and we just go through the Admin section and Aggregate CSS and JS files. But that is not properly working as our expectation. So here very first time we have some suggestion in easy way to Speed up the website and that will surly Speed up our website . That is tested with our projects. Please have a look suggestions for Desktop and Mobile both end..

Suggestions:
- Module to speed up site(Image Lazyloader, ADVANCED CSS/JS AGGREGATION)  --> We have added screen shot for checked options.
- Aggregated CSS/JS code.
- Remove extra CSS/JS that is not use full for front page.
- Set required custom image size(image styling) for page images.
- Create custom size of images(Home -> Administration -> Configuration -> Media -> Add image style).
- Follow Google page speed suggestion.
- Reduces Request to server.
- Uninstall extra modules.
- Use Optimized Image.
- Use critical css only ,use only those css/js which you need most.
- Add Async and defer attribute.
- Eliminate render-blocking resources.











Note: If have any suggestions or issue regarding 'How to optimize the website in drupal 8 ?' then you can ask by comments.  

Thursday 20 December 2018

How to add custom js file in custom module in drupal 8 ?

It is very simple way to add custom JQuery file in custom module, but we have few methods for this.

Method 1: Our custom libraries(js/css) will be apply on all pages of project.
If we want to add custom js file in whole pages of project then we have to use this method.

1. First we have to create js file like 'gallery.js' file in js folder in our custom module.
2. Then we have to create a 'hook.libraries.yml' file means there hook will be module name.
     
Example: Suppose our Module name will be 'gallery' So we will create file with name of     'gallery.libraries.yml' in custom module folder means this file should be placed with '.routing.yml' file.
3. The code for '.libraries.yml' file is..

image_gallery:
  version: 8.4.5
  js:
    js/gallery.js: {}   
  dependencies:
    - core/jquery
  css:
    theme:
      css/gallery.css: {}


Note: There is a library called image_gallery that when included in a page will deliver the 'js/gallery.js' and 'css/gallery.css' file.

4. Then we have to create '.module' file.

Example: Suppose our Module name is gallery so file name will be 'gallery.module'. There our code for attachment of this library to our module is..

<?php
function gallery_page_attachments(array &$attachments) {
   $attachments['#attached']['library'][] = 'gallery/image_gallery';
}
?>

5. Here 'gallery/image_gallery' means 'module name/libraries name' and in the time of controller function return of page will be written like..
return array('#markup' => t($html)); // here $html is return the result for page.   

Method 2: Our custom libraries(js/css) will be apply only on custom module pages of project.  
1. Whole process and file creation will be same but we will not use attachments code in '.module' file.
2. In this case we just have to create '.libraries.yml' file and apply code..

image_gallery:
  version: 8.4.5
  js:
    js/gallery.js: {}   
  dependencies:
    - core/jquery
  css:
    theme:
      css/gallery.css: {}

     
3. Then in the time of controller function return of page will be written like..
   
return [
    '#markup' => t($html),
    '#attached' => array(
        'library' => array(
            'gallery/image_gallery',
        ),
    ),
];       


here 'gallery/image_gallery' means 'module name/libraries name' and '$html' is the written result for page.

Note: If have any suggestions or issue regarding 'How to add custom js file in custom module in drupal 8 ?' then you can ask by comments.   

Wednesday 12 December 2018

How to disable or hide or exclude the cache from page in drupal 8 ?

Sometimes we want to face the issue that don't want to apply cache or disable cache for particular page on drupal 8. Here we have option that will help in that case and we are going to give an example with this.
First of all we want to show the name of Module that will help in that case that is "Cache Exclude" in drupal 8 or download by link "https://www.drupal.org/project/cacheexclude".
for this just install this module on project and go to the configure page of this module.

Cache Exclude




So juts put the page URL in that "Pages to exclude from caching" textarea as given example of `news`, we want to remove or disable cache from `news` page so put the URL here.
and Check the content type in "Content types to exclude from caching" that checklist. so that's the simple and effective process.

Note: If have any suggestions or issue regarding 'How to disable or hide or exclude the cache from page in drupal 8 ?' then you can ask by comments.   

Tuesday 11 December 2018

How to use country timezone in our Twig file ?

If we want to change the 'DATE' filter in you TWIG file, its easy to use Timezone.
here is the good example for this issue.

Example:
{{ "now"|date("m/d/Y H:i", "Europe/Paris") }}  //For Europe Paris region timezone
{{ "now"|date("m/d/Y H:i", "Asia/Calcutta") }}  //For Asia Calcutta region timezone
{{ "now"|date("m/d/Y H:i", "Europe/Berlin") }}  //For Europe Berlin region timezone

So same like we can use for other countries region time in our TWIG file.


Note: If have any suggestions or issue regarding 'How to use country timezone in our Twig file ?' then you can ask by comments.  

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...