Skip to main content

Posts

Showing posts with the label Drupal8

Unit Test With Drupal 8

Types of tests: Writing tests All PHP-based tests for Drupal core are written using the industry-standard PHPUnit framework, with Drupal extensions. There are several categories of tests; each has its own purpose, base class, namespace, and directory: Unit tests: Purpose : Test functionality of a class if the Drupal environment (database, settings, etc.) and web browser are not needed for the test, or if the Drupal environment can be replaced by a "mock" object. Base class :   \Drupal\Tests\UnitTestCase Namespace : \Drupal\Tests\yourmodule\Unit (or a subdirectory) Directory location : yourmodule/tests/src/Unit (or a subdirectory) Kernel tests: Purpose : Test functionality of a class if the full Drupal environment and web browser are not needed for the test, but the functionality has significant Drupal dependencies that cannot easily be mocked. Kernel tests can access services, the database, and a minimal mocked file system, and they use an in-memory pseudo-installation. Howe...

Best Way to use composer with drupal in multi environment(dev,stage,prod)

NOTE :  it’s extremely important that your development environment is using the same version of PHP that the target production environment will use. Otherwise Composer may download packages that won’t work in production.   (With larger development teams, using something like  Vagrant  or Docker for local development can help ensure compatibility between local development instances and the production stack How do I download modules and themes: 1 - composer require drupal/devel  it would download the module to web/modules/contrib. If you want to change the download destination, you can alter the paths in your composer.json.  Development Dependencies: composer require --dev drupal/devel  For example, since the Devel module shouldn’t be used in a production environment, you may only want it to get downloaded on development environments. With Composer, you can achieve this by passing the --dev flag to the composer require command. This wi...

Component-Based Theming in Drupal 8

About Pattern Lab : The PHP version of Pattern Lab is, at its core, a static site generator.   To use the basic features of Pattern Lab, you must have  PHP 5.3+  installed Componentization has become a growing consideration in most of the web application development firms. The reasons are obvious, instead of reinventing the wheels again and again, why don’t we re-use them. Why should you follow component driven front-end development approach? Allows for re-use of Components A component-based UI approach accelerates the development You can completely decouple the front-end development Testing becomes easier Parallelize development Flexible project workflow How to use component driven front-end development approach In Drupal? One of the most popular tools for component driven front-end development is  Pattern Lab .   What makes Pattern Lab so famous is its flexibility in pairing up with the Atomic Design methodology.   Addition of tw...

Events, and Event Subscribers

Drupal 8 provides a powerful and performance efficient way how modules can interact with each other.( I.e we can fire any custom code on a perticular action like, when save configuration of site name etc fire my  custom code etc ) Modules can either dispatch events, subscribe to  events, or both.  Subscribing to an event allows a module to declare that it would like to be  notified anytime a specific event happens. A module can subscribe to any  number of events, and can even subscribe to the same event more than once.   Dispatching an event allows a module, or Drupal core subsystem, to notify any  registered subscribers that a specific event has just taken place. When an  event is dispatched the registered code for each subscriber is executed. For  example, whenever a configuration entity is updated the Configuration API  dispatches a new event allowing all subscribers to react to the change.  Subscribing to an e...

twig templating

Twig is a PHP-based compiled templating language. When your web page renders, the Twig engine takes the template and converts it into a 'compiled' PHP template which is stored in a protected directory in sites/default/files/php/twig. The compilation is done once, template files are cached for reuse and are recompiled on clearing the Twig cache. Preprocessing for Template Files : The main role of the preprocessors is to set up variables to be placed within the template (.tpl.php) or twig files. Every layer in Drupal from core, modules, engines and themes can have numerous preprocessors Mainly There are two main functions to override the  code .. preprocess and preprocess_HOOKS respectly, first is template,module,engine,theme, Most used is theme preprocess and process THEME_preprocess(&$variables,$hook) Where hooks are like menu,page,html, etc THEME_preprocess_HOOK(&$variables) Features of Twig :  Twig Block in Drupal 8 : It is performed by tempelate in...

Cache In Drupal

Drupal 8 core caching modules: The Internal Page Cache module: this caches pages for anonymous users in the database. Pages requested by anonymous users are stored the first time they are requested and then are reused for future visitors. The Internal Dynamic Page Cache module: This is a key feature that Drupal 7 did not have. Unlike the Internal Page module, the Dynamic Page module aims to speed up the site for both anonymous and logged-in users. How Cache Works in Drupal: There are two modules available in the drupal core 1 - Internal page cache: The Internal Page Cache module caches pages for anonymous users in the database. Pages requested by anonymous users are stored the first time they are requested and then are reused. Configuring the internal page cache :   From the performance page we can set the maximum time how long browsers and proxies may cache pages based on the Cache-Control header. 2 - Internal dynamic page cache: Drupal 8 provides the Dynamic Page Cache m...

Apache Solar Search

Apache Solr : -  Apache solr is a fast open-source Java search server. Solr makes it easy to run a full-featured search server Recent version -- Solr 8.11.1 default port: 8983 You can access your Solr admin panel by typing  localhost:8983  in your browser. Basic Solr Concepts:- Solr is able to achieve fast search responses because, instead of searching the text directly, it searches an index instead. Solr is powered by Lucene, a powerful open-source full-text search library, Limitation of default drupal Search:-  1- default drupal search is for smaller sites only, 2- does’t deal with large content like 10k+,  3- Limited operated 4- SQL is not designed as a searching language, 5- It runs and searches directly on the same databases, 6- RDBMS , is incapable to search well, Benefits of using apache solr:-  1- index and make searchable really a large amount of data from 10k up to millions, 2- Provide faceted search-based nav...