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) »
  • Wrapping custom features within CiviCRM -- best way?
Pages: [1]

Author Topic: Wrapping custom features within CiviCRM -- best way?  (Read 2280 times)

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Wrapping custom features within CiviCRM -- best way?
May 06, 2010, 10:09:54 am
I'm developing a Drupal/CiviCRM site that requires several additional tools, which I'm adding via a Drupal module.  To present a consistent interface to the user, I want to wrap all module content with CiviCRM page elements (header, footer, menu, etc.).

I've found a way that "works", but I wonder if this is the best approach.  Simplified code is below, but essentially what I'm doing here is three things:
  • Ensure the module URL is under /civicrm.
  • Present module content as a Smarty template.
  • Have that template included within the template "/templates/CRM/common/drupal.tpl".

This seems pretty clean to me, but any comments from more experienced developers would be appreciated.

Thanks,
Allen


/* Implementation of hook_menu()
 */
function foo_menu() {
    $items['civicrm/foo] = array (
        'title'             => 'Foo',
        'type'              => MENU_CALLBACK,
        'page callback'     => '_foo_call_action',
        'access callback'   => TRUE,
    );
}

function _foo_call_action($args = NULL) {

    // Create a new custom object to handle module-specific logic
    $foo = new foo;

    // Implement whatever the module wants to do here
    $foo->whatever();

    // Get the name of the appropriate template from the module's own collection of templates
    $template = $foo->template;

    // Initialize CiviCRM
    civicrm_initialize( );

    // Get Civi Smarty object
    $tpl = CRM_Core_Smarty::singleton();

    // Add module's own collection of templates to Smarty template_dir array
    $tpl->template_dir[] = $foo->templateDir;

    // Specify that the module's template is the one to be included
    $tpl->assign('tplFile', $template);

    // Have Smarty fetch the output of drupal.tpl, which pulls in all of CiviCRM's common page elements, and includes the file defined in the above line.
    global $civicrm_root;
    $output = $this->tpl->fetch($civicrm_root .'/templates/CRM/common/drupal.tpl');

    return $output;
}
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Wrapping custom features within CiviCRM -- best way?
May 08, 2010, 02:31:03 am
Make sense to me. How do you handle the ACL ?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Wrapping custom features within CiviCRM -- best way?
May 08, 2010, 08:06:30 am
Do you mean how are permissions handled for this custom content?  That's something I'm still working on.

Or, are you referring to a problem I haven't thought of, which may come around and bite me later?  I'm pretty new to Civi.  Can you explain your question a little more?

Thanks,
Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

demeritcowboy

  • Ask me questions
  • ****
  • Posts: 570
  • Karma: 42
  • CiviCRM version: Always the latest!
  • CMS version: Drupal 6 mostly, still evaluating 7.
  • MySQL version: Mix of 5.0 / 5.1 / 5.5
  • PHP version: 5.3, usually on Windows
Re: Wrapping custom features within CiviCRM -- best way?
May 09, 2010, 03:30:32 pm
Maybe I'm misunderstanding what you are trying to do but it sounds like you might find it easier to extend CRM_Core_Form with some custom php/template as described here

http://wiki.civicrm.org/confluence/display/CRMUPCOMING/Customize+Built-in%2C+Profile%2C+Contribution+and+Event+Registration+Screens

except you wouldn't override an existing form, you'd be creating a new form and putting it in your custom path. Then instead of your hook_menu(), you'd also just need to make a small xml file and use this hook to tell civicrm to load it http://wiki.civicrm.org/confluence/display/CRMUPCOMING/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmxmlMenu

Look at one of the existing Menu.xml files to see the format.

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Wrapping custom features within CiviCRM -- best way?
May 10, 2010, 01:39:36 pm
Well, I think you're right.  The "best way" to do this is NOT to create a custom Drupal module that sits next to Civi and hacks it from the outside.  Seems like the more elegant solution is to create this as a CiviCRM component.

This also happens to be the solution with the steeper learning curve.  Guess I've got some decisions to make now, don't I?
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

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: Wrapping custom features within CiviCRM -- best way?
May 10, 2010, 02:26:15 pm

a few comments:

1. you can use civicrm classes in a drupal form :) Check:http://svn.civicrm.org/sfschool/trunk/

2. You can either use drupal's menu system or civicrm's (both are similar). i'd go with the one that u have more experience with

3. you can use either drupal's templating system or civicrm's, see above :)

basically i'd argue that if you know drupal development / hooks, do it the drupal way. easier and much less of a hassle for you (plus a more pleasant experience which makes you and us happy)

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

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Wrapping custom features within CiviCRM -- best way?
May 10, 2010, 02:32:49 pm
Thanks for writing, Lobo. 

Yes, using civicrm classes is very helpful.  For the menu, it makes more sense to work the content into the civicrm menu, as the civi is providing 80% of the features on the site; users will have the civicrm menu for all those features, so I don't want to give them a separate navigation structure for these custom features.  Goal is to make them feel as if it's all the same system.

I can see civicrm is moving toward making components as easy to write and install as modules are for drupal.  Will be nice. 

If I do this as a drupal module, I'll be using a lot less bandwidth on IRC.  ;D

Thanks,
Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

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: Wrapping custom features within CiviCRM -- best way?
May 10, 2010, 02:36:16 pm

i think there are 2 issues here:

a. the menu structure. to a large extent this is not very relevant (IMO). Based on what you are doing, we set breadcrumbs etc, so putting in the right place might be useful.

b. the navigation menu (the little black bar on top). we currently dont have api's to set / check / adjust those entries. We need to add api's so a module can inject menu items there (i need it for the school module)

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

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Wrapping custom features within CiviCRM -- best way?
May 10, 2010, 02:41:14 pm
I notice that CiviCase and other components add their own menu items to the navigation menu.  I might look at mimicking that behavior.  Any clues on where or how that takes place?
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Wrapping custom features within CiviCRM -- best way?
May 10, 2010, 10:29:40 pm
Directly in the DB so far for what I've seen.

CRM/Core/BAO/Navigation.php contains more high level functions (the ones that should be exposed in the API).

If you want to have a go at it, have a look at other APIs in api/v2, you'll see that it's often a thin wrapper around the BAOs

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Wrapping custom features within CiviCRM -- best way?
May 13, 2010, 02:11:36 pm
Quick note to follow up:

I decided to do what (I think) demeritcowboy suggested, which is to create a drupal module that uses hook_civicrm_xmlMenu() to add paths that point to classes extending CRM_Core_Page and CRM_Core_Form.

It's been successful for the most part, though it's very hard to understand what happens when the form is submitted.  Will keep trying and possibly post a new thread if necessary.

Thanks for the help.

- Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Wrapping custom features within CiviCRM -- best way?
May 14, 2010, 04:13:11 am
This should provide you some pointers (granted, not easy to follow the call path ;)

http://civicrm.org/node/98
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Wrapping custom features within CiviCRM -- best way?

This forum was archived on 2017-11-26.