Answers.Body: <p>Solved. It's easier to append the results, that modify the original query, which is long, verbose and complex.</p>
  
  <pre><code>                public function toApiOutput($apiRequest, $result) {
  
                   //  $result format
                   //
                   //   [id] =&gt; 12345
                   //   [sort_name] =&gt; Last, First
                   //   [email] =&gt; first.last@gmail.com
                   //   [data] =&gt; Last, First :: first.last@gmail.com
  
                      $search_term = @$apiRequest['params']['name'];
  
                      if (strlen($search_term) &gt; 0) {
  
                          $additional_result = civicrm_api3('Contact', 'get', array(
                              'debug' =&gt; 1,
                              'sequential' =&gt; 1,
                              'return' =&gt; "sort_name, email",
                              'legal_name' =&gt; array('LIKE' =&gt; "%$search_term%"),
                          ));
  
                          // Increment count
                          $result['count'] += $additional_result['count'];
  
                          foreach ($additional_result['values'] as $key =&gt; $api_result) {
  
                              $result['values'][] = array(
                                  'id' =&gt; $api_result['contact_id'],
                                  'sort_name' =&gt; $api_result['sort_name'],
                                  'email' =&gt; $api_result['email'],
                                  'data' =&gt;  "[L] " . $api_result['sort_name'] . " :: " . $api_result['email']
                              );
                          }
                      }
  
                      return $result;
  
                  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-28T01:23:37.860Z
Answers.Text: Can be downloaded from: https://github.com/adam-devapp/civicrm_legal_name_search
Answers.UserDisplayName: Adam
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-28T01:11:51.053Z
Answers.LastActivityDate: 2016-09-28T01:11:51.053Z
Answers.UserDisplayName: Adam
Answers.Body: <p>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</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-27T08:24:13.520Z
Answers.LastActivityDate: 2016-09-27T08:24:13.520Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>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).</p>
  
  <p>I have the following code:</p>
  
  <pre><code>            function searchmodule_civicrm_apiWrappers(&amp;$wrappers, $apiRequest) {
                if ($apiRequest['entity'] == 'Contact' &amp;&amp; $apiRequest['action'] == 'getquick') {
                  $wrappers[] = new CRM_Utils_API_LegalSearchAPIWrapper();
                }
              }
  </code></pre>
  
  <p>And the following class:</p>
  
  <pre><code>            &lt;?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' =&gt; 1,
                          'sequential' =&gt; 1,
                          'return' =&gt; "sort_name",
                          'legal_name' =&gt; array('LIKE' =&gt; "%$name%"),
                      ));
  
  
                      return $result;
  
                  }
  
  
              }
  </code></pre>
  
  <p>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.</p>
  
Tags: <civicrm-entity><civicrm-home>
Title: modify API request using searchmodule_civicrm_apiWrappers
ThreadText: # modify API request using searchmodule_civicrm_apiWrappers
  
  (Asked by: username_0 on 2016-09-27)
  
  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:
               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.
  
  **Tags:** civicrm-entity, civicrm-home
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2016-09-28. Score: 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;
  
                  }
  
  > **Comments:**
  > **username_0**: Can be downloaded from: https://github.com/adam-devapp/civicrm_legal_name_search  
  
  ---
  
  ### Answer by username_1 on 2016-09-27. Score: 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
  
  
  ---
  
  