You are here

Code

Custom Google Analytics tracking code.

paulbooker's picture
Submitted by paulbooker on Thu, 07/27/2017 - 11:04

/**
 * @file
 * Custom Google Analytics tracking code.
 */
(function ($) {
  Drupal.behaviors.sc_analytics = {
    attach: function (context, settings) {

      // Register AddThis event listener.
      $(document).ready(function() {
        if (typeof(addthis) != 'undefined') {
          addthis.addEventListener('addthis.menu.share', addthisEventHandler);
        }
      });

      // AddThis.
      function addthisEventHandler(e) {
        ga('send', 'event', 'AddThis', e.data.service, e.data.url);
      }

      // After pressing 'Confirm and Pay' on the Donate Now confiration screen.
      $('#sc-donations-donation-confirmation-form #edit-submit').click(function() {
        ga('send', 'event', 'Online donation', 'submit', 'donate-now');
      });

      // Contact form submit.
      $('#webform-client-form-1').submit(function() {
        ga('send', 'event', 'Contact form submission', 'submit', 'contact-us');
      });

      // Book event.
      $('.button-book-event').click(function() {
        ga('send', 'event', 'Book event', 'submit', location.pathname);
      });

      // Events RSS.
      $('#events-rss').click(function() {
        ga('send', 'event', 'Events RSS', 'click', location.pathname);
      });

      // News RSS.
      $('#news-rss, #blog-rss').click(function() {
        ga('send', 'event', 'News RSS', 'click', location.pathname);
      });

      // Audio files.
      $('.audio-file-link').click(function() {
        ga('send', 'event', 'Audio file play', 'click', $(this).attr('href'));
      });


      // Menu
        
      $('.front #menu-885-1 a.sf-depth-1').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us' );
      });
      
      $('.front #menu-1137-1 a.sf-depth-2').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Welcome' );
      });
      
      $('.front #menu-1152-1 a.sf-depth-2').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Our Purpose and Values' );
      });
      
      $('.front #menu-1160-1 a.sf-depth-2').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Governance' );
      });
      
      $('.front #menu-1037-1 a.sf-depth-3').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Governance | Chapter' );   
      });
       
      $('.front #menu-1161-1 a.sf-depth-3').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Governance | College of Cannons' );   
      });    

      $('.front #menu-1162-1 a.sf-depth-3').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Governance | Cathedral Council' );   
      }); 
      
      $('.front #menu-1163-1 a.sf-depth-3').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Governance | Annual Review and Accounts' );   
      });   
      
      $('.front #menu-1154-1 a.sf-depth-2').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Our People' );
      }); 
      
      $('.front #menu-1957-1 a.sf-depth-2').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Master Plan' );
      }); 
      
      $('.front #menu-2927-1 a.sf-depth-2').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (frontpage)', 'About Us | Safeguarding' );
      }); 
      
      // Accordion Menu
      
      $('.page-node-159 .menu-mlid-1137 a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Welcome' );
      });
      
      $('.page-node-159 .menu-mlid-1152 a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Our Purpose and Values' );
      });
        
      $('.page-node-159 .menu-mlid-1160 a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Governance' );
      });
      
      $('.page-node-159 .accordion-content-3 ul li:nth-child(1) a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Governance | Chapter' );
      });
      
      $('.page-node-159 .accordion-content-3 ul li:nth-child(2) a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Governance | College of Cannons' );
      });

      $('.page-node-159 .accordion-content-3 ul li:nth-child(3) a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Governance | Cathedral Council' );
      });      

      $('.page-node-159 .accordion-content-3 ul li:nth-child(3) a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Governance | Annual Review and Accounts' );
      });   
      
      $('.page-node-159 .menu-mlid-1154 a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Our People' );
      });     

      $('.page-node-159 .menu-mlid-1957 a').click(function() {
        ga('send', 'event', 'Menus', 'Accordion Menu (about-us)', 'Master Plan' );
      });        

      // Menu
      
      $('.page-node-3172 .menu-mlid-1137 a').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Welcome' );
      });
      
      $('.page-node-3172 .menu-mlid-1152 a').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Our Purpose and Values' );
      });
      
      $('.page-node-3172 .menu-mlid-1160 a').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Governance' );
      });
      
      $('.page-node-3172 .menu-mlid-1037 a').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Governance | Chapter' );   
      });
       
      $('.page-node-3172 .menu-mlid-1161 a').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Governance | College of Cannons' );   
      });    

      $('.page-node-3172 .menu-mlid-1162 a').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Governance | Cathedral Council' );   
      }); 
      
      $('.page-node-3172 .menu-mlid-1163 a').click(function() {
	      ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Governance | Annual Review and Accounts' );   
      });   
      
      $('.page-node-3172 .menu-mlid-1154 a').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Our People' );
      }); 
      
      $('.page-node-3172 #menu-mlid-1957 a').click(function() {
        ga('send', 'event', 'Menus', 'Main Menu (about-us/welcome)', 'About Us | Master Plan' );
      }); 
      
      //Block
      
      $('.page-node-3310 #block-block-15 ul li:nth-child(1) a').click(function() {
        ga('send', 'event', 'Menus', 'Block (about-us-governance/chapter)', 'Donate Now' );
      });

      $('.page-node-3310 #block-block-15 ul li:nth-child(2) a').click(function() {
        ga('send', 'event', 'Menus', 'Block (about-us-governance/chapter)', 'Sponsor a Stone' );
      });
      
      $('.page-node-3310 #block-block-15 ul li:nth-child(3) a').click(function() {
        ga('send', 'event', 'Menus', 'Block (about-us-governance/chapter)', 'View Upcoming Events' );
      });
      
      $('.page-node-3310 #block-block-15 ul li:nth-child(4) a').click(function() {
        ga('send', 'event', 'Menus', 'Block (about-us-governance/chapter)', 'Browse our Media Gallery' );
      });
      
      $('.page-node-3310 #block-block-15 ul li:nth-child(5) a').click(function() {
        ga('send', 'event', 'Menus', 'Block (about-us-governance/chapter)', 'Support Us' );
      });

    }
  }
})(jQuery);

