You are here

function soft_length_limit_field_widget_form_alter in Soft Length Limit 8

Implements hook_field_widget_form_alter().

File

./soft_length_limit.module, line 136

Code

function soft_length_limit_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $third_party_settings = $context['widget']
    ->getThirdPartySettings();
  if (empty($third_party_settings['soft_length_limit'])) {
    return NULL;
  }
  $sll_config = $third_party_settings['soft_length_limit'];
  if (isset($sll_config['max_limit'])) {
    $element['value']['#attributes']['data-soft-length-limit'] = $sll_config['max_limit'];
    $element['value']['#attributes']['class'][] = 'soft-length-limit';
  }
  if (isset($sll_config['minimum_limit'])) {
    $element['value']['#attributes']['data-soft-length-minimum'] = $sll_config['minimum_limit'];
  }

  // Length style select.
  if (isset($sll_config['style_select']) && $sll_config['style_select']) {
    $element['value']['#attributes']['data-soft-length-style-select'] = (int) $sll_config['style_select'];
  }
  if (isset($element['#type']) && $element['#type'] === 'text_format') {
    $element['#attributes'] = array_merge_recursive($element['#attributes'], $element['value']['#attributes']);
  }
  $element['#attached']['library'][] = 'soft_length_limit/soft_length_limit';
}