You are here

function Utils::wf_crm_enabled_fields in Webform CiviCRM Integration 8.5

Get ids or values of enabled CiviCRM fields for a webform.

Parameters

\Drupal\webform\WebformInterface $webform: The webform.

array|null $submission: (optional) if supplied, will match field keys with submitted values

boolean $show_all: (optional) if true, get every field even if it belongs to a contact that does not exist

Return value

array of enabled fields

Overrides UtilsInterface::wf_crm_enabled_fields

File

src/Utils.php, line 444
Webform CiviCRM module's common utility functions.

Class

Utils

Namespace

Drupal\webform_civicrm

Code

function wf_crm_enabled_fields(WebformInterface $webform, $submission = NULL, $show_all = FALSE) {
  $enabled = [];
  $elements = $webform
    ->getElementsDecodedAndFlattened();
  if (!empty($elements) || $show_all) {
    $handler_collection = $webform
      ->getHandlers('webform_civicrm');
    if (!$handler_collection
      ->has('webform_civicrm')) {
      return $enabled;
    }

    /** @var \Drupal\webform\Plugin\WebformHandlerInterface $handler */
    $handler = $handler_collection
      ->get('webform_civicrm');
    $handler_configuration = $handler
      ->getConfiguration();
    $fields = $this
      ->wf_crm_get_fields();
    foreach ($elements as $key => $c) {
      $exp = explode('_', $key, 5);
      $customGroupFieldsetKey = '';
      if (count($exp) == 5) {
        list($lobo, $i, $ent, $n, $id) = $exp;
        if ($lobo != 'civicrm') {
          continue;
        }
        $explodedId = explode('_', $id);
        if (wf_crm_aval($explodedId, 1) == 'fieldset' && $explodedId[0] != 'fieldset') {
          $customGroupFieldsetKey = $explodedId[0];

          // Automatically enable 'Create mode' field for Contact's custom group.
          if ($ent === 'contact') {
            $enabled[$lobo . '_' . $i . '_' . $ent . '_' . $n . '_' . $customGroupFieldsetKey . '_createmode'] = 1;
          }
        }
        if ((isset($fields[$id]) || $id == 'fieldset_fieldset' || $id == $customGroupFieldsetKey . '_fieldset') && is_numeric($i) && is_numeric($n)) {
          if (!$show_all && ($ent == 'contact' || $ent == 'participant') && empty($handler_configuration['settings']['data']['contact'][$i])) {
            continue;
          }
          if ($submission) {
            $enabled[$key] = wf_crm_aval($submission, $c['#form_key'], NULL, TRUE);
          }
          else {
            $enabled[$key] = $key;
          }
        }
      }
    }
  }
  return $enabled;
}