mecachisenros / cf-civicrm

Caldera Forms CiviCRM Integration
GNU Affero General Public License v3.0
30 stars 26 forks source link

Force results on Contact Reference Field #73

Open KilakwaBT opened 6 years ago

KilakwaBT commented 6 years ago

Working with the new CiviCRM Contact Reference Field. This is a nice feature. Just wondering if it is possible to force the results. We have a group of locations that we want folks to select from. We would like to show them in a drop down list as opposed to having the user search. Let me know, I may be missing something.

Recent version has great additional features. Looking forward to diving in deeper. Thanks

kcristiano commented 6 years ago

@KilakwaBT I like this idea (and if it currently works I missed it as well). In https://github.com/TechToThePeople/publicautocomplete you can limit by Group as detailed https://github.com/TechToThePeople/publicautocomplete#configuration (My example speaks to Organizations)

@mecachisenros Would this be a possible feature addition or did Bruce and I both miss a setting?

KilakwaBT commented 6 years ago

I was missing this due to some CSS issues on the site I was testing. I loaded a form on another site and these do populate. You need to click the drop down arrow and the list comes up fine. Like I said the drop down created is having some CSS issues which was causing me to miss this.

Very nice feature!

KilakwaBT commented 6 years ago

I see the field populates the contact name as the label and the contact ID as the value. I am looking to have the label & value be the contact name. I want to populate the contact name int a custom field in CiviCRM. I am poking around a bit. @mecachisenros or @kcristiano just wondering if this is a quick customization.

Thanks

mecachisenros commented 6 years ago

@KilakwaBT the Caldera Forms (CF) Contact Reference field works for Civi Contact Reference custom fields as well.

I'm no sure I follow when you say have the label & value be the contact name are you trying to populate a text custom field?

KilakwaBT commented 6 years ago

@mecachisenros yes I am trying to populate the field with text as opposed to the ID. I have made some progress by doing the following:

In the field.php file I changed Line 38 from: id: result[0]['id'], to id: result[0]['sort_name'], and Line 67 from: options.push( { id: contact['id'], text: contact['sort_name'] } ); to options.push( { id: contact['sort_name'], text: contact['sort_name'] } );

In the file class-civicrm-contact-reference.php I changed Line 179 From 'id' => $field_value, to sort_name' => $field_value,

This is getting the results I was looking for however the field no longer pre-populates and in the Caldera forms entries when you view the entry the test is not there. It is correct in CiviCRM as well as in the Caldera entries table.

It's kind of a unique use case as we are note creating relationships but asking for location preferences. The available locations change based on which sites are active.

I think I am close, but may be causing other issues.

Thanks!!

mecachisenros commented 6 years ago

The Contact Reference field in Civi holds/relays on the contact_id so I wouldn't recommend going that path.

So if I understand correctly you are not using the Current Employer/Household/Custom Contact Reference at all, but you need the same functionality, but based on sort_name (as that's what your custom field has as values) instead of contact_id?

KilakwaBT commented 6 years ago

Yes, we are trying to insert the sort_name into the field as text instead of contact_id. The field is a straight text field as opposed to a value set since the options will change based on the location status. Does that make sense?

mecachisenros commented 6 years ago

It kind of make sense, I'm still a bit confused about the options and options would change based on status :) (a couple of screenshots would help)

So far my understanding is that you want a drop-down/select2/autocomple field that populates contacts based on some criteria like for example a group.

If that's the case then an easier solution would be to add those as an Autopopulate or Bulk insert/ Preset option.

Or add a couple of hooks to make this possible in the current implementation.

mecachisenros commented 6 years ago

The field mapping and pre population are already hookable here and here respectively, in fact the current implementation uses both filters to handle the creation of Oraganizations when the setting is enabled.

KilakwaBT commented 6 years ago

The scenario is that each week they import locations and if the location's status of active or inactive. I have a sub-type of organization and the active locations go into a group. I was hoping to have the drop-down in the caldera change as the group changes and when selected put the location name into a custom field. Trying to avoid having the user have to update the Caldera field each time they do an import.

I'll take a closer look at the hooks.

thanks

mecachisenros commented 6 years ago

I see, I'll have a look at it, I believe a couple of hooks are needed to make the select2 data object filterable/hookable.

Meanwhile I think the easier solution for now would be to expose groups as Autopoulate options, you can create a plugin like so:

<?php
/**
 * Plugin Name: Caldera Forms CiviCRM - add Groups as Autopopulate option
 * Description: Adds CiviCRM groups as Autopopulate options.
 * Version: 0.1
 * Author: Andrei Mondoc
 */

// add autopopulate options
add_action( 'caldera_forms_autopopulate_types', 'my_cfc_autopulate_groups_field_types' );
// filter field options with contact data based on group
add_filter( 'caldera_forms_render_get_field', 'my_cfc_autopopulate_groups_fields_values', 20, 2 );

function my_cfc_get_civi_groups() {

    if ( ! civi_wp()->initialize() ) return;

    $groups = civicrm_api3( 'Group', 'get', [
        'sequential' => 1,
        'is_active' => 1,
        'options' => [ 'limit' => 0 ],
    ] );

    return $groups['values'];
}

function my_cfc_autopulate_groups_field_types() {

    foreach ( my_cfc_get_civi_groups() as $key => $group ) {
        echo "<option value=\"group_{$group['id']}\"{{#is auto_type value=\"group_{$group['id']}\"}} selected=\"selected\"{{/is}}>" . sprintf( __( 'CiviCRM Group - %1$s', 'caldera-forms-civicrm' ), $group['title'] ) . "</option>";
    }

}

function my_cfc_autopopulate_groups_fields_values( $field, $form ) {

    if ( ! empty( $field['config']['auto'] ) ) {
        $groups = my_cfc_get_civi_groups();

        foreach ( $groups as $key => $group ) {
            if ( $field['config']['auto_type'] == 'group_'.$group['id'] ) {

                $contacts = civicrm_api3( 'Contact', 'get', [
                    'sequential' => 1,
                    'return' => 'sort_name',
                    'group' => $group['id'],
                    'options' => [ 'limit' => 0 ],
                ] );
                foreach ( $contacts['values'] as $key => $contact ) {
                    $field['config']['option'][$contact['id']] = [
                        'value' => $contact['sort_name'],
                        'label' => $contact['sort_name']
                    ];
                }
            }
        }
    }

    return $field;

}

That will expose the groups as Autopopulate options for any Select field in Caldera, then you would map that field to your Custom field in the Contact processor.

lordofthegithub commented 5 years ago

@mecachisenros @KilakwaBT @kcristiano

Hi guys.. I am facing an issue and probably its because I am new to all CiviCRM & CFC which is a great project by @mecachisenros.

I am using a Contact Reference Field in a Form; and when the Contact is successfully chosen, then I prefer to map it to Contact 2 and use the Contact 2 in the Process for Send Email. as shown in the images below. The Contact 1 Processor is for my SENDER. The Contact 2 Processor is for the RECEIVER.

However, my email doesn't go to the Contact chosen via Contact Reference field. Apparently, I am unable to map the Contact Reference Field properly to the Contact 2 Processor; and because of which the SEND Email Processor is not getting the right Inputs in order to send email to the Selected Representative in the Contact Reference field.

I am wondering if you have any thoughts. I've also sent a separate email with some additional suggestions.

@mecachisensros - you're welcome to sign in and look into it. I've sent you an email to this effect as well.