Skip to main content

Posts

Showing posts from 2016

Drupal Behaviors: A Quick Introduction

If you’re adding JavaScript to your custom module it is very easy and tempting to simply add it like this: jQuery(document).ready(function($){   alert(‘hot dog flavored water’); }); Now this code works perfectly fine but what if your JavaScript needs to be executed on page load and after an AJAX request? Imagine you have a view that uses “Views Infinite Scroll” and you want add a CSS class to every result like this: jQuery(document).ready(function($){   $('.view-display-id-page .views-row').addClass('fancy-pants'); }); This will work for the results that are displayed initially but for all the results that are loaded by Infinite Scroll's AJAX call the class is not added. That’s where Drupal behaviors come in handy.  The behaviors will be executed on every request including AJAX requests, so let's do the equivalent of the code above but this time using this method: Drupal.behaviors.infiniteScrollAddClass = {   attach: function (context, settings) { ...

How to Configure Memcache on server

(1) Install Memcache module then configure according to memcache. (2) Download the module ( https://drupal.org/project/memcache ). (3) See the Documentation of how to configure memcache module on server ( https://drupal.org/node/1131458 ). ## REQUIREMENTS ## - PHP 5.1 or greater - Availability of a memcached daemon: http://memcached.org/ - One of the two PECL memcache packages: - http://pecl.php.net/package/memcache (recommended): - http://pecl.php.net/package/memcached ## INSTALLATION ## These are the broad steps you need to take in order to use this software. Order is important. 1. Install the memcached binaries on your server. See for instance: http://www.lullabot.com/articles/how_install_memcache_debian_etch 2. Install the PECL memcache extension for PHP. This must be version 2.2.1 or higher or you will experience errors. 3. Put your site into offline mode. 4. Download and install the memcache module. 5. If you have previously bee...

Most Importanct Drush Commands

Core drush commands: (core)  archive-dump (ard,    Backup your code, files, and database into a single      archive-backup, arb)  file.                                                    archive-restore       Expand a site archive into a Drupal web site.            (arr)                                                                          cache-clear (cc)      Clear a specific cache, or all drupal caches.            cache-get (cg)        Fetch a cached object and display it.               ...

Some Changes in drupal 7 and drupal 8

There are following changes in drupal7 and drupal8. 1. variable_set()/variable_get() are gone In Drupal 7, all variables are global, so accessing and saving them is done this way: // Load the site name out of configuration. $site_name = variable_get('site_name', 'Drupal'); // Change the site name to something else. variable_set('site_name', 'This is the dev site.'); In Drupal 8, configuration will only be lazy-loaded when needed. The above code would therefore change as follows: // Load the site name out of configuration. $config = config('core.site_information'); $site_name = $config->get('site_name'); // Change the site name to something else. $config->set('site_name', 'My Awesome Site'); $config->save(); 2. $_GET[‘q’] is gone The PHP super global key $_GET['q'] is 100% missing in Drupal 8, now you are forced to use the standard method current_path(). Drupal 7: function contact...

Connecting to Multiple Databases Within Drupal

In the settings.php file, $databases is an array composed of multiple database connection strings. Here’s the default syntax, specifying a single connection: array(   'driver' => 'mysql',   'database' => 'databasename',   'username' => 'username',   'password' => 'password',   'host' => 'localhost',   'port' => 3306,   'prefix' => 'myprefix_', ); As an example, you might have two databases, the default database (in this case named D7) and a legacy database as defined here. $databases = array (   'default' =>   array (     'default' =>     array (       'driver' => 'mysql',       'database' => 'd7',       'username' => 'username',       'password' => 'userpassword',       'host' => 'localhost',       'port' => ...

Composer Manager for Drupal 8

Plz see  https://www.drupal.org/node/2405811 If following error eccur when we use "composer drupal-update" command. then show below link for patch. Update $packageToCleanup, 'tests' cleanup are no longer required for Behat/Mink package diff --git a/core/lib/Drupal/Core/Composer/Composer.php b/core/lib/Drupal/Core/Composer/Composer.php index 0d5b6aa..831346b 100644 --- a/core/lib/Drupal/Core/Composer/Composer.php +++ b/core/lib/Drupal/Core/Composer/Composer.php @@ -20,7 +20,7 @@ class Composer { protected static $packageToCleanup = [ - 'behat/mink' => ['tests', 'driver-testsuite'], + 'behat/mink' => ['driver-testsuite'], 'behat/mink-browserkit-driver' => ['tests'], 'behat/mink-goutte-driver' => ['tests'], 'doctrine/cache' => ['tests'], https://www.drupal.org/node/2682003

Key/Value Attributes of Hook_menu()

How to menu system works in drupal7?

Overview of the menu dispatching process Overview of the router and link building process

How to caching system work in Drupal?

There seems to be a lot of misunderstanding how Drupal caching works. I'm not talking about advanced caching mechanisms like Varnish, APC or memcache here, I'm talking about the core Drupal caching: page cache and block cache which are available in Drupal 7 core. Drupal 6 is similar but works a but differently because page caching was simplified for Drupal 7 (e.g. aggresive mode is now a available as a settings.php var) and the entire system also has become pluggable. Everybody knows this screen and the settings. But what do they exactly mean? What difference do they actually make? What do they do? Here is an attempt to explain the settings at '/admin/config/development/performance'. Page caching: If enables, page caching will only be used for anonymous users. If you enable page caching, the entire HTML of each page will be stored in the database. This significantly reduces the amount of queries needed. This automatically means that blocks will also get cache...

Write JS

(function ($) {   $( document ).ready(function() {     $('.popup-msg').hide();     $('.user-login-for-popup').hide();     $('.commerce-add-to-cart .form-submit').click(function(event){       var $uid = $('.user-login-for-popup').html();       if(!$uid) {         event.preventDefault();         $('.popup-msg').dialog({           maxWidth:600,           maxHeight: 250,           width: 600,           height: 250,           resizable: false,           modal: true,           title: 'You need to login or register to Leaps.',         });       }     });   }); })(jQuery);

How to import node form csv file in drupal7?

Here, I want to import user from csv file in drupal 7 site. Let's create a custom module and name the module as "csv_import". csv_import.info      name = Csv Import    description = Import node from csv file.    core = 7.x    package = Hsk csv_import.module <?php /**  * Implements hook_permission()  */ function csv_import_permission() {   return array(     'administer uploader' => array(       'title' => t('Administer Uploader'),       'description' => t('Allow the following roles to upload csv files to the server.'),     ),   ); } /**  * Implements hook_menu()  */ function csv_import_menu() {   $items['file-uploader'] = array(     'title' => 'Upload a File',     'type' => MENU_CALLBACK,     'description' => 'Import a csv',     'page callback' => 'drupal_get_fo...

How to make own "Condition" and "Action" in rule?

Drupal rules module provide by default events,conditions and action. But we want to create a custom components for additional functionality. Here I will tell you how to create Condition and Action . Let's create a custom module and name the module as "hsk_rule". hsk_rule.info      name = Hsk Rule    description = Hsk Custom Rules    core = 7.x    package = Hsk hsk_rule.module <?php /**  * implementation of hook_rules_condition_info()  */ function hsk_rule_rules_condition_info() {   return array(     'hsk_rule_condition_competition_start_type' => array(       'label' => t('Custom condition for node publish'),       'group' => 'Custom Condition in Rule' ,       'module' => 'hsk_rule',     ),   ); } /**  * Condition competition_start_condition_competition_start_type  */ function hsk_rule_condition_compet...

How to use "hook_theme" and "hook_preprocess" function in drupal7 custom module?

mymodule.module <?php   /**    * Implementation of hook_menu().    */   function mymodule_menu() {     $items['products'] = array(       'title' => 'Products',       'page callback' => 'products_by_categories',       'access callback' => TRUE,       'type' => MENU_CALLBACK,     );     return $items;   }   /**    * Products By Categories Function    */   function products_by_categories() {     return theme('products');   }   /**    * Implements hook_theme().    */   function mymodule_theme() {     return array(       'products' => array(         'variables' => array(),         'template' => 'templates/products-template',       ),         ...

How to use another site database in drupal7?

I am bulding a drupal7 site then i want to fetch some data from another website database. so how to link another website database in drupal 7. There are some tricks which are following: First goto "setting.php" file of drupal 7 site then make some changes in setting.php which are below: <?php   $databases = array(     'drupal_site_database' => array(       'default' => array(         'database' => 'drupalsite',         'username' => 'test',         'password' => 'test',         'host' => 'localhost',         'port' => '',         'driver' => 'mysql',         'prefix' => '',       ),     ),     'another_site_database' => array(       'default' => array(         'database' => 'anothersite', ...

How to use join query in drupal7?

<?php     $query = db_select('xcart_products_categories', 'xpc');     $query->join('xcart_products_lng_en', 'xpd', 'xpc.productid = xpd.productid');     $query->join('xcart_pricing', 'xpp', 'xpc.productid = xpp.productid');     $query->join('xcart_products', 'xp', 'xpc.productid = xp.productid');     $query->condition('xpc.categoryid', $cid);     $query->fields('xpc',array())                ->fields('xpd',array('product','descr','fulldescr'))                ->fields('xpp',array())                ->fields('xp',array('avail','add_date'))                ->orderBy('price', 'ASC');     $xcart_products_info = $query->execute()->fetchAll(); ?>

How to use insert,update and delete query in drupal6 and drupal7?

INSERT STATEMENT <?php   // Drupal 6   db_query("INSERT INTO {mytable} (intvar, stringvar, floatvar) VALUES (%d, '%s', %f)", 5, 'hello world', 3.14);   // Drupal 7   $id = db_insert('mytable')           ->fields(array('intvar' => 5,'stringvar' => 'hello world','floatvar' => 3.14))           ->execute(); ?> UPDATE STATEMENT. <?php   //Drupal 6   db_query("UPDATE {node} SET title='%s', status=%d WHERE uid=%d", 'hello world', 1, 5);   //Drupal 7   db_update('node')     ->fields(array('title' => 'hello world', 'status' => 1))     ->condition('uid', 5)     ->execute(); ?> DELETE STATEMENT. <?php   //Drupal 6   db_query("DELETE FROM {node} WHERE uid=%d AND created < %d", 5, REQUEST_TIME - 3600);   //Drupal 7   db_delete('node')     ->condition('uid', ...

Important Sources of Druapl 8

Official docs Drupal 8 Updates and How to Help Creating Drupal 8.x modules D7 to D8 upgrade tutorial: Pants module Writing module .info.yml files (Drupal 8.x) REST: exposing data as RESTful web services Change records for Drupal core What Drupal 8 API changes are still outstanding D8DX: Improving the D8 developer experience Drupal 8 is Coming Soon! PHP Docs Classes and Objects Code Examples Using Drupal 8's new route controllers Drupal 8 Module Development, Part 1: Getting Started Drupal 8 Module Development, Part 2: Forms Step-by-step: Converting modules from Drupal 7 to Drupal 8 A practical example for converting a Drupal 7 module to work with Drupal 8 Theming in Drupal 8 - Conversion of themes to Twig Twigify converts Drupal 7 phpTemplate themes into Drupal 8 Twig themes Drupal 8 Field API series part 1: field formatters Routing Example in Drupal 8 Controlling Access to Drupal 8 Routes with Access Checks Drupal 8 Field API series part 2: field widget...