You are here

webform_attributes.module in Webform Attributes 7.2

Same filename and directory in other branches
  1. 7 webform_attributes.module

Functions related the Webform Attributes.

File

webform_attributes.module
View source
<?php

/**
 * @file
 * Functions related the Webform Attributes.
 */

/**
 * Files include.
 */
function _webform_attributes_files_include() {
  $file = 'includes/webform_attributes.functions';
  module_load_include('inc', 'webform_attributes', $file);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function webform_attributes_form_webform_admin_settings_alter(&$form) {
  _webform_attributes_files_include();
  $form['advanced']['webform_attributes_which_component_allowed_html_attribute'] = array(
    '#type' => 'checkboxes',
    '#default_value' => webform_attributes_get_which_component_allowed_html_attribute(),
    '#options' => webform_component_options(TRUE),
    '#title' => t('Choose which component you can add HTML attributes:'),
    '#description' => t('Choose which component you can add HTML attributes, by default all components are enabled.'),
    '#required' => FALSE,
    '#weight' => -1,
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function webform_attributes_form_webform_configure_form_alter(&$form) {
  _webform_attributes_files_include();

  // Custom textarea to add attributes on form.
  $form['advanced']['webform_attributes_form'] = array(
    '#type' => 'textarea',
    '#default_value' => _webform_attributes_find_record_by_nid($form['#node']->nid),
    '#title' => t('HTML attributes'),
    '#description' => t('Key-value pairs MUST be specified as "attribute_name|attribute_value". Use of only alphanumeric characters and underscores is recommended in keys. One attribute per line.'),
    '#required' => FALSE,
    '#weight' => -8,
    '#element_validate' => array(
      '_webform_attributes_validate_options_attributes',
    ),
  );

  // Add custom submit function to save values in database.
  $form['#submit'][] = "webform_attributes_configure_form_submit";
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function webform_attributes_form_webform_client_form_alter(&$form) {
  _webform_attributes_files_include();

  // Get attributes in database and apply in form.
  if ($attributes = _webform_attributes_find_record_by_nid($form['#node']->nid, FALSE)) {
    _webform_attributes_inside_attributes($form, $attributes);
  }
}

/**
 * Implements hook_webform_component_render_alter().
 */
function webform_attributes_webform_component_render_alter(&$element) {
  _webform_attributes_files_include();
  $component_type = !empty($element['#type']) ? $element['#type'] : 'custom_type_not_mapped';
  $components_allowed = webform_attributes_get_which_component_allowed_html_attribute();
  if (in_array($component_type, $components_allowed)) {

    // Alter render for all components, apply html attributes in the inputs
    // and their wrapper.
    _webform_attributes_webform_component_render_alter_all_components($element);
  }
}

/**
 * Implements hook_form_webform_component_edit_form_alter().
 */
function webform_attributes_form_webform_component_edit_form_alter(&$form) {
  _webform_attributes_files_include();
  $component_type = $form['type']['#value'];
  $components_allowed = webform_attributes_get_which_component_allowed_html_attribute();
  if (in_array($component_type, $components_allowed)) {
    $node = $form['#node'];

    // Add custom attributes for all components.
    _webform_attributes_form_webform_component_edit_form_alter_all_components($form, $node);
  }
}