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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • I cannot get contact_id in hook_civicrm_buildForm
Pages: [1]

Author Topic: I cannot get contact_id in hook_civicrm_buildForm  (Read 961 times)

Luciano S.

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 2
  • iXiam Team Leader
  • CiviCRM version: 4.2+ / 4.3+ / 4.4+
  • CMS version: Drupal
  • MySQL version: 5.1+ / 5.5+
  • PHP version: 5.3+ / 5.4+
I cannot get contact_id in hook_civicrm_buildForm
January 03, 2013, 05:40:02 am
Hi guys,

Happy new year to everyone!!

I'm facing an issue that is annoying me a lot. I want to set a default value to a custom field associated with an activity type, based on the membership contact's info, when that activity is created.
Looks easy  :)

The hook_civicrm_buildForm should solve the problem, but the issue is when the buildForm hook is executed (actually it's executed 2 times, first when the Activity Form is loaded and second when the custom group of that form is loaded) I cannot get the contact_id to use it within the API to get my default value.

it is not present in the $form object. (It is present in the $form object in the first execution of the hook regarding Activity Form, but there I don't have the custom fields to set vaules)
it is not present in $_GET object. That's weird, because the cid parameter is in the querystring, but when I try to get it and doesn't exists in the array. Must be because the custom fields are called by AJAX or so

running out of ideas..
So where Can I get the contact_id in buildForm Hook??

cheers!

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: I cannot get contact_id in hook_civicrm_buildForm
January 03, 2013, 07:40:50 am

not sure what contact id you want, but u can also use:

$form->getVar( 'sourceContactId' );
$form->getVar('targetContactId');
$form->getVar('asigneeContactId');

you can check this file:

CRM/Activity/Form/Activity.php

for the protected variables

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

Luciano S.

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 2
  • iXiam Team Leader
  • CiviCRM version: 4.2+ / 4.3+ / 4.4+
  • CMS version: Drupal
  • MySQL version: 5.1+ / 5.5+
  • PHP version: 5.3+ / 5.4+
Re: I cannot get contact_id in hook_civicrm_buildForm
January 03, 2013, 09:49:41 am
Hi Lobo,

the contact Id I want is the _targetContactId of the activity

The problem is that the buildForm hook is called two times.
The first is called when the Activity Form is loaded. There I have in the $form object all the contactId variables you mentioned with values, but the custom fields are not already populated, so I cannot set the default values.
The second time is called when the custom fields are populated (I think the hook is called once per custom group you have in the form, in my case only one). At that time the $form contact variables are blank. The $form object it's a different instance of the first call.

So my problem is that in the first execution of the buildForm hook, I have the contactId value I need, but not the custom fields
And in the second, I have the custom fields, but not the contactId value.

that makes sense?




totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: I cannot get contact_id in hook_civicrm_buildForm
January 03, 2013, 01:04:35 pm
If you haven't before, you can try installing the Drupal modules "devel" and "civicrm_developer":

https://github.com/eileenmcnaughton/civicrm_developer

This will provide an onscreen display of most CiviCRM hook invocations. (With AJAX, it can be a little wonky. The main thing to note is that "devel" outputs using session messages. When processing an AJAX call, the session messages may not be immediately displayed. Instead, they show up a minute later in the next full pageview.)

Since the second page-request has access to the custom-data, it may have access to the activity ID (or, in the context of custom data, it may be called the entity_id). You can use the activity ID to look up the target contact ID(s) in the database.

As an aside, I don't know your use-case, but you might want to bear in consider (a) the different pageflows that lead to the activity form (e.g. creating new activity vs editing existing activity; going through the navbar vs the activity search vs the contact's "Activities" tab) and (b) what happens when a user edits the source/target/assignee fields. If the goal is to modify the custom-data fields based on the chosen target contact(s), then it might be cleaner with JavaScript/jQuery than PHP -- with JS, you can always read the currently-selected target contacts (regardless of whether it's a new/existing activity or original/modified contact).

Luciano S.

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 2
  • iXiam Team Leader
  • CiviCRM version: 4.2+ / 4.3+ / 4.4+
  • CMS version: Drupal
  • MySQL version: 5.1+ / 5.5+
  • PHP version: 5.3+ / 5.4+
Re: I cannot get contact_id in hook_civicrm_buildForm
January 04, 2013, 01:11:23 am
Hi totten, thanks for the tips

Actually it is working with JS/JQuery/AJAX. I get the targetContactId, call by AJAX the API and retrieve the info I need and set the default value to the custom field.
The source code it's a bit long and I was doing a refactory and I though will be better (less source code lines and in a way more "natural") to set the default value in the buildForm hook.

So I don't know how to call this limitation (a bug or a nice to have), but basically trying to set the default value of a custom field for an activity type, with the targetContactId (or a value related to that contact_id) when you create the Activity it's not possible using the buildForm hook. At least I couldn't find a way so far...
Luckily there are other ways to do it, with JS

cheers

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • I cannot get contact_id in hook_civicrm_buildForm

This forum was archived on 2017-11-26.