send Variable to Js File From PHP in drupal Custom Module

If we need to use the php variable in our js file we can use Core Drupal.settings Functionality :

 example:

Let we assume we want to use at the page load so, add this is hook_init():

function mymodule_init(){

$token_value = "this is value of your variable";

drupal_add_js(array('mymodule' => array('token' => $token_value)), array('type' => 'setting'));

}

Drupal 8 Opcache Problem with xampp

;Put this code in php.ini (C:\xampp\php\php.ini)

zend_extension=php_opcache.dll

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=0

; The OPcache shared memory storage size.
opcache.memory_consumption=64

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=4

; The maximum number of keys (scripts) in the OPcache hash table.
 Only numbers between 200 and 100000 are allowed.
;opcache.max_accelerated_files=2000

; The maximum percentage of "wasted" memory until a restart is scheduled.
opcache.max_wasted_percentage=5

; When this directive is enabled, the OPcache appends the current working
; directory to the script key, thus eliminating possible collisions between
; files with the same name (basename). Disabling the directive improves
; performance, but may break existing applications.
opcache.use_cwd=1

; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
;opcache.validate_timestamps=1

; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. ("1" means validate once per second, but only
; once per request. "0" means always validate)
;opcache.revalidate_freq=2

; Enables or disables file search in include_path optimization
;opcache.revalidate_path=0

; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
;opcache.save_comments=1

; If disabled, PHPDoc comments are not loaded from SHM, so "Doc Comments"
; may be always stored (save_comments=1), but not loaded by applications
; that don't need them anyway.
;opcache.load_comments=1

; If enabled, a fast shutdown sequence is used for the accelerated code
;opcache.fast_shutdown=0

; Allow file existence override (file_exists, etc.) performance feature.
;opcache.enable_file_override=0

; A bitmask, where each bit enables or disables the appropriate OPcache
; passes
;opcache.optimization_level=0xffffffff

;opcache.inherited_hack=1
;opcache.dups_fix=0

; The location of the OPcache blacklist file (wildcards allowed).
; Each OPcache blacklist file is a text file that holds the names of files
; that should not be accelerated. The file format is to add each filename
; to a new line. The filename may be a full path or just a file prefix
; (i.e., /var/www/x  blacklists all the files and directories in /var/www
; that start with 'x'). Line starting with a ; are ignored (comments).
;opcache.blacklist_filename=

; Allows exclusion of large files from being cached. By default all files
; are cached.
;opcache.max_file_size=0

; Check the cache checksum each N requests.
; The default value of "0" means that the checks are disabled.
;opcache.consistency_checks=0

; How long to wait (in seconds) for a scheduled restart to begin if the cache
; is not being accessed.
;opcache.force_restart_timeout=180

; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=

; All OPcache errors go to the Web server log.
; By default, only fatal errors (level 0) or errors (level 1) are logged.
; You can also enable warnings (level 2), info messages (level 3) or
; debug messages (level 4).
;opcache.log_verbosity_level=1

; Preferred Shared Memory back-end. Leave empty and let the system decide.
;opcache.preferred_memory_model=

; Protect the shared memory from unexpected writing during script execution.
; Useful for internal debugging only.
;opcache.protect_memory=0

Tags

Changing the year range in the form's date element drupal form api

Drupal form api ap usually used to build the custom form to extended the functionality of drupal according to your need.

We can change the year range in date element of form api using following method:

 



function hook_form() {
    $form['date_element'] = array(
        '#type' => 'date',
        '#title' => t('date'),
        '#default_value' => array(
            'day' => format_date(time(), 'custom', 'j'),
            'month' => format_date(time(), 'custom', 'n'),
            'year' => format_date(time(), 'custom', 'Y')),
        '#after_build' => array('__set_year_range'));
}

function __set_year_range($form_element, $form_values) {
$todayYear = date("Y");
    $form_element['year']['#options'] = drupal_map_assoc(range(2000, $todayYear));
    return $form_element;
}
Tags

Bootstrap subtheme dropdown Drupal 7

Steps to create sub theme and mutilevel dropdown :-

1. Download the Bootstrap theme from the drupal sites here.

2. Create the subtheme using cdn folder (/bootsrap/starterkits/cdn) , Copy the cdn folder and place the same level folder as main bootstarp folder (sites/all/themes/cdn).

3. Now rename the cdn to you theme name like BootSmarter in case of drupal.key2goal.com and rename the cdn.starterkit to BootSmarter.info and edit that file to give your theme name or add script etc.

4.enable the theme for appearance admin menu drupal admin side.

5.Now For Mutilevel sub menu create a mutilevel links in main menu .

6. Now go to sites/all/Bootsmarter/template.php and edit, Placed this code 

 Note: Single level dropdown works without this code.

