You are here

link.inc in Webform Link 6

Same filename and directory in other branches
  1. 7 components/link.inc

Webform Link component

File

components/link.inc
View source
<?php

/**
 * @file
 * Webform Link component
 */

/**
 * Implements _webform_defaults_component().
 */
function _webform_defaults_link() {
  return array(
    'name' => '',
    'form_key' => NULL,
    'pid' => 0,
    'weight' => 0,
    'value' => '',
    'mandatory' => 0,
    'extra' => array(
      'private' => FALSE,
      'description' => '',
    ),
  );
}

/**
 * Implements _webform_edit_component().
 */
function _webform_edit_link($component) {
  $form = array();
  $form['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Default URL'),
    '#default_value' => $component['value'],
    '#description' => t('The default URL of the field.') . theme('webform_token_help'),
    '#size' => 60,
    '#maxlength' => 1024,
    '#weight' => 0,
  );
  return $form;
}

/**
 * Implements _webform_theme_component().
 */
function _webform_theme_link() {
  return array(
    'webform_display_link' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'components/link.inc',
    ),
  );
}

/**
 * Implements _webform_render_component().
 */
function _webform_render_link($component, $value = NULL, $filter = TRUE) {
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  $element = array(
    '#type' => 'textfield',
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    '#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
    '#attributes' => $component['extra']['attributes'],
    '#theme_wrappers' => array(
      'webform_element_wrapper',
    ),
    '#element_validate' => array(
      'webform_link_validate',
    ),
  );
  if (isset($value[0])) {
    $element['#default_value'] = $value[0];
  }
  return $element;
}

/**
 * Implements _webform_display_component().
 */
function _webform_display_link($component, $value, $format = 'html') {
  $element = array(
    '#title' => $component['name'],
    '#weight' => $component['weight'],
    '#theme' => 'webform_display_link',
    '#theme_wrappers' => $format == 'html' ? array(
      'webform_element',
      'webform_element_wrapper',
    ) : array(
      'webform_element_text',
    ),
    '#post_render' => array(
      'webform_element_wrapper',
    ),
    '#format' => $format,
    '#value' => isset($value[0]) ? $value[0] : '',
  );
  return $element;
}

/**
 * Format the output of data for this component.
 */
function theme_webform_display_link($element) {
  return $element['#format'] == 'html' ? l($element['#value'], $element['#value']) : $element['#value'];
}

/**
 * Implements _webform_table_component().
 */
function _webform_table_link($component, $value) {
  return l($value[0], $value[0]);
}

/**
 * Implements _webform_csv_headers_component().
 */
function _webform_csv_headers_link($component, $export_options) {
  $header = array();
  $header[0] = '';
  $header[1] = '';
  $header[2] = $component['name'];
  return $header;
}

/**
 * Implements _webform_csv_data_component().
 */
function _webform_csv_data_link($component, $export_options, $value) {
  return $value[0];
}

/**
 * Form validation handler.
 *
 * If a link was submitted, check that the URL is valid.
 */
function webform_link_validate($element, &$form_state) {
  if ($element['#value'] && !valid_url($element['#value'], TRUE)) {
    $message = t('%url is not a valid URL.', array(
      '%url' => $element['#value'],
    ));
    form_error($element, $message);
  }
}