You are here

function webform_icheck_webform_element_alter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_icheck/webform_icheck.module \webform_icheck_webform_element_alter()

Implements hook_webform_element_alter().

File

modules/webform_icheck/webform_icheck.module, line 72
Provides support for highly customizable checkboxes and radio buttons.

Code

function webform_icheck_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) {
  $element_type = isset($element['#type']) ? $element['#type'] : '';
  if (!_webform_icheck_is_supported($element_type)) {
    return;
  }

  /** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
  $third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
  $default_icheck = $third_party_settings_manager
    ->getThirdPartySetting('webform_icheck', 'default_icheck');
  $icheck = NULL;
  $icheck_skin = NULL;
  if (isset($element['#icheck'])) {
    if ($element['#icheck'] !== 'none') {
      $icheck = $element['#icheck'];
      $icheck_skin = strtok($element['#icheck'], '-');
    }
  }
  elseif ($default_icheck) {
    $icheck = $default_icheck;
    $icheck_skin = strtok($default_icheck, '-');
  }
  if ($icheck) {
    $element['#attributes']['data-webform-icheck'] = $icheck;
    $element['#attached']['library'][] = 'webform_icheck/webform_icheck.element';
    $element['#attached']['library'][] = 'webform_icheck/libraries.jquery.icheck.' . $icheck_skin;
  }
}