function BootSmarter_menu_tree(&$variables) {

  return '<div class="nav-collapse"><ul class="dropdown-menu multi-level">' . $variables['tree'] . '</ul></div>'; // added the nav-collapse wrapper so you can hide the nav at small size

}

function BootSmarter_menu_link(array $variables) {

  $element = $variables['element'];

  $sub_menu = '';

 

  if ($element['#below']) {

    // Prevent dropdown functions from being added to management menu so it

    // does not affect the navbar module.

    if (($element['#original_link']['menu_name'] == 'management') && (module_exists('navbar'))) {

      $sub_menu = drupal_render($element['#below']);

    }

    //Here we need to change from ==1 to >=1 to allow for multilevel submenus

    elseif ((!empty($element['#original_link']['depth'])) && ($element['#original_link']['depth'] >= 1)) {

      // Add our own wrapper.

      unset($element['#below']['#theme_wrappers']);

      $sub_menu = '<ul class="dropdown-menu sub-menu-'.$element['#original_link']['depth'].'">' . drupal_render($element['#below']) . '</ul>';

      // Generate as standard dropdown.

      $element['#title'] .= ' <span class="caret"></span>';

      $element['#attributes']['class'][] = 'dropdown';

      $element['#localized_options']['html'] = TRUE;

 

      // Set dropdown trigger element to # to prevent inadvertant page loading

      // when a submenu link is clicked.

      $element['#localized_options']['attributes']['data-target'] = '#';

      $element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';

      //comment element bellow if you want your parent menu links to be "clickable"

      //$element['#localized_options']['attributes']['data-toggle'] = 'dropdown';

    }

  }

  // On primary navigation menu, class 'active' is not set on active menu item.

  // @see https://drupal.org/node/1896674

  if (($element['#href'] == $_GET['q'] || ($element['#href'] == '<front>' && drupal_is_front_page())) && (empty($element['#localized_options']['language']))) {

    $element['#attributes']['class'][] = 'active';

  }

  $output = l($element['#title'], $element['#href'], $element['#localized_options']);

  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";

}

function bootgsv_preprocess_page(array &$variables) {

  // Add information about the number of sidebars.

  if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {

    $variables['content_column_class'] = ' class="col-sm-6"';

  }

  elseif (!empty($variables['page']['sidebar_first']) || !empty($variables['page']['sidebar_second'])) {

    $variables['content_column_class'] = ' class="col-sm-8"';

  }

  else {

    $variables['content_column_class'] = ' class="col-sm-12"';

  }

}

7. put the css in style.css also:

 

ul.nav li.dropdown:hover > ul.dropdown-menu {display: block;}

8.Done.

Tags

Drupal 7 content type template

Open the template.php file in your theme for editing.

Look for a function called yourthemename_preprocess_page (replace the yourthemename with your theme’s name).

If this function already exists, you will need to add the if statement to the end of the function just before the closing bracket. Otherwise, you’ll need to create a new function to look like this:

