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) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • HOWTO: create URL Aliases for CiviCRM forms
Pages: [1]

Author Topic: HOWTO: create URL Aliases for CiviCRM forms  (Read 4905 times)

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
HOWTO: create URL Aliases for CiviCRM forms
January 28, 2011, 12:10:46 am
CiviCRM Forms have URLs like 'civicrm/contribute/transact?reset=1&id=1'. If you want to make this accessible by the name 'donate' you'll soon discover that Drupal's URL Aliases module doesn't support the arguments (indicated by the '?' and '&' characters). This HOWTO should help. The downside is you'll need to add a module and hack a Drupal core file (sorry).

Install the URL Alter module and add the following code for custom_url_rewrite_inbound based on instructions at http://drupal.org/node/118072#comment-2857522 (and extending that code to match both '?' and '&' characters)...

Code: [Select]
/**
 * Allows query strings in URL aliases
 *  : http://drupal.org/node/118072#comment-2857522
 */
  $parts = preg_split('/[\?\&]/', $result);

  if (count($parts) > 1) {
    $result = array_shift($parts);

    foreach ($parts as $part) {
      list($key, $value) = explode('=', $part);
      $_GET[$key] = $value;
    }
  }

A side-effect of this change is that when creating menu items which refer to CiviCRM pages, the arguments get stripped (eg, the event id on a registration page). (When you save a menu item, Drupal normalises the path, which strips the arguments.)

Make the following change to modules/menu/menu.admin.inc to prevent the query arguments from being stripped from menu items.

Code: [Select]
/**
   * Validate form values for a menu link being added or edited.
   */
  function menu_edit_item_validate($form, &$form_state) {
    $item = &$form_state['values']['menu'];
 -  $normal_path = drupal_get_normal_path($item['link_path']);
 +  /*
 +   * drupal_get_normal_path() calls custom_url_rewrite_inbound()
 +   * which will remove any query arguments from the URL.  So
 +   * replace  the call  to  drupal_get_normal_path()  with the
 +   * code from that function, sans custom_url_rewrite_inbound().
 +   *
 +   * $normal_path = drupal_get_normal_path($item['link_path']);
 +   */
 +  $normal_path = $item['link_path'];
 +  if ($src = drupal_lookup_path('source', $item['link_path'], '')) {
 +    $normal_path = $src;
 +  } 
    if ($item['link_path'] != $normal_path) {

As this is a hack of core Drupal, you'll need to fix this every time you upgrade to a new version.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: HOWTO: create URL Aliases for CiviCRM forms
January 28, 2011, 03:08:00 am
Hi,

Is there a hook already to alter the url composition in civi ?

so instead of generating the ugly civicrm/contribute/transact?reset=1&id=1 it can directly be /donate/1 ?

(and the module would need to manage the menu as well to call the right civi code obviously).

I'd like to be able to have nicer nice urls for the key part of civi. Might more be something to add for the 4.x wishlist that hacking something now

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

acbs

  • I’m new here
  • *
  • Posts: 2
  • Karma: 0
  • CiviCRM version: 3.3.2
  • CMS version: Drupal
  • MySQL version: 5.0.51a
  • PHP version: 5.2.4-2ubuntu5.12
Re: HOWTO: create URL Aliases for CiviCRM forms
January 28, 2011, 06:15:48 am
Thanks much for this reply. I agree with Xavier, this would be a nice core feature. As I know folks have enjoyed similar functionality in Drupal.


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: HOWTO: create URL Aliases for CiviCRM forms
January 28, 2011, 06:35:51 am

xavier and/or acbs:

wanna work on this and post a patch to make this happen?

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: HOWTO: create URL Aliases for CiviCRM forms
January 28, 2011, 08:31:55 am
Hi,

Will look at what kurund did for the export, see if I can see how I can apply it to add a hook on crmUrl functions... and/or IRC.

acbs, could you code the other part (using the hook to alter the url + handle the new url to call the right civicrm functions) ? (or financial contribution to make it happen) ?

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

mirrorstage

  • I post occasionally
  • **
  • Posts: 57
  • Karma: 1
  • CiviCRM version: 4.6.7
  • CMS version: Drupal 6.37
  • MySQL version: 5.6.23
  • PHP version: 5.4.42
Re: HOWTO: create URL Aliases for CiviCRM forms
February 07, 2011, 05:13:55 pm
I'm just a little unclear about where exactly to add the code for custom_url_rewrite_inbound. Does this go in the settings file? Or .htaccess? Or to modules/menu/menu.admin.inc?

I feel silly for not understanding in which file the first bit of code is supposed to go, but I keep reading and re-reading the instructions and it's not clear to me. I'd love to transform the ugly civicrm URLs into pretty drupal URLs, but the steps are not quite laid out in a straightforward enough manner for me.

Thanks for clarifying!

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: HOWTO: create URL Aliases for CiviCRM forms
February 07, 2011, 09:55:18 pm
sorry not sure I follow you. what do you mean by inbound?

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

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: HOWTO: create URL Aliases for CiviCRM forms
February 08, 2011, 01:24:37 am
@mirrorstage,

Check out the URL Alter module for Drupal. See http://drupal.org/project/url_alter for details.

The conversation between Lobo and Xavier on this post is an exciting development that, if implemented, would make my 'hack' obsolete.

Ken

nelslynn

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
  • CiviCRM version: 3.3
  • CMS version: Drupal 6.20
  • MySQL version: 5
  • PHP version: 5
Re: HOWTO: create URL Aliases for CiviCRM forms
February 23, 2011, 04:15:46 pm
WHAT is with the [code link]? I get the message:
"Sorry, you are not allowed to post external links."
whenever I try to wrap code... grrrr. FWIW, that captcha is the most annoying! Can anyone read those characters?

[end rant]

Anyway....

I get this error message when I install the url_alter and apply fix in message 1.
Parse error: syntax error, unexpected T_VARIABLE in /home/bcaamno/public_html/modules/menu/menu.admin.inc on line 346

Line 246 is:    $normal_path = $item['link_path'];

My full modified menu_edit_item_validate function is:

function menu_edit_item_validate($form, &$form_state) {
  $item = &$form_state['values']['menu'];
  //$normal_path = drupal_get_normal_path($item['link_path']);
  /*
    * drupal_get_normal_path() calls custom_url_rewrite_inbound()
    * which will remove any query arguments from the URL.  So
    * replace  the call  to  drupal_get_normal_path()  with the
    * code from that function, sans custom_url_rewrite_inbound().
    *
    * $normal_path = drupal_get_normal_path($item['link_path']);
    */
   $normal_path = $item['link_path'];
   if ($src = drupal_lookup_path('source', $item['link_path'], '')) {
     $normal_path = $src;
   }
  if ($item['link_path'] != $normal_path) {
    drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $item['link_path'], '%normal_path' => $normal_path)));
    $item['link_path'] = $normal_path;
  }
  if (!menu_path_is_external($item['link_path'])) {
    $parsed_link = parse_url($item['link_path']);
    if (isset($parsed_link['query'])) {
      $item['options']['query'] = $parsed_link['query'];
    }
    else {
      // Use unset() rather than setting to empty string
      // to avoid redundant serialized data being stored.
      unset($item['options']['query']);
    }
    if (isset($parsed_link['fragment'])) {
      $item['options']['fragment'] = $parsed_link['fragment'];
    }
    else {
      unset($item['options']['fragment']);
    }
    if ($item['link_path'] != $parsed_link['path']) {
      $item['link_path'] = $parsed_link['path'];
    }
  }
  if (!trim($item['link_path']) || !menu_valid_path($item)) {
    form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $item['link_path'])));
  }
}

