You are here

function _webform_render_email in Webform 7.4

Same name and namespace in other branches
  1. 5.2 components/email.inc \_webform_render_email()
  2. 5 components/email.inc \_webform_render_email()
  3. 6.3 components/email.inc \_webform_render_email()
  4. 6.2 components/email.inc \_webform_render_email()
  5. 7.3 components/email.inc \_webform_render_email()

Implements _webform_render_component().

File

components/email.inc, line 144
Webform module email component.

Code

function _webform_render_email($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  $element = array(
    '#type' => 'webform_email',
    '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    '#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $component['value'],
    '#required' => $component['required'],
    '#weight' => $component['weight'],
    '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
    '#attributes' => $component['extra']['attributes'],
    '#element_validate' => array(
      '_webform_validate_email',
    ),
    '#theme_wrappers' => array(
      'webform_element',
    ),
    '#translatable' => array(
      'title',
      'description',
      'placeholder',
    ),
  );
  if ($component['required']) {
    $element['#attributes']['required'] = 'required';
  }

  // Add an e-mail class for identifying the difference from normal textfields.
  $element['#attributes']['class'][] = 'email';

  // Enforce uniqueness.
  if ($component['extra']['unique']) {
    $element['#element_validate'][] = 'webform_validate_unique';
  }
  if ($component['extra']['format'] == 'long') {

    // html5 email elements enforce short-form email validation in addition to
    // pattern validation. This means that long format email addresses must be
    // rendered as text.
    $element['#attributes']['type'] = 'text';

    // html5 patterns have implied delimiters and start and end patterns.
    // The are also case sensitive, not global, and not multi-line.
    // See https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation
    // See http://stackoverflow.com/questions/19605773/html5-email-validation
    $address = '[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*';
    $name = '("[^<>"]*?"|[^<>",]*?)';
    $short_long = "({$name} *<{$address}>|{$address})";
    $element['#attributes']['pattern'] = $component['extra']['multiple'] ? "{$short_long}(, *{$short_long})*" : $short_long;
  }
  elseif ($component['extra']['multiple']) {
    $element['#attributes']['multiple'] = 'multiple';
  }
  if (isset($value[0])) {
    $element['#default_value'] = $value[0];
  }
  if ($component['extra']['placeholder']) {
    $element['#attributes']['placeholder'] = $component['extra']['placeholder'];
  }
  if ($component['extra']['disabled']) {
    if ($filter) {
      $element['#attributes']['readonly'] = 'readonly';
    }
    else {
      $element['#disabled'] = TRUE;
    }
  }

  // Change the 'width' option to the correct 'size' option.
  if ($component['extra']['width'] > 0) {
    $element['#size'] = $component['extra']['width'];
  }
  return $element;
}