function THEME_preprocess_page(&$variables) {   if (isset($variables['node']->type)) {    // If the content type's machine name is "my_machine_name" the file    // name will be "page--my-machine-name.tpl.php".    $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;    } } 

Now you can create a template file called page--content-type.tpl.php and all nodes with that type will use the new template file.

Thanks

Tags

Update the drupal core using drush

##Drush is your friend

I had used Drush previously so I knew it can be a real time saver. For anyone reading this who is not familiar with Drush, it is a command line tool used to manage a Drupal installation and run all kinds of Drupal commands. Such as: run cron, clear caches, make backups, install modules etc. If you do Drupal development and are not using Drush, you really should. It saves you time and makes working with Drupal more enjoyable by not having to click around the Drupal admin interface that much. Drush is your friend.

In a multisite installation you have to tell Drush which site you want to run the command on (or if you don’t, by default the command will be run on the default site). Luckily there is also a @sites keyword that executes the command on all sites of a multisite installation. It will list all the sites and ask for a confirmation before executing, so you always know when you are running a command on multiple sites.

The Drupal update process

According to Drupal Core update instructions the update process involves more or less the following steps:

-Make a backup

-Put site in maintenance mode

-Update core files

-Run update script to update database

-Disable maintenance mode

Also, it is always best to do a trial run of the complete update process on a test server before updating production environment. There are some Drush commands that can be used to sync a Drupal database to a remote server that help you in managing a mirrored testing server, but those are out of the scope of this post.

I will now describe the Drush commands I used to accomplish the above steps and update my multisite Drupal. Please note that the commands listed here are for Drupal 7 and Drush 5, so the commands may vary for different versions. Most Drush commands also have a shorthand version but I will be using the full commands in this post to better illustrate what each command is doing. Feel free to explore the Drush help/documentation to learn about the shorthands and all options available for each command.

1. Make a backup

To completely backup a Drupal installation, you need to backup the files and the database. Drush has two different commands for backup purposes; archive-dump and sql-dump. The former backs up everything including files and database as the latter only dumps the database. Since the archive-dump command wraps everything in a single file, including Drupal core and all the files, I did not want to run it on all of the 62 sites (and thus getting 62 tarballs with the same Drupal core code base). So I decided to use the sql-dump command in Drush and create one tar file of the whole Drupal root manually.

Here is the command I used to backup all the databases:

drush @sites sql-dump --result-file --gzip

This command will dump all the different databases used by the sites into individual files. The –result-file switch tells Drush to save the dump into a file (using date based filename under ~/drush-backups directory when no filename is specified) and the –gzip switch obviously makes it gzip compressed.

Just for the sake of completeness, here is the command I used to create a tar archive of the Drupal installation:

tar -czvf drupal-backup.tar.gz drupal-root/

2. Put site in maintenance mode

The Drupal maintenance mode (or offline mode as it was called in previous Drupal versions) is saved as a Drupal variable in the database as are any other Drupal options that can be set using the admin interface. To change variable values using Drush we can use the variable-set command. You also need to clear caches for the maintenance mode to take effect, so we also need to use the Drush command cache-clear. Also make sure you are logged in to the sites (at least one that you can use to test) before putting the site into maintenance mode. As a logged in admin user you can see the site normally instead of only the maintenance mode notification so you can verify everything is working correctly after the update.

Here are the commands used to put all the sites into maintenance mode:

drush @sites variable-set maintenance_mode 1
drush @sites cache-clear all

3. Update core files (and modules)

To update Drupal core and also all installed contrib modules (/sites/all/modules), the pm-update command can be used. It downloads the latest version from drupal.org and installs the updated versions. A list of modules to update can also be passed on as argument to this command (and drupal can be used as the module name to update only Drupal core). Since I was feeling a little bit adventurous, I decided to update all contrib modules also at the same time.

Because we only have the core and module files in one place in the filesystem even though they are used by all of the sites, we only need to run this command on one site and not all. Thus we use the default keyword instead of @sites with this command.The pm-update command also runs the database update script, but since we have to run it for all of the sites, we could have also used the pm-updatecode command, which is similar to pm-update except it does not run the database update script.

Here is the command to install the updates:

drush default pm-update

or

drush pm-update

4. Run update script to update database

Now we have the updated versions of Drupal core and contrib modules. Drupal has a system for migrating database changes when updating. Normally when you update a module this is run automatically when you visit the modules page in the admin interface. But when using Drush to install the updates, we have to run it manually on all the sites since they all have separate database. This can be accomplished using the updatedb command as follows:

drush @sites updatedb

And now the update is done, time to test everything is working correctly. Now is the time to revert in case something went wrong. And I mean seriously, test EVERYTHING. It’s not that uncommon to run into issues, especially when using lots of contrib modules. I will outline some issues we ran into in the last chapter.

5. Disable maintenance mode

Ok, so everything seems to be working like a charm (or you have been busy debugging, googling, fixing, pulling out hair and cursing and finally got it working). Time to open the doors to the public again. This is almost identical to step 2, only set the variable value to 0 to disable the maintenance mode. And also remember to clear the cache.

Here are the commands used to disable maintenance mode on all the sites:

drush @sites variable-set maintenance_mode 0
drush @sites cache-clear all

All done!

Congratulations, if you have been following this guide to update a real Drupal installation and got this far, you should now have your Drupal multisite installation running a shiny new version of Drupal and also the latest and greatest of contrib modules.

Thanks

Tags

Drupal 7 Multisite setup

Here is some links will helps to create a multisite of drupal 7 

1. https://www.ostraining.com/blog/drupal/drupal-multi-sites/

2. https://www.drupal.org/docs/7/multisite-drupal/configuring-a-basic-multi-site-development-environment-in-linux

3. https://www.ibm.com/developerworks/library/wa-multisitedrupal/index.html

step 1 : create subdomain and locate path to main domain site folder.

step 2 : create db and db user

step 3 : create a new folder in sites folder with the any name , what you want .

step 4 : copy default.setting.php file  from default folder to inside this newly created folder.

step 5 : rename the default.setting.php file to setting.php of newly created folder.

step 6 : open sites.php file and add the subdomain link correspondent to folder name.

for example :

$sites = array(
    'xx.key2goal.com' => 'default',
    'yy.key2goal.com' => 'yy_folder',
    'zz.key2goal.com' => 'zz_folder',
  );

step 7 : now open url in new tab and execute drupal setting add db name 

step 8: Finaly your multisite setting is completed.

Thanks

Tags