form_styler.module in jQuery form styler 7.2
Same filename and directory in other branches
Attach jQuery form styler plugin to selected forms.
@TODO: Allow change more plugin settings to admin interface.
File
form_styler.moduleView source
<?php
/**
* @file
* Attach jQuery form styler plugin to selected forms.
*
* @TODO: Allow change more plugin settings to admin interface.
*/
/**
* Implements hook_libraries_info().
*/
function form_styler_libraries_info() {
$libraries['jquery_form_styler'] = array(
'name' => 'jQuery form styler',
'vendor url' => 'https://github.com/Dimox/jQueryFormStyler/',
'download url' => 'https://github.com/Dimox/jQueryFormStyler/archive/master.zip',
'version arguments' => array(
'file' => 'dist/jquery.formstyler.min.js',
'pattern' => '/jQuery\\s+Form\\s+Styler\\s+v?([0-9\\.]+)/',
),
'files' => array(
'js' => array(
'dist/jquery.formstyler.min.js',
),
'css' => array(
'dist/jquery.formstyler.css',
'dist/jquery.formstyler.theme.css',
),
),
'variants' => array(
'minified' => array(
'files' => array(
'js' => array(
'dist/jquery.formstyler.min.js',
),
'css' => array(
'dist/jquery.formstyler.css',
'dist/jquery.formstyler.theme.css',
),
),
),
'source' => array(
'files' => array(
'js' => array(
'dist/jquery.formstyler.js',
),
'css' => array(
'dist/jquery.formstyler.css',
'dist/jquery.formstyler.theme.css',
),
),
),
'onlyjs' => array(
'files' => array(
'js' => array(
'dist/jquery.formstyler.min.js',
),
'css' => array(
'dist/jquery.formstyler.css',
),
),
),
),
);
return $libraries;
}
/**
* Check if the backstretch libraries have been loaded.
*
* @return bool
* A boolean indicating the loaded status.
*/
function form_styler_plugin_loaded() {
$jquery_form_styler = libraries_load('jquery_form_styler');
return !empty($jquery_form_styler['loaded']);
}
/**
* Implements hook_permission().
*/
function form_styler_permission() {
return array(
'administer form styler' => array(
'title' => t('Change settings'),
'description' => t('Perform set form ids for including form styler.'),
),
);
}
/**
* Implements hook_menu().
*/
function form_styler_menu() {
$items['admin/config/user-interface/form-styler'] = array(
'title' => 'Form styler settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'form_styler_admin_form',
),
'access arguments' => array(
'administer form styler',
),
);
return $items;
}
/**
* Administration page callbacks for the form_styler module.
*/
function form_styler_admin_form($form, $form_state) {
$library = libraries_detect('jquery_form_styler');
if (empty($library['installed'])) {
$error_message = isset($library['error message']) ? $library['error message'] : '';
$placeholders = array(
'!error' => $error_message,
'!form_styler' => l(t('jQuery form styler plugin'), $library['download url']),
'%path' => 'sites/all/libraries',
);
$message = t('!error You need to download the !form_styler, extract the archive, rename it to "jquery_form_styler" and place the plugin directory in the %path directory on your server.', $placeholders);
drupal_set_message($message, 'error');
return array();
}
// Vertical Tabs group.
$form['form_styler'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
// Create settings form fieldsets.
$form['plugin'] = array(
'#type' => 'fieldset',
'#title' => t('Main settings and performance'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'form_styler',
);
$form['themes'] = array(
'#type' => 'fieldset',
'#title' => t('Theme Overrides'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'form_styler',
);
$description_options = array(
'!plugin_link' => url($library['vendor url'], array(
'externak' => TRUE,
)),
);
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('JS settigns'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'form_styler',
'#description' => t('Allow to chage default options of plugin. More information about options you can see at <a href="!plugin_link">Plugin page</a>.', $description_options),
);
// Textfield for form ids.
$form['plugin']['form_styler_ids'] = array(
'#title' => t('Form ids'),
'#type' => 'textarea',
'#description' => t('Specify the id of form for which you want to use form styler'),
'#default_value' => variable_get('form_styler_ids', ''),
);
// Allow users to select form styler plugin variant.
$form['plugin']['form_styler_variants'] = array(
'#title' => t('Select variant'),
'#type' => 'radios',
'#options' => array(
'minified' => t('Compressed'),
'source' => t('Uncompressed'),
'onlyjs' => t('Only compressed js (without css)'),
),
'#description' => t('Select variants for attaching form styler'),
'#default_value' => variable_get('form_styler_variants', 'minified'),
);
// Get Theme overrides information.
$themes = list_themes();
$theme_default = variable_get('theme_default', FALSE);
$admin_theme = variable_get('admin_theme', FALSE);
// Header form theme ovverides table.
$header = array(
t('Theme'),
t('Status'),
t('Operations'),
);
$rows = array();
$link_options = array(
'query' => drupal_get_destination(),
'fragment' => 'edit-form-styler',
);
foreach ($themes as $theme_key => $theme) {
// Skip disabled themes, but only if they are not configured as admin
// theme. This is an inconsistency in drupal core, that you can select a
// disabled theme as admin theme.
if (!$theme->status && $theme_key !== $admin_theme) {
continue;
}
$theme_name = $theme->info['name'];
// Provide additional information for default and admin themes.
if ($theme_key === $theme_default && ($theme_key === $admin_theme || empty($admin_theme))) {
$theme_name .= ' (' . t('default/admin theme') . ')';
}
elseif ($theme_key === $theme_default) {
$theme_name .= ' (' . t('default theme') . ')';
}
elseif ($theme_key === $admin_theme) {
$theme_name .= ' (' . t('admin theme') . ')';
}
$status = theme_get_setting('form_styler_all_forms', $theme_key);
// Create the table rows.
$rows[] = array(
$theme_name,
$status ? t('Enabled on all forms') : t('Disabled'),
l(t('Configure'), 'admin/appearance/settings/' . $theme_key, $link_options),
);
}
// Attach theme overrides information table to form.
$form['themes']['overrides'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$default_options = form_styler_get_default_options();
foreach ($default_options as $option_name => $option) {
$type = isset($option['type']) ? $option['type'] : 'textfield';
$option_key = 'form_styler_opt_' . $option_name;
$form['options'][$option_key] = array(
'#type' => $type,
'#title' => $option['title'],
'#default_value' => variable_get($option_key, $option['default']),
);
if (isset($option['description'])) {
$form['options'][$option_key]['#description'] = $option['description'];
}
if (isset($option['related'])) {
$form['options'][$option_key]['#states'] = array(
'visible' => array(
':input[name="form_styler_opt_' . $option['related']['field'] . '"]' => $option['related']['condition'],
),
);
}
}
return system_settings_form($form);
}
/**
* Implements hook_form_alter().
*/
function form_styler_form_alter(&$form, &$form_state, $form_id) {
// Check enabled for all forms in theme.
$attach = theme_get_setting('form_styler_all_forms');
// Check form ids for attaching plugin.
if (!$attach) {
$raw_ids = str_replace('#', '', variable_get('form_styler_ids', ''));
$ids = explode(PHP_EOL, variable_get('form_styler_ids', ''));
$ids = array_filter(array_map('trim', $ids));
$attach = in_array($form_id, $ids);
}
// Comparing for default HTML-id.
if (!$attach && isset($form['#id'])) {
$html_id = $form['#id'];
$attach = in_array($html_id, $ids);
}
if ($attach) {
// Check librari is instaled or not.
$library_name = 'jquery_form_styler';
if (($library = libraries_detect($library_name)) && !empty($library['installed'])) {
// Check selected variant.
$variant = variable_get('form_styler_variants', 'minified');
$module_path = drupal_get_path('module', 'form_styler');
$form['#attributes']['class'][] = 'form-styler-ready';
$form['#attached']['js'][] = $module_path . '/js/form_styler-init.js';
if ($variant !== 'onlyjs') {
$form['#attached']['css'][] = $module_path . '/css/form_styler.css';
}
$form['#attached']['libraries_load'][] = array(
$library_name,
$variant,
);
$default_options = form_styler_get_default_options();
// Attach form styler settings.
$settings = array();
foreach ($default_options as $option_name => $option) {
$option_key = 'form_styler_opt_' . $option_name;
$value = variable_get($option_key, $option['default']);
if (isset($option['translatable'])) {
$translatable = $option['translatable'];
}
else {
$translatable = FALSE;
}
$settings['options'][$option_name] = $translatable ? t($value) : $value;
}
$form['#attached']['js'][] = array(
'data' => array(
'form_styler_settings' => $settings,
),
'type' => 'setting',
);
}
else {
// Log the error message if plugin not installed.
$error_message = $library['error message'];
watchdog('form_styler', $error_message, array(), WATCHDOG_ERROR);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function form_styler_form_system_theme_settings_alter(&$form, $form_state) {
// Ignore global theme settings.
if (empty($form_state['build_info']['args'][0])) {
return;
}
$form['form_styler'] = array(
'#type' => 'fieldset',
'#title' => t('jQuery form styler'),
'#description' => t('You can optionally enable form_styler for all forms on this theme.'),
);
$form['form_styler']['form_styler_all_forms'] = array(
'#type' => 'checkbox',
'#title' => t('Enable on all forms'),
'#default_value' => theme_get_setting('form_styler_all_forms', $form_state['build_info']['args'][0]),
);
}
/**
* Implements hook_get_default_options().
*/
function form_styler_get_default_options() {
// Create JavaScript settings options.
// @todo allow to edit more options
$defaultOptions = array(
'filePlaceholder' => array(
'default' => 'File not selected',
'title' => t('File select placeholder'),
'translatable' => TRUE,
),
'fileBrowse' => array(
'default' => 'Browse...',
'title' => 'File browse button text',
'translatable' => TRUE,
),
'selectPlaceholder' => array(
'default' => 'Select...',
'title' => t('Select field placeholder'),
'translatable' => TRUE,
),
'selectSearch' => array(
'default' => FALSE,
'title' => t('Show search field'),
'type' => 'checkbox',
),
'selectSearchLimit' => array(
'default' => 10,
'title' => t('Options count for search'),
'description' => t('Minimal options count for show search field in select box'),
'related' => array(
'field' => 'selectSearch',
'condition' => array(
'checked' => TRUE,
),
),
),
);
return $defaultOptions;
}
Functions
Name | Description |
---|---|
form_styler_admin_form | Administration page callbacks for the form_styler module. |
form_styler_form_alter | Implements hook_form_alter(). |
form_styler_form_system_theme_settings_alter | Implements hook_form_FORM_ID_alter(). |
form_styler_get_default_options | Implements hook_get_default_options(). |
form_styler_libraries_info | Implements hook_libraries_info(). |
form_styler_menu | Implements hook_menu(). |
form_styler_permission | Implements hook_permission(). |
form_styler_plugin_loaded | Check if the backstretch libraries have been loaded. |