diff --git a/maxlength.module b/maxlength.module index 8342e3c..c1e74c3 100644 --- a/maxlength.module +++ b/maxlength.module @@ -33,13 +33,27 @@ function maxlength_help($path, $arg) { function maxlength_form_alter(&$form, &$form_state, $form_id) { // Editing the content if ($form['#id'] == 'node-form') { - _maxlength_content_form_alter($form, $form_state, $form_id); + $type = $form['type']['#value']; + _maxlength_format_element($form['title'], $form['title']['#default_value'], 'title', 'title', $type); + _maxlength_format_element($form['body_field']['body'], $form['body_field']['body']['#default_value'], 'body', 'body', $type); + _maxlength_format_cck_elements($form, $form_state, $type); + } + // Editing the comment + elseif ($form['#id'] == 'comment-form') { + $node = node_load($form['nid']['#value']); + $type=$node->type; + // Add a max length formatter to the comment subject and body + _maxlength_format_element($form['subject'], $form['subject']['#default_value'], 'subject', 'subject', $type); + _maxlength_format_element($form['comment_filter']['comment'], $form['comment_filter']['comment']['#default_value'], 'comment', 'comment', $type); } // Editing the content type elseif ($form_id == 'node_type_form' && isset($form['identity']['type'])) { _maxlength_content_type_form_alter($form, $form_state, $form_id); } - + // Editing a Comment text field. +// elseif ($form_id['#id'] == 'node-type-form' && $form['post']['op'] != 'Save content type') { +// _maxlength_content_type_form_alter($form, $form_state, $form_id); +// } // Editing a CCK text field. elseif ($form_id == 'content_field_edit_form' AND isset($form['field']['max_length'])) { _maxlength_cck_form_alter($form, $form_state, $form_id); @@ -53,15 +67,10 @@ function maxlength_form_alter(&$form, &$form_state, $form_id) { } } -function _maxlength_content_form_alter(&$form, &$form_state, $form_id) { - $type = $form['type']['#value']; - // update the title as needed - _maxlength_format_element($form['title'], $form['title']['#default_value'], 'title', 'title', $type); - - // Update the body as needed - _maxlength_format_element($form['body_field']['body'], $form['body_field']['body']['#default_value'], 'body', 'body', $type); - - +function _maxlength_format_cck_elements(&$form, &$form_state, $type) { + // It has been determined that we are editing the content of a node + // add all maxlength formatters to all the cck fields + if (module_exists('content')) { // Get a list of all the CCK fields for this content type $list = array_keys(content_fields(NULL, $type)); @@ -78,23 +87,41 @@ function _maxlength_content_form_alter(&$form, &$form_state, $form_id) { } function _maxlength_content_type_form_alter(&$form, &$form_state, $form_id) { + /* Editing a Content Type. Provide the fields for the user to choose maxlength settings on: + * Title + * Body + * Comment Title + * Comment Body + */ $type = $form['#node_type']->type; - $labels = array('-3' => 'title', '-1 ' => 'body'); - - foreach ($labels as $weight => $label) { - - // bit of a hack to allow us to position the input fields correctly - $form['submission'][$label .'_label']['#weight'] = $weight - 1; - - $form['submission'][MAXLENGTH_NODE_TYPE . $label] = array( + + $labels = array( + 'title' => array('parent_form' => 'submission', 'weight' => '-3'), + 'body' => array('parent_form' => 'submission', 'weight' => '-1'), + 'subject' => array('parent_form' => 'comment', 'weight' => '-1'), + 'comment' => array('parent_form' => 'comment', 'weight' => '0'), + ); + + foreach ($labels as $label => $options) { + $parent_form = $options['parent_form']; + $weight = $options['weight']; + + if ($parent_form == 'submission') + { + // Ensure that the "limit title length" / "limit body length" fieldssets we create + // are placed directly beneath the "title field label" / "body field label" form elements + $form[$parent_form][$label .'_label']['#weight'] = $weight - 1; + } + + $form[$parent_form][MAXLENGTH_NODE_TYPE . $label] = array( '#type' => 'fieldset', '#weight' => $weight, - '#title' => t('Limit !type length', array('!type ' => $label)), + '#title' => t('Limit !type length', array('!type ' => $label)), '#collapsible' => TRUE, '#collapsed' => strlen(variable_get($type .'_'. $label, '')) == 0, ); - $form['submission'][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label] = array( + $form[$parent_form][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label] = array( '#type' => 'textfield', '#title' => t('!label max length', array('!label' => ucwords($label))), '#field_suffix' => t('characters'), @@ -104,13 +131,13 @@ function _maxlength_content_type_form_alter(&$form, &$form_state, $form_id) { '#description' => t('Maximum number of characters allowed for the !type field of this content type. Leave blank for an unlimited size.', array('!type' => $label)) .'
'. ''. t('Please remember, it counts all characters, including HTML, so may not work as expected with rich text editors e.g. FCKeditor / tinyMCE.') .'', ); - $form['submission'][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label .'_js'] = array( + $form[$parent_form][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label .'_js'] = array( '#type' => 'checkbox', '#title' => t('Enable remaining characters countdown for the !label', array('!label' => ucwords($label))), '#default_value' => variable_get(MAXLENGTH_NODE_TYPE . $label .'_js_'. $type, '0'), '#description' => t('This will enable a Javascript based count down, as well as the client side validation for the !type field of this content type. If no limit set this is ignored.', array('!type' => $label)), ); - $form['submission'][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label .'_text'] = array( + $form[$parent_form][MAXLENGTH_NODE_TYPE . $label][MAXLENGTH_NODE_TYPE . $label .'_text'] = array( '#type' => 'textarea', '#title' => t('!label count down message', array('!label' => ucwords($label))), '#default_value' => variable_get(MAXLENGTH_NODE_TYPE . $label .'_text_'. $type, 'Content limited to !limit characters, remaining: !remaining'), @@ -173,6 +200,7 @@ function _maxlength_cck_form_submit($form, &$form_state) { * Can we use a theme function to let alter the placement of the maxlength text. */ function _maxlength_format_element(&$element, $value = '', $field, $id, $type = '') { + $values = _maxlength_get_values($field, $type); if ($values !== FALSE AND isset($values['limit']) AND $values['limit'] AND $values['use_js']) { @@ -215,7 +243,7 @@ function _maxlength_get_values($field = 'body', $type = '') { $values['limit'] = variable_get(MAXLENGTH_NODE_TYPE . $field, FALSE); $values['use_js'] = variable_get(MAXLENGTH_NODE_TYPE . $field .'_js', FALSE); $values['text'] = variable_get(MAXLENGTH_NODE_TYPE . $field .'_text', FALSE); - } //body and title + } //body and title and comment elseif ($type != '') { $values['limit'] = variable_get(MAXLENGTH_NODE_TYPE . $field .'_'. $type, FALSE); $values['use_js'] = variable_get(MAXLENGTH_NODE_TYPE . $field .'_js_'. $type, FALSE); @@ -238,6 +266,8 @@ function _maxlength_get_values($field = 'body', $type = '') { */ function maxlength_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { $fields = array('title', 'body'); + + // TODO: add equivalent validation for when comments are submitted. They are not currently checked foreach ($fields as $field) { $limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $field .'_'. $node->type, '')); @@ -259,6 +289,37 @@ function maxlength_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { } } + +function maxlength_comment(&$comment_values, $op) { + switch ($op) { + case 'validate' : + // Raise errors if the comment subject or body exceeds their maxlength + $fields = array('subject', 'comment'); + $node = node_load($comment_values['nid']); + foreach ($fields as $field) { + $limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $field .'_'. $node->type, '')); + if ($limit > 0) { + if (drupal_strlen($comment_values[$field]) > $limit) { + form_set_error($field, t('The !field field has exceeded its maximum number of characters (!limit).', array('!limit' => $limit, '!field' => $field))); + } + } + } + break; + case 'insert' : + // the auto-population of the subject line may have made it exceed the maximum length. + // If so, update the comment with a truncated subject + $node = node_load($comment_values['nid']); + $field = 'subject'; + $limit = intval(variable_get(MAXLENGTH_NODE_TYPE . $field .'_'. $node->type, '')); + if ($limit > 0) { + if (drupal_strlen($comment_values[$field]) > $limit) { + $comment_values[$field] = drupal_substr($comment_values[$field], 0, $limit); + comment_save($comment_values); + } + } + break; + } +} /** * Implementation of hook_node_type(). */ @@ -287,3 +348,4 @@ function maxlength_node_type($op, $info) { } } +