Answers.Body: <p>Disclaimer: There may be newer/simpler ways of doing this now that SearchKit has a &quot;autocomplete&quot; display, but I have not experimented with it yet, and not sure if we can use it to override a  EntityReference field.</p>
  <p>In a custom extension, you can implement an auto-complete provider that overrides the default behaviour. Here is an example.</p>
  <ol>
  <li>add the <code>scan-classes@1.0.0</code> mixin to the <code>info.xml</code> file of your extension:</li>
  </ol>
  <pre><code>  &lt;mixins&gt;
      [...]
      &lt;mixin&gt;scan-classes@1.0.0&lt;/mixin&gt;
    &lt;/mixins&gt;
  </code></pre>
  <p>Then create the directory for the service, where we will create the PHP file:</p>
  <pre><code>mkdir -p Civi/Api4/Service/Autocomplete/
  </code></pre>
  <p>Now create a file called <code>Civi/Api4/Service/Autocomplete/MyAddressAutocompleteProvider.php</code> with the following contents:</p>
  <pre><code>&lt;?php
  
  namespace Civi\Api4\Service\Autocomplete;
  
  use Civi;
  use Civi\Core\Event\GenericHookEvent;
  use Civi\Core\HookInterface;
  
  /**
   * @service
   * @internal
   */
  class MyAddressAutocompleteProvider extends \Civi\Core\Service\AutoService implements HookInterface {
  
    /**
     * Provide default SearchDisplay for Address autocompletes
     *
     * @param \Civi\Core\Event\GenericHookEvent $e
     */
    public function on_civi_search_defaultDisplay(GenericHookEvent $event): void {
      if ($event-&gt;display['type'] !== 'autocomplete' || $event-&gt;savedSearch['api_entity'] !== 'Address') {
        return;
      }
      $event-&gt;display['settings'] = [
        'sort' =&gt; [
          ['street_address', 'ASC'],
        ],
        'columns' =&gt; [
          [
            'type' =&gt; 'field',
            'key' =&gt; 'street_address',
            'rewrite' =&gt; '[street_address], [city]',
            'empty_value' =&gt; '[city]',
          ],
          [
            'type' =&gt; 'field',
            'key' =&gt; 'state_province_id.name',
            'rewrite' =&gt; '[postal_code], [state_province_id.name], [country_id.name]',
            'empty_value' =&gt; '[country_id.name]',
          ],
          [
            'type' =&gt; 'field',
            'key' =&gt; 'id',
            'rewrite' =&gt; '#[id]',
          ],
        ],
      ];
    }
  
  }
  </code></pre>
  <p>You can rename  <code>MyAddressAutocompleteProvider</code> to something more relevant to your extension name, but avoid calling it just <code>AddressAutocompleteProvider</code> because that's what it is called in CiviCRM core.</p>
  <p>Notice in my example above, that I added the <code>[postal_code]</code> as part of the second line, before the state/province. When fields are added in the rewrite, they are automatically added to the SQL query for autocompletes.</p>
  <p>For all of this to work, you will need to flush the CiviCRM cache (either from the admin menu, or <code>cv flush</code> on the command line).</p>
  <p><a href="https://i.sstatic.net/zIR0w.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zIR0w.png" alt="modified autocomplete" /></a></p>
  <p>This example was based on the <a href="https://docs.civicrm.org/dev/en/latest/framework/autocomplete/#modifying-the-display" rel="nofollow noreferrer">official documentation</a>, although some bits were not obvious to me at first.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-31T11:21:57.177Z
Answers.Text: Thank you! Great documentation,
Answers.UserDisplayName: masetto
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-30T21:43:01.367Z
Answers.LastActivityDate: 2024-01-30T21:43:01.367Z
Answers.UserDisplayName: bgm
Body: <p>I added a Entity Reference custom field for Address entity.</p>
  <p>When I search an address the autocomplete shows the street_address, postal_code, city, province and country. But I'd like also to show address_name and supplemental_address.</p>
  <p><a href="https://i.sstatic.net/uKVjD.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uKVjD.png" alt="autocomplete for address entity reference custom field" /></a></p>
  <p>And in form view also:</p>
  <p><a href="https://i.sstatic.net/6aUHV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6aUHV.png" alt="address entity reference custom field in form view" /></a></p>
  <p>How can I do this?</p>
  <p>PS: I'm using latest version of CiviCRM on WordPress.</p>
  
Tags: <entity-reference><entityref>
Title: Change output for address Entity Reference
ThreadText: # Change output for address Entity Reference
  
  (Asked by: username_0 on 2024-01-03)
  
  I added a Entity Reference custom field for Address entity.
  
  When I search an address the autocomplete shows the street\_address, postal\_code, city, province and country. But I'd like also to show address\_name and supplemental\_address.
  
  [![autocomplete for address entity reference custom field](https://i.sstatic.net/uKVjD.png)](https://i.sstatic.net/uKVjD.png)
  
  And in form view also:
  
  [![address entity reference custom field in form view](https://i.sstatic.net/6aUHV.png)](https://i.sstatic.net/6aUHV.png)
  
  How can I do this?
  
  PS: I'm using latest version of CiviCRM on WordPress.
  
  **Tags:** entity-reference, entityref
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-01-30. Score: 1 | Accepted answer
  
  Disclaimer: There may be newer/simpler ways of doing this now that SearchKit has a "autocomplete" display, but I have not experimented with it yet, and not sure if we can use it to override a  EntityReference field.
  In a custom extension, you can implement an auto-complete provider that overrides the default behaviour. Here is an example.
  
  add the scan-classes@1.0.0 mixin to the info.xml file of your extension:
  
    
      [...]
      scan-classes@1.0.0
    
  
  Then create the directory for the service, where we will create the PHP file:
  mkdir -p Civi/Api4/Service/Autocomplete/
  
  Now create a file called Civi/Api4/Service/Autocomplete/MyAddressAutocompleteProvider.php with the following contents:
  display['type'] !== 'autocomplete' || $event->savedSearch['api_entity'] !== 'Address') {
        return;
      }
      $event->display['settings'] = [
        'sort' => [
          ['street_address', 'ASC'],
        ],
        'columns' => [
          [
            'type' => 'field',
            'key' => 'street_address',
            'rewrite' => '[street_address], [city]',
            'empty_value' => '[city]',
          ],
          [
            'type' => 'field',
            'key' => 'state_province_id.name',
            'rewrite' => '[postal_code], [state_province_id.name], [country_id.name]',
            'empty_value' => '[country_id.name]',
          ],
          [
            'type' => 'field',
            'key' => 'id',
            'rewrite' => '#[id]',
          ],
        ],
      ];
    }
  
  }
  
  You can rename  MyAddressAutocompleteProvider to something more relevant to your extension name, but avoid calling it just AddressAutocompleteProvider because that's what it is called in CiviCRM core.
  Notice in my example above, that I added the [postal_code] as part of the second line, before the state/province. When fields are added in the rewrite, they are automatically added to the SQL query for autocompletes.
  For all of this to work, you will need to flush the CiviCRM cache (either from the admin menu, or cv flush on the command line).
  
  This example was based on the official documentation, although some bits were not obvious to me at first.
  
  > **Comments:**
  > **username_0**: Thank you! Great documentation,  
  
  ---
  
  