Drupal 7

Warning message

JQuery Cycle must be installed in order to run the slideshow. Please go to Status Report for instructions.

By default drupal date module stores all the values in the UTC format and to change it into the current local time format you can do something suggested in this post for the conversion :

http://techrappers.com/post/69/how-convert-utc-dates-local-time-php

The above post will give you exact local time after conversion which can be used in sql query for comparison.

Nice Menu module of drupal by default provide hover support of mouse. This tutorial will show you how to add an on-click event support for nice menus.
This post also solves the issue listed over here https://www.drupal.org/node/352711

1) Override the default superfish.js provided with the nice menus module. Nice menus superfish javascript provide the on-hover support but you have to override with the new superfish.js which provide the on-click support.
To do this create a custom module in your custom directory and then write the following code :

/**
 * Implements hook_library_alter().
 */
function YOUR_MODULE_NAME_library_alter(&$libraries, $module) {  
  // Change the existing superfish hover menu to superfish click menu for nice menus.
  if ($module == 'nice_menus' && isset($libraries['superfish'])) {
      $libraries['superfish']['js'] = array(
        drupal_get_path('module', 'nice_menu_onclick') . '/js/superfish_onclick.js' => array(),
      );
  }
}

2) Create a folder name "js" and inside your folder drop/create new file named superfish_onclick.js with following code :

/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

(function($){
    $.fn.superfish = function(op){

        var sf = $.fn.superfish,
            c = sf.c,
            $arrow = $([' »'].join('')),
            over = function(){
                var $this = $(this), menu = getMenu($this);
                clearTimeout(menu.sfTimer);
                $this.showSuperfishUl().siblings().hideSuperfishUl();
            },
            out = function(){
                var $this = $(this), menu = getMenu($this), o = sf.op;
                clearTimeout(menu.sfTimer);
                menu.sfTimer=setTimeout(function(){
                    o.retainPath=($.inArray($this[0],o.$path)>-1);
                    $this.hideSuperfishUl();
                    if (o.$path.length && $this.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
                },o.delay);
            },
            getMenu = function($menu){
                var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
                sf.op = sf.o[menu.serial];
                return menu;
            },
            addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };

        return this.each(function() {
          var $this = $(this);
            var s = this.serial = sf.o.length;
            var o = $.extend({},sf.defaults,op);
            o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
                $(this).addClass([o.hoverClass,c.bcClass].join(' '))
                    .filter('li:has(ul)').removeClass(o.pathClass);
            });
            sf.o[s] = sf.op = o;
      // CHANGED: by KARL SWEDBERG
            if ( (o.eventType === 'hoverIntent' && !$.fn.hoverIntent) || !(/^(?:hover|hoverIntent|toggle)$/).test(o.eventType) ) {
              o.eventType = 'hover';
            }
            $this.find('li:has(ul)')[o.eventType](over,out).each(function() {
                if (o.autoArrows) {
                  addArrow( $('>a:first-child',this) );
                 // this.addClass("yourClass");
                }
            })
            .not('.'+c.bcClass)
                .hideSuperfishUl();


            $this.find('a').each(function(i){
                var $a = $(this), $li = $a.parents('li');
                $a.focus(function(){over.call($li);}).blur(function(){out.call($li);});
                $a.click(function(event) {
                  event.preventDefault();
                  if ( !$a.hasClass("sf-with-ul") ) {
                    location.href = this.href;
                  }
                });
            });
            o.onInit.call(this);

        }).each(function() {
            var menuClasses = [c.menuClass];
            if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) {
              menuClasses.push(c.shadowClass);
            }
            $(this).addClass(menuClasses.join(' '));
        });
    };

    var sf = $.fn.superfish;
    sf.o = [];
    sf.op = {};
    sf.IE7fix = function(){
        var o = sf.op;
        if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined) {
            this.toggleClass(sf.c.shadowClass+'-off');
        }
        };
    sf.c = {
        bcClass     : 'sf-breadcrumb',
        menuClass   : 'sf-js-enabled',
        anchorClass : 'sf-with-ul',
        arrowClass  : 'sf-sub-indicator',
        shadowClass : 'sf-shadow'
    };
    sf.defaults = {
        hoverClass  : 'sfHover',
        pathClass   : 'overideThisToUse',
        pathLevels  : 1,
        delay       : 0,
        animation   : {opacity:'show'},
        speed       : 'normal',
        closeAnimation: {opacity: 'hide'},
        closeSpeed: 0,
        autoArrows  : true,
        dropShadows : true,
    // CHANGED: by KARL SWEDBERG
        eventType   : 'toggle', // one of 'toggle', 'hover', or 'hoverIntent'
    // disableHI  : false,    // true disables hoverIntent detection
        onInit      : function(){}, // callback functions
        onBeforeShow: function(){},
        onShow      : function(){},
        onHide      : function(){}
    };
    $.fn.extend({
        hideSuperfishUl : function(){
            var o = sf.op,
                not = (o.retainPath===true) ? o.$path : '';
            o.retainPath = false;
            var $closingLi = $(['li.',o.hoverClass].join(''),this).add(this).not(not);
            var $ul = $closingLi
                    .find('>ul');
            $ul.animate(o.closeAnimation, o.closeSpeed, function() {
              $closingLi.removeClass(o.hoverClass);
        $ul.css('visibility','hidden');
      });
            o.onHide.call($ul);
            return this;
        },
        showSuperfishUl : function(){
            var o = sf.op,
                sh = sf.c.shadowClass+'-off',
                $ul = this.addClass(o.hoverClass)
                    .find('>ul:hidden').css('visibility','visible');
            sf.IE7fix.call($ul);
            o.onBeforeShow.call($ul);
            $ul.show(0,function(){ sf.IE7fix.call($ul); o.onShow.call($ul);});
            return this;
        }
    });

})(jQuery);

