View source
<?php
define('SOFT_LENGTH_LIMIT_TITLE_MAX', 'soft_length_limit_title_max');
define('SOFT_LENGTH_LIMIT_TITLE_MIN', 'soft_length_limit_title_min');
define('SOFT_LENGTH_STYLE_SELECT', 'soft_length_style_select');
function _soft_length_limit_types($usage) {
$return = array();
switch ($usage) {
case 'fields':
$return = array(
'text_textarea' => 'text_textarea',
'text_textfield' => 'text_textfield',
'text_textarea_with_summary' => 'text_textarea_with_summary',
);
break;
case 'elements':
$return = array(
'textarea' => 'textarea',
'textfield' => 'textfield',
'text_format' => 'text_format',
);
break;
case 'entity_types':
$return = array(
'node' => 'node',
);
break;
}
return $return;
}
function soft_length_limit_form_node_type_form_alter(&$form, &$form_state, $form_id) {
$form['submission'][SOFT_LENGTH_LIMIT_TITLE_MAX] = array(
'#type' => 'textfield',
'#title' => t('Soft length limit'),
'#default_value' => variable_get(SOFT_LENGTH_LIMIT_TITLE_MAX . '_' . $form['#node_type']->type, NULL),
'#description' => t('If any value is given here, a counter will appear next to this field, informing the user of the chosen number of allowed characters. If the number is exceeded, a warning will be shown.'),
'#element_validate' => array(
'element_validate_integer_positive',
),
'#weight' => -2,
);
$form['submission'][SOFT_LENGTH_LIMIT_TITLE_MIN] = array(
'#type' => 'textfield',
'#title' => t('Soft length minimum'),
'#default_value' => variable_get(SOFT_LENGTH_LIMIT_TITLE_MIN . '_' . $form['#node_type']->type, NULL),
'#description' => t('If any value is given here, the minimum number recommended characters will be displayed as the editor enters text in this field.'),
'#element_validate' => array(
'element_validate_integer_positive',
),
'#weight' => -1,
);
$form['submission'][SOFT_LENGTH_STYLE_SELECT] = array(
'#type' => 'checkbox',
'#title' => t('Enable enhanced view'),
'#default_value' => variable_get(SOFT_LENGTH_STYLE_SELECT . '_' . $form['#node_type']->type, NULL),
'#description' => t('Check this to enable an enhanced view of soft length states.'),
'#weight' => -1,
);
$form['submission']['title_label']['#weight'] = -3;
$form['#validate'][] = 'soft_length_limit_validate_title_length';
$form['#submit'][] = 'soft_length_limit_set_title_maxlength';
}
function soft_length_limit_validate_title_length($form, &$form_state) {
if ($form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MAX] < 0 || $form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MAX] > 255) {
form_set_error('title_length', t('The value for soft length limit must be a whole number greater than zero and less than 256'), NULL);
}
if ($form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MIN] < 0 || $form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MIN] > 255) {
form_set_error('soft_length_minimum', t('The value for soft length minimum must be a whole number greater than zero and less than 256'));
}
if ($form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MAX] < $form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MIN]) {
form_set_error('soft_length_minimum', t('The value for soft length minimum must be less than or equal to the soft length limit'));
}
}
function soft_length_limit_set_title_maxlength($form, &$form_state) {
variable_set(SOFT_LENGTH_LIMIT_TITLE_MAX . '_' . $form_state['values']['type'], $form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MAX]);
variable_set(SOFT_LENGTH_LIMIT_TITLE_MIN . '_' . $form_state['values']['type'], $form_state['values'][SOFT_LENGTH_LIMIT_TITLE_MIN]);
variable_set(SOFT_LENGTH_STYLE_SELECT . '_' . $form_state['values']['type'], $form_state['values'][SOFT_LENGTH_STYLE_SELECT]);
}
function soft_length_limit_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
$types = _soft_length_limit_types('fields');
if (isset($types[$form['#instance']['widget']['type']])) {
$form['instance']['widget']['settings']['soft_length_limit'] = array(
'#type' => 'textfield',
'#title' => t('Soft length limit'),
'#default_value' => isset($form['#instance']['widget']['settings']['soft_length_limit']) ? $form['#instance']['widget']['settings']['soft_length_limit'] : NULL,
'#description' => t('If any value is given here, a counter will appear next to this field, informing the user of the chosen number of allowed characters. If the number is exceeded, a warning will be shown.'),
'#element_validate' => array(
'element_validate_integer_positive',
),
'#weight' => -3,
);
$form['instance']['widget']['settings']['soft_length_minimum'] = array(
'#type' => 'textfield',
'#title' => t('Soft length minimum'),
'#default_value' => isset($form['#instance']['widget']['settings']['soft_length_minimum']) ? $form['#instance']['widget']['settings']['soft_length_minimum'] : NULL,
'#description' => t('If any value is given here, the minimum number recommended characters will be displayed as the editor enters text in this field.'),
'#element_validate' => array(
'element_validate_integer_positive',
),
'#weight' => -2,
);
$form['instance']['widget']['settings']['soft_length_style_select'] = array(
'#type' => 'checkbox',
'#title' => t('Enable enhanced view'),
'#default_value' => isset($form['#instance']['widget']['settings']['soft_length_style_select']) ? $form['#instance']['widget']['settings']['soft_length_style_select'] : 0,
'#description' => t('Check this to enable an enhanced view of soft length states.'),
'#weight' => -1,
);
}
}
function soft_length_limit_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
$entity_types = _soft_length_limit_types('entity_types');
if (!isset($entity_types[$entity_type])) {
return;
}
$fields = field_info_instances($entity_type, $form['#bundle']);
$elements = array();
foreach ($fields as $key => $value) {
if (isset($value['widget']['settings']['soft_length_limit']) && $value['widget']['settings']['soft_length_limit'] > 0) {
$elements[$key] = $value;
}
}
if (count($elements) || isset($form['title'])) {
soft_length_limit_set_attr($form, $elements);
$form['#attached']['js'][] = drupal_get_path('module', 'soft_length_limit') . '/jquery.textchange.min.js';
$form['#attached']['js'][] = drupal_get_path('module', 'soft_length_limit') . '/soft_length_limit.js';
$form['#attached']['css'][] = drupal_get_path('module', 'soft_length_limit') . '/soft_length_limit.css';
}
}
function soft_length_limit_set_attr(&$element, $sub_elements) {
$children = element_get_visible_children($element);
$types = _soft_length_limit_types('elements');
foreach ($children as $value) {
if (isset($element[$value]['#type']) && isset($types[$element[$value]['#type']])) {
if (isset($element[$value]['#field_name']) && isset($sub_elements[$element[$value]['#field_name']])) {
$widget_settings = isset($sub_elements[$element[$value]['#field_name']]['widget']['settings']) ? $sub_elements[$element[$value]['#field_name']]['widget']['settings'] : FALSE;
if ($widget_settings) {
$soft_limit = $widget_settings['soft_length_limit'];
$element[$value]['#soft_length_limit'] = isset($element[$value]['#maxlength']) && $soft_limit > $element[$value]['#maxlength'] ? $element[$value]['#maxlength'] : $soft_limit;
$element[$value]['#attributes']['data-soft-length-limit'] = $soft_limit;
$element[$value]['#attributes']['class'][] = 'soft-length-limit';
$soft_min = isset($widget_settings['soft_length_minimum']) ? $widget_settings['soft_length_minimum'] : '';
$element[$value]['#attributes']['data-soft-length-minimum'] = $soft_min;
if (isset($widget_settings['soft_length_style_select'])) {
$element[$value]['#attributes']['data-soft-length-style-select'] = $widget_settings['soft_length_style_select'];
}
}
}
}
soft_length_limit_set_attr($element[$value], $sub_elements);
}
}
function soft_length_limit_form_node_form_alter(&$form, $form_state) {
$type = $form['#node']->type;
$max_length = variable_get(SOFT_LENGTH_LIMIT_TITLE_MAX . '_' . $type, NULL);
$min_length = variable_get(SOFT_LENGTH_LIMIT_TITLE_MIN . '_' . $type, NULL);
if (!$min_length) {
$min_length = 0;
}
$style_select = variable_get(SOFT_LENGTH_STYLE_SELECT . '_' . $type, NULL);
if (isset($form['title']) && (!empty($max_length) || !empty($min_length))) {
$form['title']['#attributes']['class'][] = 'soft-length-limit';
$form['title']['#attributes']['data-soft-length-limit'] = $max_length;
$form['title']['#attributes']['data-soft-length-minimum'] = $min_length;
$form['#attached']['js'][] = drupal_get_path('module', 'soft_length_limit') . '/jquery.textchange.min.js';
$form['#attached']['js'][] = drupal_get_path('module', 'soft_length_limit') . '/soft_length_limit.js';
$form['#attached']['css'][] = drupal_get_path('module', 'soft_length_limit') . '/soft_length_limit.css';
$form['title']['#attributes']['data-soft-length-style-select'] = $style_select;
}
}