Epesi ENS | Epesi Academy | epesi.cloud PaaS | GitHub

Help Me! | Download from SourceForge Download Epesi BIM Free & Open Source CRM


I'd like to display the contact in the view/edit/listing mode of my module in a different format - first and last name followed by the addition of a custom field I added to the contact module.

To do this I already added the following array element into the $fields array which I later install via my module:


'display_callback'=>array('Insurance_PolisaCommon', 'display_contact_name')),

I then have the following in my PolisaCommon module:


public static function display_contact_name($v, $nolink=false) {
return Utils_RecordBrowserCommon::create_linked_label_r('contact', 'First Name', $v, $nolink);
}

That is incomplete and errornous though, incomplete cause I intend to show full name along with a custom field I also want to display and it also results in an error, something about 'first_name' index not defined.


Regards,
Liran.

Some good progress, resulted in a partial solution:


public static function display_contact_name($record, $nolink=false) {

$ret = "";
if (!$nolink) $ret .= Utils_RecordBrowserCommon::record_link_open_tag('contact', $record['customer']);
$contact = CRM_ContactsCommon::get_contact($record['customer']);
$ret .= $contact['last_name'].(($contact['first_name']!=='')?' '.$contact['first_name']:'');
$ret .= " - ".$contact['customer_id'];
if (!$nolink) $ret .= Utils_RecordBrowserCommon::record_link_close_tag();

return $ret;

}

That results in displaying the contact full name along with the other customer_id field which I am interested in, but it actually displays it everywhere except where I want to - and that is in the select field when the form is edit/new form. Any advise on achieving that? The reason is obvious - there might be several records of a contact with the same name, I want to add a customer_id field there (instead of company for example) so that it'll be clear which contact is this.


Regards,
Liran.

You'll need QFfield callback for that I'm afraid.

Check method

	public static function QFfield_webaddress(&$form, $field, $label, $mode, $default)

In Contacts Common - that's most likely the simplest example you can grab. You just need to specify proper select element you need and sort of use your display callback in the "view" part.

Please see to this method and tell me if yo have any questions.

Kind regards,
Arek

Hey Arek,

Thanks for directing me to the solution... I can't find any documentation on QField on the wiki and the best I can come up with, trying to adapt my needs to the example code is something of this sort:

in PolisaInstall:


array('name'=>'Customer', 'type'=>'crm_contact', 'param'=>array('field_type'=>'select',
'required'=>true, 'extra'=>false, 'visible'=>true), 'visible'=>true, 'required'=>true, 'extra'=>false,
'display_callback'=>array('Insurance_PolisaCommon', 'display_contact_name'),
'QFfield_callback'=>array('Insurance_PolisaCommon', 'QFfield_contact_name'),
),

in PolisaCommon:


public static function display_contact_name($record, $nolink=false) {

$ret = "";
if (!$nolink) $ret .= Utils_RecordBrowserCommon::record_link_open_tag('contact', $record['customer']);
$contact = CRM_ContactsCommon::get_contact($record['customer']);
$ret .= $contact['last_name'].(($contact['first_name']!=='')?' '.$contact['first_name']:'');
$ret .= " - ".$contact['customer_id'];
if (!$nolink) $ret .= Utils_RecordBrowserCommon::record_link_close_tag();

return $ret;

}

public static function QFfield_contact_name(&$form, $field, $label, $mode, $default) {
if ($mode=='add' || $mode=='edit') {
$form->addElement('select', $field, $label);
if ($mode=='edit') $form->setDefaults(array($field=>$default));
} else {
$form->addElement('static', $field, $label);
$form->setDefaults(array($field=>self::display_contact_name(array('customer'=>$default), null, array('id'=>'customer'))));
}
}

One thing I noticed different from the ContactsCommon QField deceration for the webaddress field is that it uses addElement('text') while I obviously need it to be 'select' though, I'm probably missing some arguments there, I just don't know how it's supposed to work.

I also had to "guess" what should be the structure of the array of setDefaults() and just used 'customer' as I thought it's probably the name of the field but again, I don't know.


I'd appreciate if you can organize all of this for me.

Thanks,
Liran.

You pasted the wrong bit of code, or so it seems - it's QFfield we're talking about.

Your guess with set defaults is right - ket is the name of the field, value [for selects] is the key of the option you want as default.

Select simply accepts one more, 4th argument - an array with options:


array($value=>$displayed_value)

Kind regards,
Arek

What do you mean passed wrong bit of code?
There's a scroll down, I also passed the QFfield method... take a look.

You said select takes an array with options, so do I need to do get_records() for all contacts and assign them the way I want to (with the extra field I want to be displayed) as the value?


Regards,
Liran.

Ah, right. Just saw one method and didn't notice scrollbar.

That's the way to handle it, yes: use get_records() on contacts.

Kind regards,
Arek

Ok so this is what I've come up with, could you just review that this is how it should
actually happen? I checked myself and functionality-wise it's ok but I'm just not familiar
yet with QFfield and I'd appreciate if you could review and make sure it's the "right way"
to do it:


public static function QFfield_contact_name(&$form, $field, $label, $mode, $default) {
if ($mode=='add' || $mode=='edit') {
$record_set = "contact";
$records = Utils_RecordBrowserCommon::get_records($record_set, $crits = array(), $cols = array(), $order = array(),
$limit = array(), $admin = false);

foreach($records as $record=>$value)
$users[$record] = $value['last_name'] . " " . $value['first_name'] . " - " . $value['customer_id'];

$form->addElement('select', $field, $label, $users);
if ($mode=='edit') $form->setDefaults(array($field=>$default));
} else {

$form->addElement('static', $field, $label);
$form->setDefaults(array($field=>self::display_contact_name(array('customer'=>$default), null )));
}
}

Thanks,
Liran.

Yep, that should cut it. 🙂

One little thing I'd add: initialize $users array

$users = array();
With error report set to strict, it would bug out without it.

Kind regards,
Arek

Write a Reply...