Using Drupal 6.20 and CiviCRM 3.3.2
« Last Edit: February 23, 2011, 05:49:16 pm by nelslynn »

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: HOWTO: create URL Aliases for CiviCRM forms
February 23, 2011, 08:48:30 pm
I can't tell why you'd get that error. The only between your code and my change that I can see is that you've commented out a line rather than removing it.

  //$normal_path = drupal_get_normal_path($item['link_path']);

But I can't see why that would cause a compile error.

Can you try removing code (prior to line 246) to see if the error goes away? Check all your lines end in semicolon (they seem to).

Ken

elmobile

  • I’m new here
  • *
  • Posts: 13
  • Karma: 0
    • elMobile Inc.
  • CiviCRM version: 3.1, 3.3, 3.4, 4.1
  • CMS version: Drupal 6.16, 6.22, 7 / Joomla 1.5, 2.5
Re: HOWTO: create URL Aliases for CiviCRM forms
August 18, 2011, 09:33:39 am
Hi ken,

Does your solution work on versions older than CiviCRM 3.4.x and Drupal 6.22?

I have a project which needs this feature but they are currently on
CiviCRM 3.1
Drupal 6.16
.

Maybe we should upgrade the Drupal and CiviCRM to make the clean URL work?

Do you know other Drupal modules might be incompatible with the URL_Alter ad CiviCRM?

Currently we have Twitter, Views, and Webform modules at the Drupal site.

Thanks!

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: HOWTO: create URL Aliases for CiviCRM forms
August 18, 2011, 02:40:05 pm
@elmobile,

You should, of course, try this on a test server!

Re: versions - the fix doesn't depend on the CiviCRM version, and the Drupal version should be fine.

Re: other Drupal modules - I know of an issue with Drupal for Facebook which competes with URL Alter in wanting to set custom_url_rewrite_inbound(). We also use Twitter, Views, and Webform, so they are OK. Other Drupal modules don't use arguments, so you're unlikely to have difficulties.

Ken

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • HOWTO: create URL Aliases for CiviCRM forms

This forum was archived on 2017-11-26.