Thursday, 28 June 2018

Drupal: Prevent from SQL Injection

Hello All,
So today we learn how we can prevent our code from SQL injection. So first we learn what is the SQL injection and how we can prevent from this.

SQL Injection:-
It is the most common web hacking technique that might destroy your database.
So it occurs when the user ask for input. For example we want to get user detail from users table for user id 10.
so our query like be this
$uid = 10;
db_query(" SELECT  * from users where uid = $uid ");
now hacker can pass here $uid = 10 OR 1= 1
so the query will become like this
db_query(" SELECT  * from users where uid = 10 OR 1=1 ");
so it will return all rows of tables because uid = 10 may be false but 1 = 1 is always true.

So your query should be like this
db_query(" SELECT  * from users where uid = :uid ", array(':uid' => $uid));
It will prevent you from sql injection and before passing varibles into query you need to check variable type.

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