3) Open the template.php of your theme folder and add the following code in that :

function YOUR_THEME_NAME_menu_link(array $variables) {
    $element = $variables['element'];
    $sub_menu = '';

    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }
    if ($element['#href'] == t('http://#')) {
        $output = '' . $element['#title'] . '';
    } else {
        $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    }
     return "
  • " . $output . $sub_menu . "
  • \n"; }

    4) Now go to your menu which you have added as nice menu in blocks. In the path of the parent menu item put "http://#" (without quotes) and save it. This is because when you click on the parent menu, that should not contain any link it should be javascript void(0). To accomplish that in Step 3 we have told drupal that if they find "http://#" in the link path then make that path non-clickable.

    5) Flush your cache.

    Please see the attached module. You can simply drop this folder in your custom modules, but you have to do Step 3 and 4 manually.

    To display the row number generally we can use the views "Global: View result counter (ID)" field in the view but if your view is grouped by
    any specific field then it will start displaying the wrong row number. It will show the on the basis of the latest uploaded content.
    You can correct this by using PHP and its quite straight forward.
    For this you have to create a template inside your theme/templates folders.

    1) Name the template
    Go to your view -> click on Theme: Information -> Under Style output section, choose a view name which represent your view.
    In my case it was a block with name block_1 and view name "document" so i choose " views-view-table--document--block-1.tpl.php" to be
    very specific to that block.

    2) Create a Global: View result counter (ID) field in your view if you haven't created it.

    3) Copy the below code and put it inside your template file.
    Below code starts a counter variable and search for "Global: View result counter (ID)" field whose machine name is "counter" and replace it with our counter value. By this way we will get the advantage of displaying all the classes also, which view was already displaying.

    /**
     * @file
     * Template to display a view as a table.
     *
     * - $title : The title of this group of rows.  May be empty.
     * - $header: An array of header labels keyed by field id.
     * - $caption: The caption for this table. May be empty.
     * - $header_classes: An array of header classes keyed by field id.
     * - $fields: An array of CSS IDs to use for each field id.
     * - $classes: A class or classes to apply to the table, based on settings.
     * - $row_classes: An array of classes to apply to each row, indexed by row
     *   number. This matches the index in $rows.
     * - $rows: An array of row items. Each row is an array of content.
     *   $rows are keyed by row number, fields within rows are keyed by field ID.
     * - $field_classes: An array of classes to apply to each field, indexed by
     *   field id, then row number. This matches the index in $rows.
     * @ingroup views_templates
     */
    ?>
    <?php print $attributes; ?>>
       <?php if (!empty($title) || !empty($caption)) : ?>
         <?php print $caption . $title; ?>
      <?php endif; ?>
      <?php if (!empty($header)) : ?>
        
          
            <?php foreach ($header as $field => $label): ?>
              >
                <?php print $label; ?>
              
            <?php endforeach; ?>
          
        
      <?php endif; ?>
      
        <?php $count = 1;
         foreach ($rows as $row_count => $row): ?>
          >
            <?php foreach ($row as $field => $content): ?>
              <?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
                <?php if($field == 'counter') {
                	  print $count;
                	}
                	else{
                		print $content;
                	} ?>
              
            <?php endforeach; ?>
          
        <?php $count++;
        endforeach; ?>
      
    

    4) Flush the cache.

    To hide the views on a page programatically, you have to use hook_views_pre_view. This is useful when you have same view rendering on a page more than one time and you have to hide one of the display mode. For example, it might be possible that your page is rendering a view and a block of a view . Then you might want to hide that view and display a block only.

    /**
     * Implementation of hook_views_pre_view().
     */
    function YOUR_MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
      //Check the view name and display mode of view, then hide the view.
     // Display id is the machine name of the view or block inside a view
       if ($view->name == 'view_machine_name' && $display_id == 'document_page') {
    		$view->executed = TRUE;
       }
     }
    

    First Create a function hook_form_alter.Then inside hook_form_alter depending upon your choice :
    a) If you want to disable field (it will be visible, but user can't change it) :

    $form['field_myfield']['#disabled'] = TRUE;

    b) If you want it to be hidden, but to keep value it has before editing the way to do that is:

    $form['field_myfield']['#access'] = FALSE;

    c) if you want to hide it:

    hide($form['field_myfield']);

    If you want to have a taxonomy for countries inside your website, you can download the XML file attached with this post and then download the Taxonomy Import module from here https://www.drupal.org/project/taxonomy_xml.
    After downloading, you can see two new tabs for import/export on taxonomies page.
    Go to import -> Target Vocabulary (Select "Your Desktop Direct File Upload") -> Browse file to upload -> From dropdown select format of file to XML -> then click on import.

    You can see that a new vocabulary has been created, then later you can change the name of vocabulary .

    Download File: 

    Suppose you are using drupal entity form rather than a webform, because you would like to group the fields and take advantage of add more button. Using entity form is also useful when you have to interact with drupal core node fields. Below code is useful if you want to get the values of fields inside a particular entity form.

      $entityform_id = 1; // entity form id
      $entityform = entityform_load($entityform_id);
      $entityform_wrapper = entity_metadata_wrapper('entityform', $entityform);
      // Access fields with $entityform_wrapper->field_name->value() or ->field_name->raw()
      $field_my_field = $entityform_wrapper->field_name->value(); // for example
    
      // If you have single field 
      drupal_set_message($entityform_wrapper->field_my_field->value()); // for example
    
      // if you have mutilple fields using field collection and field_my_field is a group of fields from field collection
      foreach ($field_my_field as $field_my_field_key => $field_my_field_value) {
        drupal_set_message($field_my_field_value->you_field_name[LANGUAGE_NONE][0]['value']);
      }
    

    This is the error due to entity reference module. Please apply the patch https://drupal.org/files/entityreference-1459540-47-workaround-fatal-error.patch and then run the update.php i.e
    yourwebsite.com/update.php to solve this problem.

    If you have a content type where you have comments enabled/disabled and later you have decided to ON/OFF the comments but the problem is that if you have disabled/enabled the comments from the content type, it does not turn off/on the comments on existing nodes which you have made before. 

    To turn off the comments you have to update 2 tables not just one . These are :

    1. node
    2. node_revision

    So run the query :

    UPDATE node_revision, node SET node_revision.comment = 0 WHERE node_revision.nid = node.nid AND node.type = "story";

    Where comment values can be 0,1,2.

    0 = no, 1 = closed (read only), 2 = open .

    To show the right images while sharing the links on facebook, you have to tell the Facebook which image to choose on the given webpage. As one webpage can contain many images and choosing the right image for right post can increase your website traffic.

    To do this you have to turn your webpage into graph objects . This is called Open Graph Protocol .

    To turn your webpage into graph objects you have to place additional <meta> tags into <head> section of your webpage.

    • og:title - The title of your object as it should appear within the graph, e.g., "The Rock".
    • og:type - The type of your object, e.g., "video.movie". Depending on the type you specify, other properties may also be required.
    • og:image - An image URL which should represent your object within the graph.
    • og:url - The canonical URL of your object that will be used as its permanent ID in the graph, e.g., "http://www.imdb.com/title/tt0117500/".

    However, to do this in Drupal there is a module called Open Graphs Meta tags. This will add a separate section into your node in which you can define, which image to choose while sharing the links 

    and other options also .

    https://drupal.org/project/opengraph_meta

     

    Facebook Open Graph

    Pages

    Subscribe to RSS - Drupal 7