You are here

private function WebformCivicrmPreProcess::addResources in Webform CiviCRM Integration 8.5

Add necessary js & css to the form

1 call to WebformCivicrmPreProcess::addResources()
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 205
Front-end form pre-processor.

Class

WebformCivicrmPreProcess

Namespace

Drupal\webform_civicrm

Code

private function addResources() {
  $this->form['#attached']['library'][] = 'webform_civicrm/forms';
  $utils = \Drupal::service('webform_civicrm.utils');
  $default_country = $utils
    ->wf_crm_get_civi_setting('defaultContactCountry', 1228);
  $billingAddress = wf_crm_aval($this->data, "billing:number_number_of_billing", FALSE);

  // Variables to push to the client-side
  $js_vars = [];

  // JS Cache eliminates the need for most ajax state/province callbacks
  foreach ($this->data['contact'] as $c) {
    if (!empty($c['number_of_address']) || !empty($billingAddress)) {
      $js_vars += [
        'defaultCountry' => $default_country,
        'defaultStates' => $utils
          ->wf_crm_get_states($default_country),
        'noCountry' => t('- First Choose a Country -'),
        'callbackPath' => Url::fromUserInput('/webform-civicrm/js', [
          'alias' => TRUE,
        ])
          ->toString(),
      ];
      break;
    }
  }

  // Preprocess contribution page
  if (!empty($this->data['contribution'])) {
    $this
      ->addPaymentJs();
    $this->form['#attached']['library'][] = 'webform_civicrm/payment';
    $currency = wf_crm_aval($this->data, "contribution:1:currency");
    $contributionCallbackQuery = [
      'currency' => $currency,
      'snippet' => 4,
      'is_drupal_webform' => 1,
    ];
    $contributionCallbackUrl = 'base://civicrm/payment/form';
    $js_vars['processor_id_key'] = 'processor_id';
    if (!empty($this->data['contribution'][1]['contribution'][1]['is_test'])) {

      // RM: This is needed in order for CiviCRM to know that this is a 'test' (i.e. 'preview' action in CiviCRM) transaction - otherwise, CiviCRM defaults to 'live' and returns the payment form with public key for the live payment processor!
      $contributionCallbackQuery['action'] = \CRM_Core_Action::description(\CRM_Core_Action::PREVIEW);
    }
    $js_vars['contributionCallback'] = Url::fromUri($contributionCallbackUrl, [
      'query' => $contributionCallbackQuery,
      'alias' => TRUE,
    ])
      ->toString();

    // Add payment processor - note we have to search in 2 places because $this->loadMultipageData hasn't been run. Maybe it should be?
    $fid = 'civicrm_1_contribution_1_contribution_payment_processor_id';
    if (!empty($this->enabled[$fid])) {
      $js_vars['paymentProcessor'] = $this
        ->getData($fid);

      // @todo Why does it matter if its enabled or not? Nothing is submitted.
      // $js_vars['paymentProcessor'] = wf_crm_aval($this->form_state, 'storage:submitted:' . $this->enabled[$fid]);
    }
    else {
      $js_vars['paymentProcessor'] = $this
        ->getData($fid);
    }
  }
  $this->form['#attached']['drupalSettings']['webform_civicrm'] = $js_vars;
}