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) »
  • Trouble with a hook
Pages: [1]

Author Topic: Trouble with a hook  (Read 1464 times)

AlexR

  • I’m new here
  • *
  • Posts: 19
  • Karma: 0
  • CiviCRM version: 4.3.1
  • CMS version: Drupal
  • MySQL version: 5.1.69
  • PHP version: 5.3.23
Trouble with a hook
July 04, 2013, 06:24:29 am
I am trying to make a hook that turns phone numbers into links whenever they are displayed in a form. Here is my code:

Code: [Select]
function nbla_hooks_civicrm_alterContent( &$content, $context, $tplName, &$object ) {

if(isset($object->_formValues)){
$qfKey = $object->_formValues['qfKey'];
$controllerSearch = "CRM_Contact_Controller_Search_{$qfKey}";
}

if(isset($object->_renderer->_tpl->_tpl_vars['session']->_session['CiviCRM'][$controllerSearch]['rows'])){
foreach($object->_renderer->_tpl->_tpl_vars['session']->_session['CiviCRM'][$controllerSearch]['rows'] as $key => $test){
$phoneNumber = $test['phone'];
$contactId = $test['contact_id'];

if(!empty($phoneNumber)){
$object->_renderer->_tpl->_tpl_vars['session']->_session['CiviCRM'][$controllerSearch]['rows'][$key]['phone']="<a href='https://www.google.ca'>{$phoneNumber}</a>";
}
}
}
}

In order to get this code to work, I had to change the variables $_session in CRM/Core/Session.php and $_renderer in CRM/Core/Form.php from protected to public. Will this have any negative implications?

In order to get the links to appear in the form, I have to refresh the page after the page has been generated. Should I be using a different hook?
« Last Edit: July 04, 2013, 07:27:09 am by AlexR »

AlexR

  • I’m new here
  • *
  • Posts: 19
  • Karma: 0
  • CiviCRM version: 4.3.1
  • CMS version: Drupal
  • MySQL version: 5.1.69
  • PHP version: 5.3.23
Re: Trouble with a hook
July 04, 2013, 12:33:49 pm
Before the refresh, the source code shows that there is an anchor tag containing the link.

Code: [Select]
<td><a href='https://www.google.ca'>5065552929</a></td>
After the refresh, when the link actually loads, there are 3 anchor tags.

Code: [Select]
<td><a href='https://www.google.ca'><a href='https://www.google.ca'><a href='https://www.google.ca'>5065552929</a></a></a></td>
I am not quite sure why this is happening.

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Trouble with a hook
July 05, 2013, 04:13:04 am
I think you are looping through all the controller rows, right?
So does it not add the link even if it does not get a phone number? Mind you, I have only glanced....
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Trouble with a hook
July 05, 2013, 04:48:03 am
Perhaps it would be easier to do this with JS...
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

AlexR

  • I’m new here
  • *
  • Posts: 19
  • Karma: 0
  • CiviCRM version: 4.3.1
  • CMS version: Drupal
  • MySQL version: 5.1.69
  • PHP version: 5.3.23
Re: Trouble with a hook
July 05, 2013, 11:00:39 am
I was hoping to get away with using a hook to cover all instances.

I ended up creating custom extra.tpl files such as to not alter the core.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Trouble with a hook

This forum was archived on 2017-11-26.