function better_field_descriptions_fields_form in Better Field Descriptions 7
Implements hook_form().
1 string reference to 'better_field_descriptions_fields_form'
- better_field_descriptions_menu in ./
better_field_descriptions.module - Implements hook_menu().
File
- ./
better_field_descriptions.admin.inc, line 132 - Functionality and helper functions for Better field descriptions.
Code
function better_field_descriptions_fields_form($form, &$form_state) {
// Get entity type.
$entity_type = variable_get('better_field_descriptions_default_entity', 'node');
// Get better descriptions settings.
$bfds = variable_get('better_field_descriptions_settings', array());
// Get existing descriptions for fields.
$bfd = variable_get('better_field_descriptions', array());
// Use default template if not configured.
if (isset($bfd['template']) == FALSE || empty($bfd['template'])) {
$bfd['template'] = 'better-field-descriptions-text';
}
// Regex for locating all template files starting with 'better-descriptions'.
$regex = '/^better-field-descriptions.*\\.tpl\\.php$/i';
// Fetching template files from this module.
$path = drupal_get_path('module', 'better_field_descriptions');
$templates_module = drupal_system_listing($regex, $path, 'name', 0);
// Fetching template files from themes.
$templates_system = drupal_system_listing($regex, "themes", 'name', 0);
// Merges all template files allowing theme templates to override the modules
// templates.
$templates = array_merge($templates_module, $templates_system);
$form['#templates'] = $templates;
// Collects all templates found into array for select list.
$better_descriptions_templates = array();
foreach ($templates as $template) {
$path = $template->uri;
// Removing the '.tpl' if exists.
if (($pos = strpos($template->name, '.')) !== FALSE) {
$template = substr($template->name, 0, $pos);
}
$better_descriptions_templates[$template] = $template;
}
// Possible positions for the better description.
$positions = array(
0 => t('Above title and input'),
1 => t('Below title and input'),
2 => t('Between title and input'),
);
$form['entity_type'] = array(
'#type' => 'markup',
'#markup' => '<h1>Entity type: ' . $entity_type . '</h1>',
);
$form['descriptions'] = array(
'#type' => 'markup',
'#markup' => t('Add/edit better descriptions to the fields below.'),
);
$form['bundles'] = array(
'#type' => 'item',
'#prefix' => '<div id="better-field-descriptions-form-id-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
// Template selection.
$form['bundles']['template'] = array(
'#type' => 'select',
'#title' => t('Select template for the descriptions'),
'#options' => $better_descriptions_templates,
'#default_value' => $bfd['template'],
'#description' => t('Changing this value will trigger a theme registry rebuild. You can also provide your own template, consult the documentation.'),
);
// Setting label, default if not set.
if (isset($bfd['default_label']) == FALSE) {
$default_label = t('Description');
}
else {
$default_label = $bfd['default_label'];
}
$form['bundles']['default_label'] = array(
'#type' => 'textfield',
'#title' => 'Default label for all field descriptions.',
'#default_value' => $default_label,
'#description' => t('This label will be used if not set in each of the fields below.'),
);
// Setting description display defaults if not set
if (isset($bfd['hide_default_descriptions']) == FALSE) {
$hide_defaults = FALSE;
}
else {
$hide_defaults = $bfd['hide_default_descriptions'];
}
$form['bundles']['hide_default_descriptions'] = array(
'#type' => 'checkbox',
'#title' => t('Hide default descriptions?'),
'#default_value' => $hide_defaults,
'#description' => t('Hide default descriptions where a "better description" exists.'),
);
// Get info on bundles.
$bundles = field_info_bundles($entity_type);
foreach ($bfds as $bundle_machine_name => $fields) {
// Check if node type exist.
$extra_fields = field_info_extra_fields($entity_type, $bundle_machine_name, 'form');
if (!empty($extra_fields)) {
// Array to hold fields in the node.
$fields_instances = [];
// Get info on pseudo fields, like title.
$extra_fields = field_info_extra_fields($entity_type, $bundle_machine_name, 'form');
if (isset($extra_fields['title'])) {
$fields_instances['title'] = $extra_fields['title'];
}
// Get info on regular fields to the bundle.
$fields_instances += field_info_instances($entity_type, $bundle_machine_name);
// Wrapping each bundle in a collapsed fieldset.
$form['bundles'][$bundle_machine_name] = [
'#type' => 'fieldset',
'#title' => $bundles[$bundle_machine_name]['label'],
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#prefix' => '<div id="better-field-descriptions-form-id-wrapper">',
'#suffix' => '</div>',
];
foreach ($fields as $field_machine_name) {
if (!empty($fields_instances[$field_machine_name])) {
// Descriptions.
$bfd_description = '';
if (isset($bfd[$bundle_machine_name][$field_machine_name]['description'])) {
$bfd_description = $bfd[$bundle_machine_name][$field_machine_name]['description'];
}
$form['bundles'][$bundle_machine_name][$field_machine_name]['description'] = [
'#type' => 'textarea',
'#title' => $fields_instances[$field_machine_name]['label'] . ' (' . $field_machine_name . ')',
'#default_value' => filter_xss($bfd_description, _field_filter_xss_allowed_tags()),
'#description' => t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', [
'@tags' => _field_filter_xss_display_allowed_tags(),
]),
];
// Label.
$bfd_label = '';
if (isset($bfd[$bundle_machine_name][$field_machine_name]['label'])) {
$bfd_label = $bfd[$bundle_machine_name][$field_machine_name]['label'];
}
$form['bundles'][$bundle_machine_name][$field_machine_name]['label'] = [
'#type' => 'textfield',
'#title' => 'Label for this field description',
'#default_value' => filter_xss($bfd_label),
'#description' => t('Label for this field description.'),
];
$position = 1;
if (isset($bfd[$bundle_machine_name][$field_machine_name]['position'])) {
$position = $bfd[$bundle_machine_name][$field_machine_name]['position'];
}
// Position of description.
$form['bundles'][$bundle_machine_name][$field_machine_name]['position'] = [
'#type' => 'radios',
'#title' => 'Position of description.',
'#options' => $positions,
'#default_value' => $position,
'#description' => t('Position the description field above or below the input field. When choosing the between-option the #title of the field will be used as label (if label is left blank) and the original field #title will be set as invisible.'),
];
}
}
}
}
$form['#submit'] = array(
'better_field_descriptions_fields_submit',
);
return system_settings_form($form);
}