webform_attributes.functions.inc in Webform Attributes 7
Same filename and directory in other branches
Functions related the Webform Attributes.
File
includes/webform_attributes.functions.incView source
<?php
/**
* @file
* Functions related the Webform Attributes.
*/
/**
* Callback form submit.
*/
function webform_attributes_configure_form_submit(&$form, &$form_state) {
$form_values = $form_state['values'];
if (!isset($form_values['webform_attributes_form'])) {
return FALSE;
}
$node = $form['#node'];
$attributes = $form_values['webform_attributes_form'];
if (empty($attributes)) {
// Delete record.
return _webform_attributes_delete_record($node->nid);
}
// Parser values.
$attributes = _webform_attributes_parser_string_to_array($attributes);
// Insert/update record.
_webform_attributes_save_record($node->nid, $attributes);
}
/**
* Put attributes inside attributes property.
*/
function _webform_attributes_inside_attributes(&$element, array $attributes) {
foreach ($attributes as $attribute => $value) {
$element['#attributes'][check_plain($attribute)] = $value;
}
}
/**
* Alias for list_extract_allowed_values.
*
* @param string $string
* The list of allowed values in string format.
*
* @return bool|array
* The array of extracted key/value pairs, or FALSE if the string is invalid.
*
* @see list_extract_allowed_values()
*/
function _webform_attributes_parser_string_to_array($string) {
if (!empty($string)) {
return list_extract_allowed_values($string, 'list_text', FALSE);
}
return FALSE;
}
/**
* Alias for list_allowed_values_string.
*
* @param array $attributes
* An array of values, where array keys are values and array values are
* labels.
*
* @return bool|string
* The string representation of the $values array:
* - Values are separated by a carriage return.
* - Each value is in the format "value|label" or "value".
*
* @see list_allowed_values_string()
*/
function _webform_attributes_parser_array_to_string(array $attributes) {
if (!empty($attributes)) {
return list_allowed_values_string($attributes);
}
return FALSE;
}
/**
* Find an existing record.
*
* @param int $nid
* Node nid.
*
* @return array
* Return array with attributes when found it.
*/
function _webform_attributes_find_record_by_nid($nid, $parser = TRUE) {
$result = db_select('webform_attributes', 'wa')
->fields('wa', array(
'attributes',
))
->condition('nid', $nid, '=')
->execute()
->fetchField();
if (!empty($result)) {
$result = unserialize($result);
if ($parser) {
return _webform_attributes_parser_array_to_string($result);
}
return $result;
}
return NULL;
}
/**
* Insert a new record or update an existing record.
*
* @param int $nid
* Node nid.
* @param array $attributes
* Form attributes.
*
* @return MergeQuery
* A new MergeQuery object for this connection.
*/
function _webform_attributes_save_record($nid, array $attributes) {
return db_merge('webform_attributes')
->key(array(
'nid' => $nid,
))
->fields(array(
'attributes' => serialize($attributes),
'changed' => REQUEST_TIME,
))
->execute();
}
/**
* Remove an existing record.
*
* @param int $nid
* Node nid.
*
* @return DeleteQuery
* A new DeleteQuery object for this connection.
*/
function _webform_attributes_delete_record($nid) {
return db_delete('webform_attributes')
->condition('nid', $nid)
->execute();
}
/**
* Alter render for all components.
*/
function _webform_attributes_webform_component_render_alter_all_components(&$element, &$component) {
if (!empty($element['#webform_component']['extra']['webform_attributes_data_field'])) {
$html_data = $element['#webform_component']['extra']['webform_attributes_data_field'];
$html_datas = list_extract_allowed_values($html_data, 'list_text', FALSE);
foreach ($html_datas as $attribute => $value) {
$element['#attributes'][check_plain($attribute)] = $value;
}
}
}
/**
* Form alter for all components.
*/
function _webform_attributes_form_webform_component_edit_form_alter_all_components(&$form, &$form_state, $form_id, $node) {
$default_value = _webform_attributes_get_value_extra($node, 'webform_attributes_data_field');
$field = array(
'#type' => 'textarea',
'#default_value' => $default_value,
'#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,
);
$form['extra']['webform_attributes_data_field'] = $field;
}
/**
* Find value in extra value webform.
*/
function _webform_attributes_get_value_extra($node, $find_extra_element) {
$default_value = NULL;
$arg = arg();
if ($arg[4] === 'new') {
return $default_value;
}
$extra_elements = $node->webform['components'][$arg[4]]['extra'];
if (!empty($extra_elements[$find_extra_element])) {
$default_value = $extra_elements[$find_extra_element];
}
return $default_value;
}
Functions
Name![]() |
Description |
---|---|
webform_attributes_configure_form_submit | Callback form submit. |
_webform_attributes_delete_record | Remove an existing record. |
_webform_attributes_find_record_by_nid | Find an existing record. |
_webform_attributes_form_webform_component_edit_form_alter_all_components | Form alter for all components. |
_webform_attributes_get_value_extra | Find value in extra value webform. |
_webform_attributes_inside_attributes | Put attributes inside attributes property. |
_webform_attributes_parser_array_to_string | Alias for list_allowed_values_string. |
_webform_attributes_parser_string_to_array | Alias for list_extract_allowed_values. |
_webform_attributes_save_record | Insert a new record or update an existing record. |
_webform_attributes_webform_component_render_alter_all_components | Alter render for all components. |