You are here

private function WebformCivicrmPreProcess::findExistingGrants in Webform CiviCRM Integration 8.5

Find grant ids based on url input or "existing grant" settings

1 call to WebformCivicrmPreProcess::findExistingGrants()
WebformCivicrmPreProcess::alterForm in src/WebformCivicrmPreProcess.php
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

src/WebformCivicrmPreProcess.php, line 757
Front-end form pre-processor.

Class

WebformCivicrmPreProcess

Namespace

Drupal\webform_civicrm

Code

private function findExistingGrants() {
  $utils = \Drupal::service('webform_civicrm.utils');
  for ($n = 1; $n <= $this->data['grant']['number_of_grant']; ++$n) {
    if (!empty($this->data['grant'][$n]['grant'][1]['contact_id'])) {
      $cid = $this->ent['contact'][$this->data['grant'][$n]['grant'][1]['contact_id']]['id'];
      if ($cid) {
        if (isset($_GET["grant{$n}"]) && $utils
          ->wf_crm_is_positive($_GET["grant{$n}"])) {
          $id = $_GET["grant{$n}"];
          $item = $utils
            ->wf_civicrm_api('grant', 'getsingle', [
            'id' => $id,
          ]);
          if ($cid == $item['contact_id']) {
            $this->ent['grant'][$n] = [
              'id' => $id,
            ];
          }
        }
        elseif (!empty($this->data['grant'][$n]['existing_grant_status'])) {
          $params = [
            'sequential' => 1,
            'contact_id' => $cid,
            'status_id' => [
              'IN' => (array) $this->data['grant'][$n]['existing_grant_status'],
            ],
            'options' => [
              'limit' => 1,
            ],
          ];
          if (!empty($this->data['grant'][$n]['grant'][1]['grant_type_id'])) {
            $params['grant_type_id'] = $this->data['grant'][$n]['grant'][1]['grant_type_id'];
          }
          $items = $utils
            ->wf_crm_apivalues('grant', 'get', $params);
          if (isset($items[0]['id'])) {
            $this->ent['grant'][$n] = [
              'id' => $items[0]['id'],
            ];
          }
        }
      }
    }
  }
}