Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor submitted cid value in existing_contact #991

Open
wants to merge 3 commits into
base: 6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/WebformCivicrmBase.php
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • $editingSubmission changed to $submission_id to avoid obscuring its actual meaning.
  • Exception -> \Exception: An obvious bug since there is no Exception function in the Drupal\webform_civicrm namespace. Interestingly, this bug caused xdebug in VS Code to crash whenever hovering over a member function (was driving me crazy for a long time).
  • Corrects handling of "-" in existing_contact value (indicating "Create New" was selected".

Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,19 @@ protected function loadContact($c, $exclude = []) {
* @param array $component
* Webform component of type 'civicrm_contact'
*/
protected function findContact($component) {
protected function findContact($component, $cid_submitted_value = null) {
$contactComponent = \Drupal::service('webform_civicrm.contact_component');
$component['#form_key'] = $component['#form_key'] ?? $component['#webform_key'];

list(, $c,) = explode('_', $component['#form_key'], 3);
$filters = $contactComponent->wf_crm_search_filters($this->node, $component);

// If a valid cid value is being submitted for the existing contact, then no need to continue.
if (!empty($cid_submitted_value) && $contactComponent->wf_crm_contact_access($component, $filters, $cid_submitted_value) != FALSE) {
$this->ent['contact'][$c]['id'] = $cid_submitted_value;
return;
}

// Start with the url - that trumps everything.
$element_manager = \Drupal::getContainer()->get('plugin.manager.webform.element');
$existing_component_plugin = $element_manager->getElementInstance($component);
Expand Down
11 changes: 8 additions & 3 deletions src/WebformCivicrmPreProcess.php
Copy link
Contributor Author

@bsilvern bsilvern Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of changes here to ensure correct handling when clicking Prev/Next, especially when there are locked/hidden fields due to the existing_contact element and when working with drafts. It looks like a bigger change than it is because I changed the indent level on a big block of code and github diff is going nuts. Recommend viewing with the "hide whitespace" option.

Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function alterForm() {
}
$submitted_contacts = [];
// Keep track of cids across multipage forms
// @TODO this block appears to be left over from D7 and is never executed(?)
if (!empty($this->form_state->getValue('submitted')) && $this->form_state->get(['webform','page_count']) > 1) {
foreach ($this->enabled as $k => $v) {
// @TODO review the usage of the existing element.
Expand Down Expand Up @@ -125,6 +126,7 @@ public function alterForm() {
return;
}
// If this is an edit op, use the original IDs and return
// @TODO this block appears to be left over from D7 and is never executed(?)
if (isset($this->form['#submission']->sid) && $this->form['#submission']->is_draft != '1') {
if (isset($this->form['#submission']->civicrm)) {
$this->form_state['civicrm']['ent'] = $this->form['#submission']->civicrm;
Expand All @@ -137,17 +139,20 @@ public function alterForm() {
}

// Search for existing contacts
$user_input = $this->form_state->getUserInput();
$counts_count = count($this->data['contact']);
for ($c = 1; $c <= $counts_count; ++$c) {
$this->ent['contact'][$c] = wf_crm_aval($this->ent, "contact:$c", []);
$existing_component = $this->node->getElement("civicrm_{$c}_contact_1_contact_existing");
// Search for contact if the user hasn't already chosen one
if ($existing_component && empty($submitted_contacts[$c])) {
$this->findContact($existing_component);
if ($existing_component) {
// Ensure that a user-entered cid overrides any default created during the page load
$cid_user_input = $user_input["civicrm_{$c}_contact_1_contact_existing"] ?? null;
bsilvern marked this conversation as resolved.
Show resolved Hide resolved
$this->findContact($existing_component, $cid_user_input);
}
// Fill cid with '0' if unknown
$this->ent['contact'][$c] += ['id' => 0];
}

// Search for other existing entities
if (empty($this->form_state->get('civicrm'))) {
if (!empty($this->data['case']['number_of_case'])) {
Expand Down
Loading