Creating and consuming a JSON file

paulbooker's picture
Submitted by paulbooker on Mon, 06/05/2017 - 17:46

Creating a JSON file:

/**
 * Implementation of hook_menu().
 */
function example_module_menu() {
  $items['careers/json'] = array(
    'access callback'   => true,
    'page callback'     => 'example_module_careers', // call function below
    'delivery callback' => 'drupal_json_output' // output as json
  );
  return $items;
}


function example_module_careers() {

  $query = db_select('node', 'n');
  $query->fields('n', array('nid'));
  $query->condition('n.type', 'career', '='); 
  
  $number_of_jobs = $query->countQuery()->execute()->fetchField();

  //print($number_of_jobs);

  $query = db_select('node', 'n');
  $query->leftjoin('field_data_field_quantity', 'fq', 'fq.entity_id = n.nid');
  $query->fields('n', array('nid'));
  $query->fields('fq', array('field_quantity_value'));
  $query->condition('n.type', 'career', '='); 

  $result = $query->execute();
  
  $number_of_positions = 0;
  foreach ($result as $record) {	
	  $number_of_positions += $record->field_quantity_value;
  }
  //print($number_of_positions);
 
  $results = array('number_of_jobs' => $number_of_jobs, 'number_of_positions' => $number_of_positions);
  //print_r($results); 
  //exit;
  return array('results'=>$results,);
  
}

Consuming a JSON file:

if ($json_url) {
  $request = drupal_http_request($json_url);
  $careers = drupal_json_decode($request->data);
  $number_of_jobs = $careers['results']['number_of_jobs'];
  $number_of_positions = $careers['results']['number_of_positions'];
} 

This could be placed inside template.php or inside a Views Global: PHP field.

Adapting an existing Drupal theme for mobile devices.

paulbooker's picture
Submitted by paulbooker on Fri, 05/05/2017 - 18:11
@media screen and (max-width: 680px) {
  #head,
  #page,
  #footer-region {
    width: 100%;
  }
  
  #content,
  #sidebar-primary {
    float: none;
    width: 100%;
  }
  
  #sidebar-primary .block {
    float: none;
    width: 100%;
  }
	
  .sidebar-first #content {
    width: 100%;	
  }	 
	
  #triptych-first, #triptych-second {
    padding: 0;
  }
	
  img {
    max-width: 300px;
    height: auto;
  }
  
  #logo  {
    bottom: auto !important;
    position: inherit !important;
    right: auto !important;
  }  	
}
/**
 * Implements hook_page_alter().
 */
function mytheme_page_alter($page) {
  $viewport = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'viewport',
      'content' => 'width=device-width, initial-scale=1',
    ),
  );
  drupal_add_html_head($viewport, 'viewport');
}

Modifying the meta tag on all Drupal pages.

paulbooker's picture
Submitted by paulbooker on Mon, 10/17/2016 - 12:07
function mymodule_html_head_alter(&$head_elements) {
	  
  $head_elements['system_meta_content_type']['#attributes'] = array(
    'charset' => 'utf-8',
    'http-equiv' => "X-UA-Compatible"
  );	
}

Pages