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 Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • using crmReplace
Pages: [1]

Author Topic: using crmReplace  (Read 1375 times)

lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
using crmReplace
May 23, 2008, 03:32:06 pm
I want to control the tab order on some forms, and thought I could use the crmReplace function (very handy tool) to accomplish that. So I have code in my template such as this:

Code: [Select]
{$form.first_name.html|crmReplace:tabindex:2}
But it's not working. The tabindex is not added to the html. Any idea why? Nothing jumps out at me in the function itself that would indicate why I can't use it to replace/add any attribute to the tag.

-Brian
support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: using crmReplace
May 23, 2008, 06:20:14 pm
Brian - Checking the code for that "plugin" here - CRM/Core/Smarty/plugins/modifier.crmReplace.php - it looks like it ONLY can replace values for existing attributes - but it can't add attributes to an element (despite what the comment implies). I'm not a regular expressions expert - but based on this doc, preg_replace only modifies the subject string if it finds a match on the pattern (attribute in this case).

http://us2.php.net/preg_replace

It would be cool if you could use it to add an attribute if it doesn't exist. I guess it would need to do preg_match first, and then preg_replace or an insert before the $endOfElement depending on if it found something. Wanna give that a shot ?
Protect your investment in CiviCRM by  becoming a Member!

lcdweb

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1620
  • Karma: 116
    • www.lcdservices.biz
  • CiviCRM version: many versions...
  • CMS version: Joomla/Drupal
  • MySQL version: 5.1+
  • PHP version: 5.2+
Re: using crmReplace
May 23, 2008, 07:23:14 pm
Yeah, that's it. Well, I need to figure out a better way to insert the attribute if it doesn't yet exist, but for now, this gets the job done:

Code: [Select]
    $pattern = '/' . $attribute . '="([^"]+?)"/';

if (preg_match( $pattern, $string )) {
    return preg_replace( $pattern, $attribute . '="' . $value . '"', $string );
}
else {
return preg_replace( '/name=/', $attribute . '="' . $value . '" name=', $string );
}
}

I'll look for a better solution.
support CiviCRM through 'make it happen' initiatives!
http://civicrm.org/mih

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: using crmReplace
May 24, 2008, 09:45:29 pm

there is also another smart modifier which we dont use called crmInsert to insert stuff in a form element

check: CRM/Core/Smarty/plugins/modifier.crmInsert.php

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • using crmReplace

This forum was archived on 2017-11-26.