Friday, 28 April 2017

Drupal 8 Metatag

To make website popular and to increase the SEO of website, Metatag play the very important in it.
In D7 it installation is very easy.We just have to install module and have to configure as per our requirement.
If you have remember, in D7 when we install the module and after configuration it is created a field for entity types and because of that field we were able to set metatag for each entity.

Step to set up metatag in D8

  1. Install the module.
  2. You can setup common metatag for site by visiting this url (admin/config/search/metatag).
  3. If you want to add different metatag for each node or term than create a field of type metatag in any content type or in entity type for which you want to setup.
  4. Now create a node and on node edit page you can see metatag tab,  in which you can add metatag for your single node ot you can use tokens.
  5. Now your metatag is ready for that entity type.
  6. If you created field in one content type then you only able to setup metatag for that entity type entities only.
  7. In D8, You can't reuse of field of one entity type to another entity type.So for each entity you have to create different field of metatag type.
So by following above steps you can easily setup metatags for your site.

Thursday, 27 April 2017

Drupal 8 Create a menu and add menu link in admin menu

In drupal 7 it is a very simple task because hook_menu provides the straightforward implementation but in drupal 8 its implementation is very different. In drupal 8 we create the YAML files. Hook menu is converted into routing file.
So let us take an example to understand this. We want to add a link under the admin -> configuration -> search and metadata.
So for this first, we have to create a menu link(In D8 routing)

Create a file  (My_Module.routing.yml) under your module directory.

My_Module.MY-MENU:
  path: '/admin/config/search/MY-PATH'
  defaults:
    _form: 'Drupal\My_Module\Forms\MYMENU'
    _title: 'MY MENU'
  requirements:
    _permission: 'administer site configuration'

Here My_Module.MY-MENU it is unique machine name. For more detail click here

So above code just registers your menu in the menu configuration. After clearing cache you will be able to visit  URL directly. It works fine if you haven't made any mistake in defining controllers. To listing  this URL into admin menu we have to implement following code
Create another file in the same directory (My_Module.links.menu.yml)

My_Module.MY-MENU:
  title: 'MY MENU NAME'
  parent: system.admin_config_search
  description: 'Add MY path'
  route_name: My_Module.MY-MENU:
  weight: -1

Here My_Module.MY-MENU this can be different but for our convenience, we provide the same routing machine name.
 parent : For parent, we need to search routing machine name of parent menu (In our case parent menu is admin -> configuration -> search and metadata ). Mostly you can find these paths into routing.yml files.

Thursday, 20 April 2017

Drupal 8 Get the list of all voacbs

To get the list of all vocabs, Drupal 7 provides us a function "taxonomy_vocabulary_get_names()" which return a list of all vocabs with their machine name.
In D8 for some time this function will exist, but in future, this will be deprecated and in Drupal 9 this will be removed.
So if you are using this function then this will work but any update of Drupal 8 will deprecate this function.So to avoiding this problem use following code:

Just include this class
use Drupal\taxonomy\Entity\Vocabulary;

then write this line, which returen an array of menu objects.
$vocabs_types = Vocabulary::loadMultiple();

For only get list of vocabs, you can use this

  $vocabs = array();
    $vocabs_types = Vocabulary::loadMultiple();

    if (!empty($vocabs_types)) {
      foreach ($vocabs_types as $vocab_name => $vocab) {
        $vocabs[$vocab_name] = $vocab->label();
      }
      asort($vocabs);
    }

Drupal 8 Get list of all menus

To get the list of all menus, Drupal 7 provides us a function "menu_get_menus()" which return a list of all menus with their machine name.
In D8 for some time this function will exist, but in future, this will be deprecated and in Drupal 9 this will be removed.
So if you are using this function then this will work but any update of Drupal 8 will deprecate this function.So to avoiding this problem use following code:

Just include this class
use Drupal\system\Entity\Menu;

then write this line, which returen an array of menu objects.
$menus = Menu::loadMultiple();

For only get list of menus, you can use this

    $menus = array();
    $menu_types = Menu::loadMultiple();

    if (!empty($menu_types)) {
      foreach ($menu_types as $menu_name => $menu) {
        $menus[$menu_name] = $menu->label();
      }
      asort($menus);
    }

Drupal 8 Get all content types

To get the list of all content type, Drupal 7 provides us a function "node_type_get_types()" which return a list of all content type with their machine name.
In D8 for some time this function will exist, but in future, this will be deprecated and in Drupal 9 this will be removed.
So if you are using this function then this will work but any update of Drupal 8 will deprecate this function.So to avoiding this problem use following code:

Just include this class
use Drupal\node\Entity\NodeType;

then write this line, which returen an array of content type objects.
$content_types = NodeType::loadMultiple();

For only get list of content type, you can use this

    $types = array();
    if (!empty($content_types)) {
      foreach ($content_types as $type => $details) {
        $types[$details->id()] = $details->label();
      }
      asort($types);
    }

Drupal 8 ValidUrl

Drupal 7 provide us a valid_url() function to check the Url validation.In drupal 7  we can call it directly because it was written in the module file.But in D8 we can't.
In D8 we have to use UrlHelper class and the help of this class we can use this function.
In D8 valid_url() is converted into isValid(), so for using this function,

  1. First, include class namespace
    use Drupal\Component\Utility\UrlHelper;
  2. then use this function, No need to create the object because it is a static function so we can call it by its class name
    UrlHelper::isValid($url, $absolute = FALSE);

    Parameters : -
    $url : -The URL to verify
    bool $absolute: Whether the URL is absolute

Wednesday, 12 April 2017

Drupal 7 Block Regions and Hidden Block Regions

HI,
  I think you have created blocks and placed that block in any regions. Sometimes we need to create some new block regions.
So first, how to create new block regions. It is very simple in drupal, 
Just go to your theme.info file and add the following line of code
  regions[REGION_MACHINE_NAME] = REGION NAME

So in this way, we can create many regions as our requirement.Now by adding these line and after clear the cache, we can see these region at block structure page and we able to assign blocks to these region but we can't see blocks on the page.Why????????
Because we haven't specified block region variable in page.tpl.php file. We have to print the region variable into page template file.
Just go to page.tpl.php file and just write the following code into template file, you need to keep in mind where are you witing this line.
<?php print render($page['REGION_MACHINE_NAME']); ?>


Hidden Regions

 Sometimes we need to create such block regions which are not allowed to a user place any block in it. we don't want to show these regions on block structure page or in simple words we can say that we don't want to allow the user to change the specific block placing on the page. What is the solution for this ???????
Yes, Hidden regions are there for helping us.
Hidden regions are not different functionality, it is similar to simple block regions.When we make a region as a hidden region then we can't see that region at block structure page.
Hidden regions are behaving same as simple regions except rendering on block structure page.

How to create hidden region, there are tow methods 

1. By theme.info file
regions_hidden[] = the_region_name
    Just writing this line into your theme.info file


2. By hook_system_info_alter()

We can make any region, hidden region by the help of this hook.

Just show your theme.info file there are two regions page_top and page_bottom, now see your block structure page, here you are able to see all block regions except both page_top and  page_bottom.
Why????

Just get the answer of this question, it will help you to understand the concept of hidden block regions.


If have any questions, please feel free to ask in comments.