Drupal

PHP : Replacing logical operators with logical strings with Example

In PHP normally we use logical operators for validating. The operators performs operation with values and return Boolean value as TRUE or FALSE. Normal logical operators are AND(&&) and OR(||). We can able to replace logical operators with logical strings.

Drupal 7 & 8 : Alter system path with custom Paths with Example

In Drupal system defines some paths. For example “node/add/CONTENT-TYPE”. We can change system path in custom module. There is hook function “hook_menu_alter()”. This hook loads all menu items in our system. By changing the items path by our custom path so system work with new path. In this hook we can also unset system path by unsetting item path.

Drupal 7 & 8 : Securing Files non Executable with Example

Normally servers hits request that execute file. To prevent execution of file we need make this files as securable by changing the extension of the file. In Drupal there is a function file_munge_filename() it will munge the actual file format by adding additional characters in extension. There by file is consider as some other file so it won't execute while calling. The function file_munge_filename() need argument of file name with additional arguments of extension details and message details. If extension is set than an underscore(_) will be adding to the file. If Message parameter set true Drupal message is show at user end.

Drupal 8 : Programmatically sent Mail from system - Example

In Drupal 8 there is function service() from core is used to send Emails. This function returns a object that help to trigger mail from system by passing argument as “plugin.manager.mail”. Using this Object call mail() function. After initiate mail need to call hook_mail() for define the Mail subject, Body and From address.

Drupal 8 : Get Site Informations like Site Name and Site Mail ID with Code

In Drupal 8 site information's are stored in system.site config file. This information's are accessed from config() function with help of Drupal object. Basically this settings holds the information's of Site name, Site Slogan and Site Mail Id.

Drupal 8 : Custom Form Validate and Submit in hook_form_alter() with Example

In Drupal 8 we can able to call custom validate and submit function for our need. This custom functions need to be defined in hook_form_alter(). Validate is defined as array parameter for #validate key of $form array. Similarly for submit is defined as array parameter for #submit on $form submit action.

Drupal 8 : Encrypting String with Site Private Key including Sample code

In Drupal 7 to encrypt the string we use the core function “drupal_hash_base64()”. This function have 2 parameters, Encrypting string and optional parameter of Site private key. But this function is deprecated in Drupal 8. In Drupal 8 we need to use the class Crypt from component utility for encryption. Similarly it have 2 parameters. Accessing Private key of your site by calling the service function of Core “\Drupal::service('private_key')->get()”.

Drupal 7 : Alter View Pagination with custom Pager

We can set pager for our views in user interface under pager [Shown in screenshot]. For our custom purpose we can alter the views pager in our module by using the hook function hook_views_pre_render(). This hook contains the reference argument $view that have all render able array of the view. Using this argument we can reset the pager based on our requirements.

Drupal 8 : Check Module Exist or Not

In Drupal 7 there function called module_exist(). This function returns true if module exist else return false. For Drupal 8 module_exist() function is Deprecated. In Drupal there is Core function moduleHandler() will more information about modules in Drupal system. The moduleHandler() returns a object, using this object we can call moduleExists() function with module name as parameter to the function.

Drupal 8 : Writing your own Access denied

If we planned to restrict some pages from user we put 404 error message to that page that is called Access Denied. To achieve this in Drupal 8 we need to use Symfony components class. This AccessDenied is handle as Exception in Drupal 8. By using HttpKernel’s AccessDeniedHttpException class constructor Drupal 8 throwing 404 error.

Drupal 8 : Page Redirection with Sample code

In Drupal 8 fully works on OOPs concept. To create page redirection we need to use Symfony components class. The page redirection is done by RedirectResponse Class from Symfony HttpFoundation. This class object used inside function as a return parameter. While creating object we need to send valid site path to the constructor of RedirectResponse class. In Drupal 7 we using drupal_goto() function for redirect.