Answers.Body: <p>The entityRef widget uses the 'getlist' API action, so you can test it out by going to the API Explorer (in the Help or Support menu under Developer, or if you go to the CiviCRM path "civicrm/api/explorer").  </p>
  
  <ul>
  <li>Select <code>Contact</code> as the Entity and <code>getlist</code> as the Action.  </li>
  <li>Pick the Parameter <code>API Params</code>, and set the value to <code>{'contact_type': 'Individual', 'id': {'IN': [132, 168, 169]}}</code>.  </li>
  </ul>
  
  <p>See what you get--in theory, that should just return those three contacts.  If so, your problem isn't the parameters; it must be the way your form is customized.</p>
  
  <p>The code presented here looks good.  However, are you sure you removed the field you are replacing?  To replace a QuickForm element, you should remove the old one and generate a new one in its place.  To remove the old form, add this to the top of your code:</p>
  
  <pre><code>$form-&gt;removeElement('related_contact_id');
  </code></pre>
  
  <p>and then add the field.</p>
  
  <p><strong>An alternative method:</strong> since you're just replacing an existing entityRef field, it might be more efficient to just modify the API parameters.  The following Javascript might do the trick:</p>
  
  <pre><code>// Get rid of the old entityRef:
  $('#related_contact_id').crmEntityRef('destroy');
  
  // Generate a new entityRef:
  $('#related_contact_id').crmEntityRef({
    api: {
      params: {
        contact_type: 'Individual',
        id: {IN: [132, 168, 169]}
      }
    }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-24T20:54:19.370Z
Answers.Text: It seems it's fine from the API side, however, $form->removeElement added via hook_buildForm throws a HTML_QuickForm complain:  "Element 'related_contact_id' does not exist in HTML_QuickForm::removeElement()", but only after a Relationships Type  from an above form field is selected. Guess  a jQuery code from templates\CRM\Contact\Form\Relationship.tpl redraws #related_contact_id depending on #relationship_type_id state  ( namely this piece:   $('[name=relationship_type_id]', $form).change(function() { ), but unfortunately  I'm not as good in jQuery to confirm this.
Answers.UserDisplayName: yurg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-24T21:33:54.953Z
Answers.Text: Going further, I've tried to modify an original instance of 'related_contact_id' in place where it seems to be initially added, namely  \civicrm\CRM\Contact\Form\Relationship.php, but it seems to ignore params taken from api explorer and still gets all results instead of restricted by ID set. Here's a code:   
       ` $contactField = $this->addEntityRef(
        'related_contact_id', 
          $label, array(
           'api'=> array(
              'params' => array('contact_type' => "Individual", 'id' => array('IN' => array(56462, 254273))),
          )));  `  
  Is it possible it's an error in entityRef?
Answers.UserDisplayName: yurg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T03:31:45.330Z
Answers.Text: re; the element does not exist error -- try conditioning on: if (isset($form->_elementIndex['related_contact_id'])) { ...remove element... }
Answers.UserDisplayName: lcdservices
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T03:37:46.620Z
Answers.Text: Actually, it's probably better to use: if ($form->elementExists('related_contact_id')) {}
Answers.UserDisplayName: lcdservices
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T04:08:59.223Z
Answers.Text: I had a similar need and tried the above suggestions. My preferred solution was to remove the element and then recreate it. In my case I just wanted to rebuild a custom field and set minimumInputLength = 0 so it would retrieve the list without requiring searching. I wasn't able to do it either through PHP (buildForm hook) or js. The issue seems to be that the js that constructs the entity ref is always written to the page -- even if you've removed the element -- and so the defaults settings persist and can't be easily overwritten. I think this is probably something to look at further.
Answers.UserDisplayName: lcdservices
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T14:09:35.987Z
Answers.Text: Precisely, : *jQuery code from templates\CRM\Contact\Form\Relationship.tpl redraws #related_contact_id depending on #relationship_type_id state ( namely this piece: $('[name=relationship_type_id]', $form).change(function() { )* as i have been afraid of, so probably it will be worth trying to break a jquery link between two drop-downs and make them operate separately.
Answers.UserDisplayName: yurg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-24T19:36:35.297Z
Answers.LastActivityDate: 2016-01-24T19:36:35.297Z
Answers.UserDisplayName: Andie Hunt
Body: <p>Whatever has been tried, this code doesn't restrict entityRef widget output according to desired parameters:</p>
  
  <pre><code>$selection = array(132, 168, 169);
  $sa_custom_props = array(
    'api' =&gt; array('params' =&gt; array(
     'contact_type' =&gt; 'Individual',
      'id' =&gt; array('IN' =&gt; $selection)
    )));
  $form-&gt;addEntityRef(
     'related_contact_id', 
      ts('Contact'), 
       $sa_custom_props    
   );
  }  
  </code></pre>
  
  <p>It happens at an attempt to override current contacts select widget at "Add Relationship" (CRM\Contact\Form\Relationship.tpl&amp;.php pair) form, if it matters.  Any piece of advice is highly  appreciated, thanks!   </p>
  
Tags: <api><developer><entityref>
Title: entityRef widget seems to not respecting params
ThreadText: # entityRef widget seems to not respecting params
  
  (Asked by: username_0 on 2016-01-24)
  
  Whatever has been tried, this code doesn't restrict entityRef widget output according to desired parameters:
  $selection = array(132, 168, 169);
  $sa_custom_props = array(
    'api' => array('params' => array(
     'contact_type' => 'Individual',
      'id' => array('IN' => $selection)
    )));
  $form->addEntityRef(
     'related_contact_id', 
      ts('Contact'), 
       $sa_custom_props    
   );
  }  
  
  It happens at an attempt to override current contacts select widget at "Add Relationship" (CRM\Contact\Form\Relationship.tpl&.php pair) form, if it matters.  Any piece of advice is highly  appreciated, thanks!
  
  **Tags:** api, developer, entityref
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-01-24. Score: 1 | Accepted answer
  
  The entityRef widget uses the 'getlist' API action, so you can test it out by going to the API Explorer (in the Help or Support menu under Developer, or if you go to the CiviCRM path "civicrm/api/explorer").  
  
  Select Contact as the Entity and getlist as the Action.  
  Pick the Parameter API Params, and set the value to {'contact_type': 'Individual', 'id': {'IN': [132, 168, 169]}}.  
  
  See what you get--in theory, that should just return those three contacts.  If so, your problem isn't the parameters; it must be the way your form is customized.
  The code presented here looks good.  However, are you sure you removed the field you are replacing?  To replace a QuickForm element, you should remove the old one and generate a new one in its place.  To remove the old form, add this to the top of your code:
  $form->removeElement('related_contact_id');
  
  and then add the field.
  An alternative method: since you're just replacing an existing entityRef field, it might be more efficient to just modify the API parameters.  The following Javascript might do the trick:
  // Get rid of the old entityRef:
  $('#related_contact_id').crmEntityRef('destroy');
  
  // Generate a new entityRef:
  $('#related_contact_id').crmEntityRef({
    api: {
      params: {
        contact_type: 'Individual',
        id: {IN: [132, 168, 169]}
      }
    }
  });
  
  > **Comments:**
  > **username_0**: It seems it's fine from the API side, however, $form->removeElement added via hook_buildForm throws a HTML_QuickForm complain:  "Element 'related_contact_id' does not exist in HTML_QuickForm::removeElement()", but only after a Relationships Type  from an above form field is selected. Guess  a jQuery code from templates\CRM\Contact\Form\Relationship.tpl redraws #related_contact_id depending on #relationship_type_id state  ( namely this piece:   $('[name=relationship_type_id]', $form).change(function() { ), but unfortunately  I'm not as good in jQuery to confirm this.  
  > **username_0**: Going further, I've tried to modify an original instance of 'related_contact_id' in place where it seems to be initially added, namely  \civicrm\CRM\Contact\Form\Relationship.php, but it seems to ignore params taken from api explorer and still gets all results instead of restricted by ID set. Here's a code:         ` $contactField = $this->addEntityRef(       'related_contact_id',          $label, array(          'api'=> array(             'params' => array('contact_type' => "Individual", 'id' => array('IN' => array(56462, 254273))),         )));  `   Is it possible it's an error in entityRef?  
  > **username_2**: re; the element does not exist error -- try conditioning on: if (isset($form->_elementIndex['related_contact_id'])) { ...remove element... }  
  > **username_2**: Actually, it's probably better to use: if ($form->elementExists('related_contact_id')) {}  
  > **username_2**: I had a similar need and tried the above suggestions. My preferred solution was to remove the element and then recreate it. In my case I just wanted to rebuild a custom field and set minimumInputLength = 0 so it would retrieve the list without requiring searching. I wasn't able to do it either through PHP (buildForm hook) or js. The issue seems to be that the js that constructs the entity ref is always written to the page -- even if you've removed the element -- and so the defaults settings persist and can't be easily overwritten. I think this is probably something to look at further.  
  > **username_0**: Precisely, : *jQuery code from templates\CRM\Contact\Form\Relationship.tpl redraws #related_contact_id depending on #relationship_type_id state ( namely this piece: $('[name=relationship_type_id]', $form).change(function() { )* as i have been afraid of, so probably it will be worth trying to break a jquery link between two drop-downs and make them operate separately.  
  
  ---
  
  