API Explorer - Default contact-ref to select current user
authorColeman Watts <coleman@civicrm.org>
Fri, 31 Jul 2015 20:41:57 +0000 (16:41 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 31 Jul 2015 21:33:20 +0000 (17:33 -0400)
templates/CRM/Admin/Page/APIExplorer.js

index b2b0794bfcfc773b1ab2c987942ac4a7593931bf..b2aeb0877d4e549c9107c344433ce534034268e0 100644 (file)
       operator = $('.api-param-op', $row).val(),
       $valField = $('input.api-param-value', $row),
       multiSelect = isMultiSelect(name, operator),
-      currentVal = $valField.val();
+      currentVal = $valField.val(),
+      wasSelect = $valField.data('select2');
+    if (wasSelect) {
+      $valField.crmEntityRef('destroy');
+    }
+    $valField.attr('placeholder', ts('Value'));
     // Boolean fields only have 1 possible value
     if (_.includes(BOOL, operator)) {
-      if ($valField.data('select2')) {
-        $valField.select2('destroy');
-      }
       $valField.css('visibility', 'hidden').val('1');
       return;
     }
     $valField.css('visibility', '');
     // Option list or entityRef input
     if (isSelect(name, operator)) {
+      $valField.attr('placeholder', ts('- select -'));
       // Reset value before switching to a select from something else
-      if ($(this).is('.api-param-name') || !$valField.data('select2')) {
+      if ($(this).is('.api-param-name') || !wasSelect) {
         $valField.val('');
       }
       // When switching from multi-select to single select
       }
       // EntityRef
       else {
+        var entity = getFieldData[name].FKApiName;
+        $valField.attr('placeholder', entity == 'Contact' ? '[' + ts('Auto-Select Current User') + ']' : ts('- select -'));
         $valField.crmEntityRef({
-          entity: getFieldData[name].FKApiName,
+          entity: entity,
           select: {
             multiple: multiSelect,
-            minimumInputLength: _.includes(OPEN_IMMEDIATELY, getFieldData[name].FKApiName) ? 0 : 1
+            minimumInputLength: _.includes(OPEN_IMMEDIATELY, entity) ? 0 : 1
           }
         });
       }
-      return;
-    }
-    // Plain text input
-    if ($valField.data('select2')) {
-      $valField.select2('destroy');
     }
   }
 
 
   /**
    * Format value to look like php code
+   * TODO: Use short array syntax when we drop support for php 5.3
    * @param val
    */
   function phpFormat(val) {
           name = 'options';
         }
       }
+      // Default for contact ref fields
+      if ($(this).is('.crm-contact-ref') && input === '') {
+        val = evaluate('user_contact_id', makeArray);
+      }
       if (name && val !== undefined) {
         params[name] = op === '=' ? val : (params[name] || {});
         if (op !== '=') {
       .on('change', 'input.api-param-name, select.api-param-op', renderValueField)
       .on('change', 'input.api-param-name, .api-option-name', function() {
         if ($(this).val() === '-' && $(this).data('select2')) {
-          $(this).select2('destroy');
-          $(this).val('').focus();
+          $(this)
+            .crmSelect2('destroy')
+            .val('')
+            .focus();
         }
       })
       .on('click', '.api-param-remove', function(e) {