function bootstrap_element_smart_description

8.x-3.x deprecated.php bootstrap_element_smart_description(array &$element, array &$target = NULL, $input_only = TRUE, $length = NULL)
7.x-3.x common.inc bootstrap_element_smart_description(array &$element, array &$target = NULL, $input_only = TRUE, $length = NULL)

Converts an element description into a tooltip based on certain criteria.

Parameters

array $element: An element render array, passed by reference.

array $target: The target element render array the tooltip is to be attached to, passed by reference. If not set, it will default to the $element passed.

bool $input_only: Toggle determining whether or not to only convert input elements.

int $length: The length of characters to determine if description is "simple".

Related topics

Source includes/common.inc (line 34)

function bootstrap_element_smart_description(array &$element, array &$target = NULL, $input_only = TRUE, $length = NULL) {
  // Determine if tooltips are enabled.
  static $enabled;
  if (!isset($enabled)) {
    $enabled = bootstrap_setting('tooltip_enabled') && bootstrap_setting('forms_smart_descriptions');
  }

  // Immediately return if "simple" tooltip descriptions are not enabled.
  if (!$enabled) {
    return;
  }

  // Allow a different element to attach the tooltip.
  if (!isset($target)) {
    $target = &$element;
  }

  // Retrieve the length limit for smart descriptions.
  if (!isset($length)) {
    $length = (int) bootstrap_setting('forms_smart_descriptions_limit');
    // Disable length checking by setting it to FALSE if empty.
    if (empty($length)) {
      $length = FALSE;
    }
  }

  // Retrieve the allowed tags for smart descriptions. This is primarily used
  // for display purposes only (i.e. non-UI/UX related elements that wouldn't
  // require a user to "click", like a link).
  $allowed_tags = array_filter(array_unique(array_map('trim', explode(',', bootstrap_setting('forms_smart_descriptions_allowed_tags') . ''))));

  // Disable length checking by setting it to FALSE if empty.
  if (empty($allowed_tags)) {
    $allowed_tags = FALSE;
  }

  $html = FALSE;
  $type = !empty($element['#type']) ? $element['#type'] : FALSE;

  // Return if element or target shouldn't have "simple" tooltip descriptions.
  if (($input_only && !isset($target['#input']))
    // Ignore text_format elements.
    // @see https://www.drupal.org/node/2478339
     || $type === 'text_format'

      // Ignore if the actual element has no #description set.
       || empty($element['#description'])

      // Ignore if the target element already has a "data-toggle" attribute set.
       || !empty($target['#attributes']['data-toggle'])

      // Ignore if the target element is #disabled.
       || isset($target['#disabled'])

      // Ignore if either the actual element or target element has an explicit
      // #smart_description property set to FALSE.
       || (isset($element['#smart_description']) && !$element['#smart_description'])
     || (isset($target['#smart_description']) && !$target['#smart_description'])

      // Ignore if the description is not "simple".
       || !_bootstrap_is_simple_string($element['#description'], $length, $allowed_tags, $html)
    ) {
    // Set the both the actual element and the target element
    // #smart_description property to FALSE.
    $element['#smart_description'] = FALSE;
    $target['#smart_description'] = FALSE;
    return;
  }

  // Default property (on the element itself).
  $property = 'attributes';

  // Add the tooltip to the #label_attributes property for 'checkbox'
  // and 'radio' elements.
  if ($type === 'checkbox' || $type === 'radio') {
    $property = 'label_attributes';
  }
  // Add the tooltip to the #wrapper_attributes property for 'checkboxes'
  // and 'radios' elements.
  elseif ($type === 'checkboxes' || $type === 'radios') {
    $property = 'wrapper_attributes';
  }
  // Add the tooltip to the #input_group_attributes property for elements
  // that have valid input groups set.
  elseif ((!empty($element['#field_prefix']) || !empty($element['#field_suffix'])) && (!empty($element['#input_group']) || !empty($element['#input_group_button']))) {
    $property = 'input_group_attributes';
  }

  // Retrieve the proper attributes array.
  $attributes = &_bootstrap_get_attributes($target, $property);

  // Set the tooltip attributes.
  $attributes['title'] = $allowed_tags !== FALSE ? filter_xss($element['#description'], $allowed_tags) : $element['#description'];
  $attributes['data-toggle'] = 'tooltip';
  if ($html || $allowed_tags === FALSE) {
    $attributes['data-html'] = 'true';
  }

  // Remove the element description so it isn't (re-)rendered later.
  unset($element['#description']);
}