You are here

function soft_length_limit_set_attr in Soft Length Limit 7

Recurse through form and set variables.

Recursive helper function that sets the correct attributes for the form elements with a specific soft limit specified, and continues through child elements.

Parameters

array $element: The form element to iterate through

array $sub_elements: Array of the elements which should have a soft limit attribute

1 call to soft_length_limit_set_attr()
soft_length_limit_field_attach_form in ./soft_length_limit.module
Implements hook_field_attach_form().

File

./soft_length_limit.module, line 200
Soft Length Limit module

Code

function soft_length_limit_set_attr(&$element, $sub_elements) {
  $children = element_get_visible_children($element);
  $types = _soft_length_limit_types('elements');
  foreach ($children as $value) {
    if (isset($element[$value]['#type']) && isset($types[$element[$value]['#type']])) {
      if (isset($element[$value]['#field_name']) && isset($sub_elements[$element[$value]['#field_name']])) {
        $widget_settings = isset($sub_elements[$element[$value]['#field_name']]['widget']['settings']) ? $sub_elements[$element[$value]['#field_name']]['widget']['settings'] : FALSE;
        if ($widget_settings) {

          // Soft limit length.
          $soft_limit = $widget_settings['soft_length_limit'];
          $element[$value]['#soft_length_limit'] = isset($element[$value]['#maxlength']) && $soft_limit > $element[$value]['#maxlength'] ? $element[$value]['#maxlength'] : $soft_limit;
          $element[$value]['#attributes']['data-soft-length-limit'] = $soft_limit;
          $element[$value]['#attributes']['class'][] = 'soft-length-limit';

          // Soft minimum.
          $soft_min = isset($widget_settings['soft_length_minimum']) ? $widget_settings['soft_length_minimum'] : '';
          $element[$value]['#attributes']['data-soft-length-minimum'] = $soft_min;

          // Length style select.
          if (isset($widget_settings['soft_length_style_select'])) {
            $element[$value]['#attributes']['data-soft-length-style-select'] = $widget_settings['soft_length_style_select'];
          }
        }
      }
    }
    soft_length_limit_set_attr($element[$value], $sub_elements);
  }
}