You are here

private function AdminForm::addAjaxItem in Webform CiviCRM Integration 8.5

Boilerplate-reducing helper function for FAPI ajax. Set an existing form element to control an ajax container. The container will be created if it doesn't already exist.

Parameters

string $path: A : separated string of nested array keys leading to the control element's parent

string $control_element: Array key of the existing element to add ajax behavior to

string $container: Path to the key of the container to be created (relative to $path) use '..' to go up a level

string $class: Css class to add to target container

7 calls to AdminForm::addAjaxItem()
AdminForm::buildActivityTab in src/AdminForm.php
Activity settings
AdminForm::buildCaseTab in src/AdminForm.php
Case settings FIXME: This is exactly the same code as buildGrantTab. More utilities and less boilerplate needed.
AdminForm::buildContactTab in src/AdminForm.php
Build fields for a contact
AdminForm::buildContributionTab in src/AdminForm.php
Contribution settings
AdminForm::buildGrantTab in src/AdminForm.php
Grant settings FIXME: This is nearly the same code as buildCaseTab. More utilities and less boilerplate needed.

... See full list

File

src/AdminForm.php, line 1625
Webform CiviCRM module's admin form.

Class

AdminForm
@file Webform CiviCRM module's admin form.

Namespace

Drupal\webform_civicrm

Code

private function addAjaxItem($path, $control_element, $container, $class = 'civicrm-ajax-wrapper') {

  // Get a reference to the control container
  // For anyone who wants to call this evil - I challenge you to find a better way to accomplish this
  eval('$control_container = &$this->form[\'' . str_replace(':', "']['", $path) . "'];");

  // Now find the container element (may be outside the $path if .. is used)
  foreach (explode(':', $container) as $level) {
    if ($level == '..') {
      $path = substr($path, 0, strrpos($path, ':'));
    }
    else {
      $path .= ':' . $level;
    }
  }
  eval('$target_container = &$this->form[\'' . str_replace(':', "']['", substr($path, 0, strrpos($path, ':'))) . "'];");
  $id = 'civicrm-ajax-' . str_replace([
    ':',
    '_',
  ], '-', $path);
  $control_container[$control_element]['#ajax'] = [
    'callback' => '\\Drupal\\webform_civicrm\\Form\\WebformCiviCRMSettingsForm::pathstrAjaxRefresh',
    'pathstr' => $path,
    'wrapper' => $id,
    'effect' => 'fade',
  ];
  if (!isset($target_container[$level])) {
    $target_container[$level] = [];
  }
  $target_container[$level]['#prefix'] = '<div class="' . $class . '" id="' . $id . '">';
  $target_container[$level]['#suffix'] = '</div>';
}