1

I'm trying to change the CiviCRM API getquick search to support legal name, but without a core override. (To remove the need for hacks).

I have the following code:

            function searchmodule_civicrm_apiWrappers(&$wrappers, $apiRequest) {
              if ($apiRequest['entity'] == 'Contact' && $apiRequest['action'] == 'getquick') {
                $wrappers[] = new CRM_Utils_API_LegalSearchAPIWrapper();
              }
            }

And the following class:

            <?php

            require_once 'api/Wrapper.php';

            /**
             * Class CRM_Utils_API_LegalSearchAPIWrapper
             */
            class CRM_Utils_API_LegalSearchAPIWrapper implements API_Wrapper {

                /**
                 * @var CRM_Utils_API_ReloadOption
                 */
                private static $_singleton = NULL;

                /**
                 * @return CRM_Utils_API_ReloadOption
                 */
                public static function singleton() {
                    if (self::$_singleton === NULL) {
                        self::$_singleton = new CRM_Utils_API_LegalSearchAPIWrapper();
                    }
                    return self::$_singleton;
                }

                /**
                 * @inheritDoc
                 */
                public function fromApiInput($apiRequest) {
                    return $apiRequest;
                }

                /**
                 * @inheritDoc
                 */
                public function toApiOutput($apiRequest, $result) {

                    $name = json_decode($apiRequest);
                    $name = $name['params']['name'];


                    $result = civicrm_api3('Contact', 'get', array(
                        'debug' => 1,
                        'sequential' => 1,
                        'return' => "sort_name",
                        'legal_name' => array('LIKE' => "%$name%"),
                    ));


                    return $result;

                }


            }

The class executes, so no issues there, but I'm unsure how to append the query with a legal name attribute. When I write a seperate query for the API to return sort_name, it generates a DB error in the API.

0

Have you tried your API call in the API Explorer first and does that not show any errors? That would be my first step, it would show if the problem is in the return...and I would also add a try/catch around your API call

1

Solved. It's easier to append the results, that modify the original query, which is long, verbose and complex.

                public function toApiOutput($apiRequest, $result) {

                 //  $result format
                 //
                 //   [id] => 12345
                 //   [sort_name] => Last, First
                 //   [email] => first.last@gmail.com
                 //   [data] => Last, First :: first.last@gmail.com

                    $search_term = @$apiRequest['params']['name'];

                    if (strlen($search_term) > 0) {

                        $additional_result = civicrm_api3('Contact', 'get', array(
                            'debug' => 1,
                            'sequential' => 1,
                            'return' => "sort_name, email",
                            'legal_name' => array('LIKE' => "%$search_term%"),
                        ));

                        // Increment count
                        $result['count'] += $additional_result['count'];

                        foreach ($additional_result['values'] as $key => $api_result) {

                            $result['values'][] = array(
                                'id' => $api_result['contact_id'],
                                'sort_name' => $api_result['sort_name'],
                                'email' => $api_result['email'],
                                'data' =>  "[L] " . $api_result['sort_name'] . " :: " . $api_result['email']
                            );
                        }
                    }

                    return $result;

                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.