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) »
  • Integrating Civi Profiles with the Drupal menu system recipe
Pages: [1]

Author Topic: Integrating Civi Profiles with the Drupal menu system recipe  (Read 1279 times)

ToddW

  • I’m new here
  • *
  • Posts: 16
  • Karma: 1
Integrating Civi Profiles with the Drupal menu system recipe
April 12, 2011, 10:29:55 am
How do you get a CiviCRM profile (search/listings/details) to appear like a standard Drupal page within the Drupal menu system? Seems like this topic comes up quite a bit, and I haven't found a solution that I'm satisifed with. So, this may be redundant, but here's the recipe I came up with:

I am building a site for a regional organization of churches. They want a Find a Church feature with zipcode proximity search and a few other filters, plus mapping. So, I've built my Civi Profile and got it working, the mapping feature works like a charm. So, how do I add it to the public website?

1) Create a Drupal page called "Find a Church", use this for the body:
Quote
<?php drupal_goto('civicrm/profile',array('force'=>1,'gid'=>9,'reset'=>1)); ?>
You'll have to enable PHP input module and set your gid to the correct id for your profile.
2) This will be the parent menu item/link for your profile pages, add it to the menu where you want your profile to appear.
3) Once you save this page, it's always going to redirect when you view it, so if you need edit it, you'll have to get to it from the content list in the admin area.
4) Now create a custom module if you don't already have one, and add this snippet of code to hook_init function like so:
Quote
/**
 * Implementation of hook_init()
 */
function my_tweaks_init()
{   
  if ( (arg(1) == 'profile') && ($_REQUEST['gid'] == 9)) { //church search/listing
    $item = menu_get_item();
    $item['href'] = 'node/339';
    menu_set_item(NULL, $item); //civicrm/profile?force=1&gid=9&reset=1
  }
}
And be sure to change gid 9 to your profile id.
Also, node/339 refers to my Find a Church page that I just created. Find the node ID from the content list (or looking at the db).

That's it. It's not elegant, but it's a pretty small amount of code and doesn't require additional modules or hacks to the core like I've seen elsewhere.

Note: I tried to use the hook_civicrm_pageRun hook instead of hook_init, but it was never invoked for some reason.

A long term dynamic strategy would be to build/extend something like the Menu Trails module, where the user can choose profile IDs from Civi instead of content types for rerouting.


-----------------------
As a followup, I think I published this too soon. Looks like the gid isn't always present in the URL if the search form is submitted, so we have to check session data in the init hook. Something ugly like this:

Quote
   //check for civcirm session with gid or query param with gid = 9
   if ( arg(0) == 'civicrm') {
      if ( arg(1) == 'profile' ) {
           $gid = 0;
          
           if ( isset($_REQUEST['gid']) ) {
              $gid = 9;
           } else if ( isset($_REQUEST['qfKey'])) {
              $q = db_query("SELECT data from civicrm_cache WHERE path = '%s' LIMIT 1", "CiviCRM_CRM_Profile_Form_Search_".$_REQUEST['qfKey']);
         
                  if ( $q && $d = db_fetch_array($q) ) {
                    $data = unserialize($d['data']);
                    $gid = @$data['gid'];
                  }
           }
          
            if ( (arg(1) == 'profile') && ($gid == 9)) { //church search/listing
             $item = menu_get_item();
             $item['href'] = 'node/339';
             menu_set_item(NULL, $item); //civicrm/profile?force=1&gid=9&reset=1
         
            }
      }
   }
:'(
« Last Edit: April 12, 2011, 02:08:39 pm by ToddW »

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Integrating Civi Profiles with the Drupal menu system recipe
April 12, 2011, 02:52:47 pm

hey todd:

would be great if you could investigate the menu trails module and see if we can extend it to include civicrm profile / contribution / registration and other pages

thanx

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

ToddW

  • I’m new here
  • *
  • Posts: 16
  • Karma: 1
Re: Integrating Civi Profiles with the Drupal menu system recipe
August 15, 2011, 01:52:33 pm
Instead of added the drupal_goto directly in the page content. It's probably better form to actually register a handler in Drupal's menu system via the custom module:

/**
 * Implementation of hook_menu().
 */
function my_tweaks_menu() {
  $items = array();

  $items['find-church'] = array(
    'title' => t('Find a Church'),
    'page callback' => ' my_tweaks_redirect',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

function  my_tweaks_redirect() {
  drupal_goto('civicrm/profile',array('force'=>1,'gid'=>9,'reset'=>1, 'crmSID'=>'2_d'));
}

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Integrating Civi Profiles with the Drupal menu system recipe
September 04, 2011, 04:28:34 am
What I have done when I have a similar requirement:

I create a standard Drupal menu item using the UI, and provide the URL for the menu item as "civicrm/profile?reset=1&gid=8&force=1"   

What are the advantages of your recipe over this?
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Integrating Civi Profiles with the Drupal menu system recipe
September 04, 2011, 03:57:58 pm
If you're not a fan of wonky-looking urls, then you might also try http://forum.civicrm.org/index.php?topic=19532.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Integrating Civi Profiles with the Drupal menu system recipe

This forum was archived on 2017-11-26.