CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Displaying the CiviCRM menu on all Drupal pages
Pages: [1]

Author Topic: Displaying the CiviCRM menu on all Drupal pages  (Read 3286 times)

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Displaying the CiviCRM menu on all Drupal pages
May 31, 2011, 11:33:57 am
Prior to CiviCRM 3.0, the CiviCRM menu was implemented as a Drupal block which could be included on any page processed by Drupal. Beginning in 3.0, the code which loads the menu was moved into a Smarty template. This may have some good implications for maintenance (i.e. you don't have to worry about whether the Drupal blocks are properly configured), but it removes the ability to display the menu pages which aren't generated by Smarty.

To restore the old functionality without making major core changes, I put this quick snippet into a file called civicrm_navblock.module and enabled it:

Code: [Select]
<?php
/**
 * hook_block implementation.
 * 
 * @param string $op
 * @param int    $delta
 * @param array  $edit
 * 
 * @return mixed
 */
function civicrm_navblock_block($op = 'list', $delta = 0, $edit = array ()) {
  switch (
$op) {
    case 
'list':
      
$blocks[0] = array (
        
'info' => t('CiviCRM Menu'),
        
'status' => 1,
        
'visibility' => 1,
        
'region' => 'header',
      );
      return 
$blocks;
    case 
'configure':
      
$form = array();
      return 
$form;
    case 
'save':
      return;
    case 
'view':
      switch (
$delta) {
        case 
0:
          global 
$_civicrm_navblock;
          if (!
user_access('access CiviCRM')) { break; }
          
civicrm_initialize();
          if (empty(
$_civicrm_navblock['content'])) { break; }
          
$block['subject'] = '';
          
$block['content'] = $_civicrm_navblock['content'];
          break;
      }
      return 
$block;
  } 
// switch $op
}

/**
 * Implementation of hook_civicrm_config
 */
function civicrm_navblock_civicrm_config(&$config) {
  global 
$_civicrm_navblock;
  require_once 
'CRM/Core/Smarty.php';
  
$smarty = CRM_Core_Smarty::singleton();
  if (
$smarty->get_template_vars('buildNavigation')) {
    
// Generate markup for use with Drupal bock
    
$_civicrm_navblock['content'] = $smarty->fetch('CRM/common/Navigation.tpl');
    
// Disable rendering in Smarty
    
$smarty->assign('buildNavigation', FALSE);
  }
}

This takes care of loading the menu markup. But the markup doesn't work unless various JS/CSS dependencies are also loaded. This required a small patch to civicrm_html_head() in civicrm.module, i.e.

Code: [Select]
--- drupal/civicrm.module
+++ drupal/civicrm.module
@@ -41,7 +41,7 @@
     }
 
     $head = null;
-    if ( arg(0) == 'civicrm' ) {
+    if ( arg(0) == 'civicrm' || user_access('access CiviCRM')) {
         require_once 'CRM/Core/Config.php';
         $config = CRM_Core_Config::singleton();

If there's a better way, please let me know. If there's no better way, feel free to copy/reuse.

juggleboy

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
Re: Displaying the CiviCRM menu on all Drupal pages
July 05, 2011, 02:47:28 pm
Thanks, worked a treat.

Although just to elaborate, you have to 'create' a drupal .module file with the first snippet of code, and of course add the supporting files that would go with a drupal module to then enable it. For the patch you can just copy the '+' line and paste it, overwriting the existing line in the civicrm.module 

dflasse

  • I post occasionally
  • **
  • Posts: 56
  • Karma: 0
    • Tout peut arriver
Re: Displaying the CiviCRM menu on all Drupal pages
September 29, 2011, 01:49:01 am
It worked great but, oddly, the menu now appears only on the frontpage and not on other pages (even in /civicrm)

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Displaying the CiviCRM menu on all Drupal pages
September 29, 2011, 12:05:01 pm
If this is related to http://forum.civicrm.org/index.php/topic,21646.msg90912.html , then it sounds like a D6/D7 issue. I haven't worked with D7, so this is just a stab in the dark...

The hack involves disabling CiviCRM's normal mechanism for outputting the menu markup and instead using a Drupal block to output the menu markup. If the Drupal block doesn't run correctly, then the markup may never get outputted. This might very well be the case -- e.g. an implementation of hook_block for D6 may not run correctly on D7 because of (subtle?) contract changes. So that's one track you might investigate.

jstn

  • I’m new here
  • *
  • Posts: 10
  • Karma: 0
  • CiviCRM version: 4.0.0
  • CMS version: Drupal 7 Stable
  • MySQL version: Recent Stable?
  • PHP version: PHP 5
Re: Displaying the CiviCRM menu on all Drupal pages
January 17, 2012, 07:18:16 am
This wasn't working for me in Drupal 7. I had split your hook_block code into the appropriate new functions (hook_block_info and hook_block_view) but the block wasn't showing up in the list.

I stumbled upon a work-around though.

Code: [Select]

/**
 * Implementation of hook_civicrm_config
 */
function civi_navblock_civicrm_config(&$config) {
global $_civi_navblock;
require_once 'CRM/Core/Smarty.php';
$smarty = CRM_Core_Smarty::singleton();
if ($smarty->get_template_vars('buildNavigation')) {
// Generate markup for use with Drupal bock
$_civi_navblock['content'] = $smarty->fetch('CRM/common/Navigation.tpl');
}
}

function _civi_navblock_civicrm_menu() {
civicrm_initialize(); //this triggers hook_civicrm_config(), otherwise $_civi_navblock is empty
global $_civi_navblock;
if(isset($_civi_navblock['content']))
echo $_civi_navblock['content'];
}

I have the exact same hook_civicrm_config function, except I get rid of the line that sets buildNavigation to FALSE. I then added this new function (_civi_navblock_civicrm_menu) that outputs the menu to the page. The output was full of javascript that handled the menu for me and put it in the appropriate place. So, if I want to have the CiviCRM menu on one of my Drupal pages I just call this new function.

I also tossed
Code: [Select]
if(!isset($_civi_navblock)) $_civi_navblock = null; at the beginning of the file, just in case.

The civicrm.module file change is still required.

mcgeehon

  • I post occasionally
  • **
  • Posts: 76
  • Karma: 0
  • CiviCRM version: 4.x
  • CMS version: 7.x
  • MySQL version: 5.x
  • PHP version: 5.2.x
Re: Displaying the CiviCRM menu on all Drupal pages
September 19, 2012, 08:36:56 pm
I was able to build the module as listed and enable it.  I cannot however figure out where and how to call the function in the module to generate the civicrm menu on my home page.

Could someone give me a description of which file to modify as an example and where to call this function to get it to print the menu on my homepage.

Thanks

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Displaying the CiviCRM menu on all Drupal pages
February 02, 2013, 12:19:15 pm
We found an unrelated bug in 4.2.7 which involves menu rendering on supported configurations; the fix involves changing how the menu is rendered. If you currently use the technique from this forum post and upgrade to the next release (4.2.8), then you'll need to update civicrm_navblock as well. The new code is simpler (fewer hooks/globals), more performant (doesn't render menu unless absolutely needed), and more flexible (before you could enable the menu on Drupal pages but could never *disable* on civicrm pages; now you can enable or disable on any page).

Issue: http://issues.civicrm.org/jira/browse/CRM-11805
Fix: https://fisheye2.atlassian.com/changelog/CiviCRM?cs=45308
Revised module: https://github.com/totten/civicrm_navblock/tree/7.x-2.x

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Displaying the CiviCRM menu on all Drupal pages

This forum was archived on 2017-11-26.