Monday, April 8, 2019

Create custom Twig function Extentions in Drupal 8

Some times we need custom function that will help us to use custom function to use in Twig file. We can extend the core Twig_Extension to use in our custom module.
Please follow the below steps/code to create custom twig function.
Creating your own Twig Extension function
To start with it first, we need to create a module.
1.) Create a new module inside: /modules/custom/ directory inside your drupal project.
2.) Our new module structure would look like this:
modules
--custom
----demo_module
------demo_module.info.yml
------demo_module.services.yml
------src
--------TwigExtension.php
3.) demo_module.info.yml contains
name: demo_module
type: module
description: 'Provides Twig Extension that process the Regular Expression functions.'
core: 8.x
version: 0.1.1
Information provided inside module_name.info.yml is used to be displayed on admin module page.
4.) Create a service file inside your module as 'module_name.services.yml'
services:
  demo_module.twig.TwigExtension:
    class: Drupal\demo_module\TwigExtension
    tags:
      - {name: twig.extension}
5.) The TwigExtension.php contains the following code
<?php
namespace Drupal\demo_module;
use Drupal\block\Entity\Block;
use Drupal\user\Entity\User;
use Drupal\node\Entity\Node;

/**
* Class DefaultService.
*
* @package Drupal\demo_module
*/
class TwigExtension extends \Twig_Extension {

/**
* {@inheritdoc}
* This function must return the name of the extension. It must be unique.
*/
public function getName() {
return 'pcre_extension';
}
/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('pregReplace', [$this, 'pregReplace']),
];
}
/*
* This function is used to return only letters
*/
public function pregReplace($value, $pattern, $replacement = '', $limit = -1) {
if (!isset($value)) {
return null;
}
return preg_replace($pattern, $replacement, $value, $limit);
}
6.) Now, enable your demo_module module.
drush en demo_module -y
7.) Clear the cache.
drush cr
8.) Applying Twig Extension to your twig template
You can make a call to your TwigExtension directly inside template using “{{ }}” braces. These braces are used to render any content placed inside them.

{{ val|pregReplace('/[^A-Za-z]/') }}
Moving further, set the value from pregReplace('/[^A-Za-z]/') function to remove all special characters in Twig template files, it will return only letters.
TwigExtension provides a great accessibility to the code which is often reused. With easy re-usability of the code, it also saves a lot of time and efforts. Hope you will now be able to use Twig in Drupal 8.

Thursday, December 6, 2018

Views exposed form programmatically and assign to somewhere

Sometimes to get the views exposed form and place into somewhere in the site. So we can use the below code to achieve programmatically.

$view = Views::getView('view_name');
$view->setDisplay('display_name');
$view->initHandlers();
$form_state = (new FormState())
  ->setStorage([
    'view' => $view,
    'display' => &$view->display_handler->display,
    'rerender' => TRUE,
  ])
  ->setMethod('get')
  ->setAlwaysProcess()
  ->disableRedirect();
$form_state->set('rerender', NULL);
$form = \Drupal::formBuilder()->buildForm('\Drupal\views\Form\ViewsExposedForm', $form_state);

Wednesday, May 24, 2017

Download drupal contributed module in Drupal 8 using composer

Use the following commands to download and install in drupal 8 contributed module using Composer.

composer require drupal/{module_name} {version}

Example: composer require drupal/devel 1.0-beta1

Once execute the above command, this will download the module (not installed). For install, we need the execute the drush or drupal console command to install the module.

Via Drush: drush en {module_name}
Via Drupal console: drupal module:install {module_name}

Tuesday, May 23, 2017

Clear the cache in Drupal 8

We can clear the cache in Drupal 8 in many ways

1. Via CMS end
Go to Administration > Configuration > Development > Performance (/admin/config/development/performance)
Click the button "Clear all caches"

2. Drupal console
drupal cache:rebuild all

3. Drush
drush cache-rebuild / drush cr

Monday, May 22, 2017

Create custom module in Drupal 8 via Drupal console

Generate module 
 drupal generate:module - It will ask the module name and basic information.

Give the module a name of module (welcome) and Enter module description, path and basic information. Then drupal will create the below files under the your custom module.

composer.json
welcome.info.yml
welcome.module

Install module

The module now exists but it is not enabled. You don’t need to go to the module page to enable it, you can install and enable the welcome module using the module:install command:

drupal module:install welcome

This will install the module and automatically rebuild the cache.