*/
public $urlPath = array();
- /**
- * Stores info about reference fields for preprocessing
- * Public so that hooks can access it
- *
- * @var array
- */
- public $entityReferenceFields = array();
-
/**
* constants for attributes for various form elements
* attempt to standardize on the number of variations that we
// the user can do both the form and set default values with this hook
CRM_Utils_Hook::buildForm(get_class($this), $this);
- $this->preprocessReferenceFields();
-
$this->addRules();
}
list(, $id) = explode('_', $name);
$label = isset($props['label']) ? $props['label'] : CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'label', $id);
$gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', 'option_group_id', $id);
- $props['data-option-group-url'] = array_key_exists('option_url', $props) ? $props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid);
+ $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : 'civicrm/admin/options/' . CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $gid);
}
// Core field
else {
}
}
$label = isset($props['label']) ? $props['label'] : $fieldSpec['title'];
- $props['data-option-group-url'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-group-url'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec);
+ $props['data-option-edit-path'] = array_key_exists('option_url', $props) ? $props['option_url'] : $props['data-option-edit-path'] = CRM_Core_PseudoConstant::getOptionEditUrl($fieldSpec);
}
$props['class'] = (isset($props['class']) ? $props['class'] . ' ' : '') . "crm-select2";
$props['data-api-entity'] = $props['entity'];
* @param array $props mix of html and widget properties, including:
* - select - params to give to select2 widget
* - entity - defaults to contact
+ * - create - can the user create a new entity on-the-fly?
+ * Set to TRUE if entity is contact and you want the default profiles,
+ * or pass in your own set of links. @see CRM_Core_BAO_UFGroup::getCreateLinks for format
* - api - array of settings for the getlist api
* - placeholder - string
* - multiple - bool
* - class, etc. - other html properties
* @param bool $required
+ *
+ * @access public
* @return HTML_QuickForm_Element
*/
- function addEntityRef($name, $label, $props = array(), $required = FALSE) {
+ function addEntityRef($name, $label = '', $props = array(), $required = FALSE) {
+ $config = CRM_Core_Config::singleton();
// Default properties
$props['api'] = CRM_Utils_Array::value('api', $props, array());
$props['entity'] = CRM_Utils_Array::value('entity', $props, 'contact');
$props['class'] = isset($props['class']) ? $props['class'] . ' ' : '';
$props['class'] .= "crm-select2 crm-form-entityref";
- $props['select'] = CRM_Utils_Array::value('select', $props, array()) + array(
+ if ($props['entity'] == 'contact' && isset($props['create']) && !(CRM_Core_Permission::check('edit all contacts') || CRM_Core_Permission::check('add contacts'))) {
+ unset($props['create']);
+ }
+ // Convenient shortcut to passing in array create links
+ if ($props['entity'] == 'contact' && isset($props['create']) && $props['create'] === TRUE) {
+ if (empty($props['api']['params']['contact_type'])) {
+ $props['create'] = CRM_Core_BAO_UFGroup::getCreateLinks(array('new_individual', 'new_organization', 'new_household'));
+ }
+ else {
+ $props['create'] = CRM_Core_BAO_UFGroup::getCreateLinks('new_' . strtolower($props['api']['params']['contact_type']));
+ }
+ }
+
+ $defaults = array(
'minimumInputLength' => 1,
'multiple' => !empty($props['multiple']),
- 'placeholder' => CRM_Utils_Array::value('placeholder', $props, $required ? ts('- select -') : ts('- none -')),
+ 'placeholder' => CRM_Utils_Array::value('placeholder', $props, $required ? ts('- select %1 -', array(1 => ts($props['entity']))) : ts('- none -')),
'allowClear' => !$required,
- // Disabled pending https://github.com/ivaynberg/select2/pull/2092
- //'formatInputTooShort' => ts('Start typing a name or email address...'),
- //'formatNoMatches' => ts('No contacts found.'),
);
+ if ($props['entity'] == 'contact') {
+ $defaults['formatInputTooShort'] = $config->includeEmailInName ? ts('Start typing a name or email...') : ts('Start typing a name...');
+ }
+ $props['select'] = CRM_Utils_Array::value('select', $props, array()) + $defaults;
- $this->entityReferenceFields[] = $name;
$this->formatReferenceFieldAttributes($props);
return $this->add('text', $name, $label, $props, $required);
}
$props['data-select-params'] = json_encode($props['select']);
$props['data-api-params'] = $props['api'] ? json_encode($props['api']) : NULL;
$props['data-api-entity'] = $props['entity'];
- CRM_Utils_Array::remove($props, 'multiple', 'select', 'api', 'entity', 'placeholder');
- }
-
- /**
- * Convert IDs to values and format for display
- */
- private function preprocessReferenceFields() {
- foreach ($this->entityReferenceFields as $name) {
- $field = $this->getElement($name);
- $val = $field->getValue();
- // Support array values
- if (is_array($val)) {
- $val = implode(',', $val);
- $field->setValue($val);
- }
- if ($val) {
- $data = array();
- $entity = $field->getAttribute('data-api-entity');
- $select = json_decode($field->getAttribute('data-select-params'), TRUE);
- // Support serialized values
- if (strpos($val, CRM_Core_DAO::VALUE_SEPARATOR) !== FALSE) {
- $val = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($val, CRM_Core_DAO::VALUE_SEPARATOR));
- $field->setValue($val);
- }
- $result = civicrm_api3($entity, 'getlist', array('params' => array('id' => $val)));
- if ($field->isFrozen()) {
- $field->removeAttribute('class');
- }
- if (!empty($result['values'])) {
- // Simplify array for single selects - makes client-side code simpler (but feels somehow wrong)
- if (empty($select['multiple'])) {
- $result['values'] = $result['values'][0];
- }
- $field->setAttribute('data-entity-value', json_encode($result['values']));
- }
- }
+ if (!empty($props['create'])) {
+ $props['data-create-links'] = json_encode($props['create']);
}
+ CRM_Utils_Array::remove($props, 'multiple', 'select', 'api', 'entity', 'placeholder', 'create');
}
/**