diff --git a/CRM/Activity/Form/Search.php b/CRM/Activity/Form/Search.php index 063fa8001f31..1c82959f2828 100644 --- a/CRM/Activity/Form/Search.php +++ b/CRM/Activity/Form/Search.php @@ -353,16 +353,6 @@ public function getFormValues() { return NULL; } - /** - * This virtual function is used to set the default values of various form elements. - * - * @return array|NULL - * reference to the array of default values - */ - public function setDefaultValues() { - return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues); - } - /** * Return a descriptive name for the page, used in wizard header * diff --git a/CRM/Contact/Form/Search/Advanced.php b/CRM/Contact/Form/Search/Advanced.php index 0656d737d63d..b0743b220311 100644 --- a/CRM/Contact/Form/Search/Advanced.php +++ b/CRM/Contact/Form/Search/Advanced.php @@ -190,26 +190,26 @@ public function getTemplateFileName() { /** * Set the default form values. * - * * @return array * the default array reference + * @throws \Exception */ public function setDefaultValues() { + $defaults = parent::setDefaultValues(); // Set ssID for unit tests. if (empty($this->_ssID)) { $this->_ssID = $this->get('ssID'); } - $defaults = array_merge($this->_formValues, array( + $defaults = array_merge($this->_formValues, [ 'privacy_toggle' => 1, 'operator' => 'AND', - )); + ], $defaults); $this->normalizeDefaultValues($defaults); if ($this->_context === 'amtg') { $defaults['task'] = CRM_Contact_Task::GROUP_ADD; } - return $defaults; } diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index 240ade9aeaa6..1a533342a05e 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -33,9 +33,13 @@ class CRM_Contact_Form_Search_Criteria { /** - * @param CRM_Core_Form $form + * @param CRM_Contact_Form_Search_Advanced $form + * + * @throws \CRM_Core_Exception */ public static function basic(&$form) { + $form->addSearchFieldMetadata(['Contact' => self::getSearchFieldMetadata()]); + $form->addFormFieldsFromMetadata(); self::setBasicSearchFields($form); $form->addElement('hidden', 'hidden_basic', 1); @@ -100,9 +104,6 @@ public static function basic(&$form) { } } - // add text box for last name, first name, street name, city - $form->addElement('text', 'sort_name', ts('Complete OR Partial Name'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); - // add text box for last name, first name, street name, city $form->add('text', 'email', ts('Complete OR Partial Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); @@ -254,6 +255,20 @@ public static function basic(&$form) { $form->add('select', 'phone_phone_type_id', ts('Phone Type'), ['' => ts('- any -')] + $phoneType, FALSE, ['class' => 'crm-select2']); } + /** + * Get the metadata for fields to be included on the contact search form. + */ + public static function getSearchFieldMetadata() { + $fields = [ + 'sort_name' => ['title' => ts('Complete OR Partial Name'), 'template_grouping' => 'basic'], + ]; + $metadata = civicrm_api3('Contact', 'getfields', [])['values']; + foreach ($fields as $fieldName => $field) { + $fields[$fieldName] = array_merge(CRM_Utils_Array::value($fieldName, $metadata, []), $field); + } + return $fields; + } + /** * Defines the fields that can be displayed for the basic search section. * diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 41688934afbc..865cb9e694d2 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -104,9 +104,11 @@ protected function buildTaskList() { /** * Metadata for fields on the search form. * + * Instantiate with empty array for contact to prevent e-notices. + * * @var array */ - protected $searchFieldMetadata = []; + protected $searchFieldMetadata = ['Contact' => []]; /** * @return array @@ -122,6 +124,17 @@ public function addSearchFieldMetadata($searchFieldMetadata) { $this->searchFieldMetadata = array_merge($this->searchFieldMetadata, $searchFieldMetadata); } + /** + * This virtual function is used to set the default values of various form elements. + * + * @return array|NULL + * reference to the array of default values + * @throws \Exception + */ + public function setDefaultValues() { + return array_merge($this->getEntityDefaults($this->getDefaultEntity()), (array) $this->_formValues); + } + /** * Common buildForm tasks required by all searches. */ @@ -159,7 +172,11 @@ public function addFormFieldsFromMetadata() { $this->addDatePickerRange($fieldName, $fieldSpec['title'], ($fieldSpec['type'] === (CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME))); } else { - $this->addField($fieldName, ['entity' => $entity]); + $props = ['entity' => $entity]; + if (isset($fields[$fieldName]['title'])) { + $props['label'] = $fields[$fieldName]['title']; + } + $this->addField($fieldName, $props); } } }