Skip to content

Instantly share code, notes, and snippets.

@kane-thornwyrd
Created December 16, 2011 10:26
Show Gist options
  • Save kane-thornwyrd/1485525 to your computer and use it in GitHub Desktop.
Save kane-thornwyrd/1485525 to your computer and use it in GitHub Desktop.
Kane's best practice concerning Drupal Form's default Values.
<?php
function my_drupal_module_form_values($key, $form_state = array(), $default = NULL) {
$settings = array(
// Define here your form hierachies and default values
);
if (isset($form_state['values'])) {
$settings = array_merge($settings, $form_state['values']);
}
if (isset($form_state['storage']['my_form_root_element'])) {
$settings = array_merge($settings, $form_state['storage']);
}
$var = variable_get('my_form_root_element', FALSE);
if (is_array($var)) {
$settings = array_merge($settings, array('my_form_root_element' => $var));
}
$val = _extract_val($key, $settings);
$val = $val ? $val : $default;
return $val;
}
/**
* Recursively walk through the data array to search elements based on the string|array $search.
*
* @param string|array $search
* The sub-element of $data you search
* @param array $data
* The data to search in.
*
* @return mixed|NULL
* return element found or NULL. Don't forget to === for checking the result !
*/
function _extract_val($search, $data) {
if (is_array($search)) {
$key = key($search);
$path = array_shift($search);
if (isset($data[$key])) {
if (isset($path[0])) {
return _extract_val($path[0], $data[$key]);
}
else {
$subkey = key($path);
return _extract_val($path, $data[$key]);
}
}
else {
if (isset($data[$path[0]])) {
return $data[$path[0]];
}
}
}
else {
if (isset($data[$search])) {
return $data[$search];
}
}
return NULL;
}
function my_drupal_module_form($form, &$form_state) {
// REALLY IMPORTANT: your form HAVE to be "tree" !
$form = array(
'#tree' => TRUE,
'my_form_root_element'
);
$form['my_form_root_element']['my_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('My Fieldset Title'),
);
$form['my_form_root_element']['my_fieldset']['my_textfield'] = array(
'#type' => 'textfield',
'#title' => t('Textfield'),
'#default_value' => my_drupal_module_form_values(
array(
'my_form_root_element' => array(
'my_fieldset' => array(
'my_textfield'
)
)
), $form_state, t('My Default Value')),
);
return $form;
}
<?php
/**
* Used to return a field's value, the default one if noone has been set in the $form_state.
* Incidentally, this function embeds the default settings for all this module's forms.
*
* @param string|array $key
* The key of the searched value.
* @param NULL|array $form_state
* The form state, if available, to extract submitted values.
* @param NULL|mixed $default
* The default value. Caution ! MUSN'T be NULL if the form element
* expect an array (checkboxes, select, etc.).
*
* @return mixed|NULL
* return element found or NULL. Don't forget to === for checking the result !
*/
function HOOK_form_values($key, $form_state = array(), $default = NULL) {
$settings = array(
// Define here your form hierachies and default values
);
if (isset($form_state['values'])) {
$settings = array_merge($settings, $form_state['values']);
}
if (isset($form_state['storage']['my_form_root_element'])) {
$settings = array_merge($settings, $form_state['storage']);
}
$var = variable_get('my_form_root_element', FALSE);
if (is_array($var)) {
$settings = array_merge($settings, array('my_form_root_element' => $var));
}
return _extract_val($key, $settings);
}
/**
* Recursively walk through the data array to search elements based on the string|array $search.
*
* @param string|array $search
* The sub-element of $data you search
* @param array $data
* The data to search in.
*
* @return mixed|NULL
* return element found or NULL. Don't forget to === for checking the result !
*/
function _extract_val($search, $data) {
if (is_array($search)) {
$key = key($search);
$path = array_shift($search);
if (isset($data[$key])) {
if (isset($path[0])) {
return _extract_val($path[0], $data[$key]);
}
else {
$subkey = key($path);
return _extract_val($path, $data[$key]);
}
}
else {
if (isset($data[$path[0]])) {
return $data[$path[0]];
}
}
}
else {
if (isset($data[$search])) {
return $data[$search];
}
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment