Skip to content

Commit

Permalink
Merge pull request #21606 from colemanw/afformUrlArgs
Browse files Browse the repository at this point in the history
Afform - Accept default field values from the url
  • Loading branch information
eileenmcnaughton authored Sep 27, 2021
2 parents d182fa5 + 4f7e8d8 commit b1fc671
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions ext/afform/core/ang/af/afField.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
fieldName: '@name',
defn: '='
},
controller: function($scope, $element, crmApi4, $timeout) {
controller: function($scope, $element, crmApi4, $timeout, $location) {
var ts = $scope.ts = CRM.ts('org.civicrm.afform'),
ctrl = this,
boolOptions = [{id: true, label: ts('Yes')}, {id: false, label: ts('No')}],
Expand Down Expand Up @@ -86,13 +86,26 @@
});
}

// Set default value
if (ctrl.defn.afform_default) {
// Wait for parent controllers to initialize
$timeout(function() {
// Wait for parent controllers to initialize
$timeout(function() {
// Unique field name = entity_name index . join . field_name
var entityName = ctrl.afFieldset.getName(),
joinEntity = ctrl.afJoin ? ctrl.afJoin.entity : null,
uniquePrefix = '',
urlArgs = $location.search();
if (entityName) {
var index = ctrl.getEntityIndex();
uniquePrefix = entityName + (index ? index + 1 : '') + (joinEntity ? '.' + joinEntity : '') + '.';
}
// Set default value based on url
if (urlArgs && urlArgs[uniquePrefix + ctrl.fieldName]) {
$scope.dataProvider.getFieldData()[ctrl.fieldName] = urlArgs[uniquePrefix + ctrl.fieldName];
}
// Set default value based on field defn
else if (ctrl.defn.afform_default) {
$scope.dataProvider.getFieldData()[ctrl.fieldName] = ctrl.defn.afform_default;
});
}
}
});

};

Expand Down

0 comments on commit b1fc671

Please sign in to comment.