Index: /trunk/sites/all/modules/views/views.module =================================================================== --- /trunk/sites/all/modules/views/views.module (revision 583) +++ /trunk/sites/all/modules/views/views.module (revision 652) @@ -2069,2 +2069,46 @@ return $values; } + + +/** + * Limit the number of words in a given string. + * + * This function limits text to a provided number of words, and strips out + * any HTML and PHP tags. Code originally taken from + * http://php.oregonstate.edu/manual/en/function.str-word-count.php#75728. + * + * @param $string + * The string to limit. + * @param $limit + * The maximum number of words. + * @param $end + * A string to tack onto the end of the trimmed string. + * @param $strip + * TRUE if stripping out HTML/PHP tags is desired. You most likely + * want this, or else you may get unclosed and/or broken tags. + * @return + * The trimmed string. + */ +function views_word_limiter($string, $limit = 100, $end = ' ...', $strip = TRUE) { + + // Strip out any HTML/PHP tags. + if ($strip == TRUE) { + $string = strip_tags($string); + } + + // If there are no words, simply return the string. + if (trim($string) == '') { + return $string; + } + + // Perform the matching. + preg_match('/^\s*(?:\S+\s*){1,'. (int) $limit .'}/', $string, $matches); + + // Don't add an ending if the string is short enough. + if (strlen($matches[0]) == strlen($string)) { + $end = ''; + } + + // Return the possibly shortened version with the ending. + return rtrim($matches[0]) . $end; +} Index: /trunk/sites/all/modules/views/modules/views_node.inc =================================================================== --- /trunk/sites/all/modules/views/modules/views_node.inc (revision 296) +++ /trunk/sites/all/modules/views/modules/views_node.inc (revision 652) @@ -15,5 +15,7 @@ 'handler' => array( 'views_handler_field_nodelink' => t('Normal'), - 'views_handler_field_nodelink_with_mark' => t('With updated mark') + 'views_handler_field_nodelink_with_mark' => t('With updated mark'), + 'views_handler_field_nodelink_trim' => t('Trimmed'), + 'views_handler_field_nodelink_with_mark_trim' => t('Trimmed With updated mark') ), 'option' => array( @@ -69,5 +71,7 @@ 'handler' => array( 'views_handler_field_body' => t('Full Text'), - 'views_handler_field_teaser' => t('Teaser') + 'views_handler_field_body_trim' => t('Trimmed Full Text'), + 'views_handler_field_teaser' => t('Teaser'), + 'views_handler_field_teaser_trim' => t('Trimmed Teaser') ), 'addlfields' => array('nid'), @@ -426,4 +430,48 @@ /* + * Format a field as a link to a node. + */ +function views_handler_field_nodelink_trim($fieldinfo, $fielddata, $value, $data) { + if (abs(intval(variable_get('views_admin_settings_node_title_length', 0))) > 0) { + $value = views_word_limiter( + $value, + abs(intval(variable_get('views_admin_settings_node_title_length', 0))), + variable_get('views_admin_settings_node_title_more', '...'), + variable_get('views_admin_settings_node_title_strip', FALSE) + ); + } + + if ($fielddata['options'] == 'nolink') { + return check_plain($value); + } + + return l($value, "node/$data->nid"); +} + +/* + * Format a field as a link to a 'mark', stating whether or not the node has + * updated since it was last viewed by the user. + */ +function views_handler_field_nodelink_with_mark_trim($fieldinfo, $fielddata, $value, $data) { + if (abs(intval(variable_get('views_admin_settings_node_title_length', 0))) > 0) { + $value = views_word_limiter( + $value, + abs(intval(variable_get('views_admin_settings_node_title_length', 0))), + variable_get('views_admin_settings_node_title_more', '...'), + variable_get('views_admin_settings_node_title_strip', FALSE) + ); + } + + if ($fielddata['options'] == 'nolink') { + $link = check_plain($value); + } + else { + $link = l($value, "node/$data->nid"); + } + return $link .' '. theme('mark', node_mark($data->nid, $data->node_changed)); +} + + +/* * Format a field as a node type. */ @@ -466,4 +514,58 @@ function views_handler_field_teaser($fieldinfo, $fielddata, $value, $data) { return views_handler_field_body($fieldinfo, $fielddata, $value, $data); +} + +/* + * Format a field as the Body of a node. + */ +function views_handler_field_body_trim($fieldinfo, $fielddata, $value, $data) { + $node = node_load($data->nid); + + if ($fielddata['handler'] == 'views_handler_field_body') { + $teaser = FALSE; + } + else { + $teaser = TRUE; + } + + $node->body = str_replace('', '', $node->body); + + // The 'view' hook can be implemented to overwrite the default function + // to display nodes. + if (node_hook($node, 'view')) { + node_invoke($node, 'view', $teaser, TRUE); + } + else { + $node = node_prepare($node, $teaser); + } + // Allow modules to change $node->body before viewing. + node_invoke_nodeapi($node, 'view', $teaser, TRUE); + + if (abs(intval(variable_get('views_admin_settings_node_teaser_length', 0))) > 0) { + $node->teaser = views_word_limiter( + $node->teaser, + abs(intval(variable_get('views_admin_settings_node_teaser_length', 0))), + variable_get('views_admin_settings_node_teaser_more', '...'), + variable_get('views_admin_settings_node_teaser_strip', FALSE) + ); + } + + if (abs(intval(variable_get('views_admin_settings_node_body_length', 0))) > 0) { + $node->body = views_word_limiter( + $node->body, + abs(intval(variable_get('views_admin_settings_node_body_length', 0))), + variable_get('views_admin_settings_node_body_more', '...'), + variable_get('views_admin_settings_node_body_strip', FALSE) + ); + } + + return $teaser ? $node->teaser : $node->body; +} + +/* + * Format a field as the Teaser of a node. + */ +function views_handler_field_teaser_trim($fieldinfo, $fielddata, $value, $data) { + return views_handler_field_body_trim($fieldinfo, $fielddata, $value, $data); } Index: /trunk/sites/all/modules/views/views_ui.module =================================================================== --- /trunk/sites/all/modules/views/views_ui.module (revision 296) +++ /trunk/sites/all/modules/views/views_ui.module (revision 652) @@ -121,4 +121,10 @@ 'access' => user_access('administer views'), 'type' => MENU_LOCAL_TASK); + $items[] = array('path' => 'admin/build/views/settings', + 'title' => t('Settings'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('views_ui_admin_settings'), + 'access' => user_access('administer views'), + 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'admin/build/views/delete', 'title' => t('Edit view'), @@ -335,4 +341,180 @@ } +/** + * Views UI Settings + * + * @author William Roboly + */ +function views_ui_admin_settings() { + + // Create the Form. + $form['node_title'] = array( + '#type' => 'fieldset', + '#title' => t('Node Title'), + '#collapsible' => TRUE, + ); + + $form['node_title']['views_admin_settings_node_title_type'] = array( + '#type' => 'radios', + '#title' => t('Type of Trim Action'), + '#default_value' => variable_get('views_admin_settings_node_title_type', 0), + '#options' => array(t('Word based'), t('Character based')), + '#description' => t('Trim the length of the node title field either by words or characters.'), + ); + + $form['node_title']['views_admin_settings_node_title_length'] = array( + '#type' => 'textfield', + '#title' => t('Trim length'), + '#default_value' => variable_get('views_admin_settings_node_title_length', 0), + '#size' => 4, + '#maxlength' => 4, + '#description' => t('The length you wish to trim the field by. Leave 0 if you wish to keep it the way it is. Anything other than a whole number will be converted to 0.

n.b.:The absolute value of a negative will be used.

'), + ); + + $form['node_title']['views_admin_settings_node_title_strip'] = array( + '#type' => 'checkbox', + '#title' => t('Strip HTML from field.'), + '#return_value' => 1, + '#default_value' => variable_get('views_admin_settings_node_title_strip', 0), + ); + + $form['node_title']['views_admin_settings_node_title_more'] = array( + '#type' => 'textfield', + '#title' => t('Appendice'), + '#default_value' => variable_get('views_admin_settings_node_title_more', '...'), + '#size' => 40, + '#description' => t('Whatever you place here will be appended to the field trimmed. You may add an ellipsis or a word, a set of characters, a link.'), + ); + + + $form['node_teaser'] = array( + '#type' => 'fieldset', + '#title' => t('Node Teaser'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form['node_teaser']['views_admin_settings_node_teaser_type'] = array( + '#type' => 'radios', + '#title' => t('Type of Trim Action'), + '#default_value' => variable_get('views_admin_settings_node_teaser_type', 0), + '#options' => array(t('Word based'), t('Character based')), + '#description' => t('Trim the length of the node teaser content field either by words or characters.

n.b.:These changes affect the field in question before application of a link.

'), + ); + + $form['node_teaser']['views_admin_settings_node_teaser_length'] = array( + '#type' => 'textfield', + '#title' => t('Trim length'), + '#default_value' => variable_get('views_admin_settings_node_teaser_length', 0), + '#size' => 4, + '#maxlength' => 4, + '#description' => t('The length you wish to trim the field by. Leave 0 if you wish to keep it the way it is. Anything other than a whole number will be converted to 0.

n.b.:The absolute value of a negative will be used.

'), + ); + + $form['node_teaser']['views_admin_settings_node_teaser_strip'] = array( + '#type' => 'checkbox', + '#title' => t('Strip HTML from field.'), + '#return_value' => 1, + '#default_value' => variable_get('views_admin_settings_node_teaser_strip', 0), + ); + + $form['node_teaser']['views_admin_settings_node_teaser_more'] = array( + '#type' => 'textfield', + '#title' => t('Appendice'), + '#default_value' => variable_get('views_admin_settings_node_teaser_more', '...'), + '#size' => 40, + '#description' => t('Whatever you place here will be appended to the field trimmed. You may add an ellipsis or a word, a set of characters, a link.'), + ); + + $form['node_body'] = array( + '#type' => 'fieldset', + '#title' => t('Node Body'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form['node_body']['views_admin_settings_node_body_type'] = array( + '#type' => 'radios', + '#title' => t('Type of Trim Action'), + '#default_value' => variable_get('views_admin_settings_node_body_type', 0), + '#options' => array(t('Word based'), t('Character based')), + '#description' => t('Trim the length of the node body content field either by words or characters.

n.b.:These changes affect the field in question before application of a link.

'), + ); + + $form['node_body']['views_admin_settings_node_body_length'] = array( + '#type' => 'textfield', + '#title' => t('Trim length'), + '#default_value' => variable_get('views_admin_settings_node_body_length', 0), + '#size' => 4, + '#maxlength' => 4, + '#description' => t('The length you wish to trim the field by. Leave 0 if you wish to keep it the way it is. Anything other than a whole number will be converted to 0.

n.b.:The absolute value of a negative will be used.

'), + ); + + $form['node_body']['views_admin_settings_node_body_strip'] = array( + '#type' => 'checkbox', + '#title' => t('Strip HTML from field.'), + '#return_value' => 1, + '#default_value' => variable_get('views_admin_settings_node_body_strip', 0), + ); + + $form['node_body']['views_admin_settings_node_body_more'] = array( + '#type' => 'textfield', + '#title' => t('Appendice'), + '#default_value' => variable_get('views_admin_settings_node_body_more', '...'), + '#size' => 40, + '#description' => t('Whatever you place here will be appended to the field trimmed. You may add an ellipsis or a word, a set of characters, a link.'), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t("Save Options"), + ); + + $form['#redirect'] = NULL; + return $form; +} + +function views_ui_admin_settings_validate($form_id, $form_values) { + if (!is_numeric($form_values['views_admin_settings_node_body_length'])) { + form_set_error('views_admin_settings_node_body_length', t('The length provided is not numeric')); + } + + if (!is_numeric($form_values['views_admin_settings_node_teaser_length'])) { + form_set_error('views_admin_settings_node_teaser_length', t('The length provided is not numeric')); + } + + if (!is_numeric($form_values['views_admin_settings_node_title_length'])) { + form_set_error('views_admin_settings_node_title_length', t('The length provided is not numeric')); + } + +} + +function views_ui_admin_settings_submit($form_id, $form_values) { + $op = isset($form_values['op']) ? $form_values['op'] : ''; + + // Exclude unnecessary elements. + unset($form_values['submit'], $form_values['reset'], $form_values['form_id'], $form_values['op'], $form_values['form_token']); + + foreach ($form_values as $key => $value) { + if ($op == t('Reset to defaults')) { + variable_del($key); + } + else { + if (is_array($value) && isset($form_values['array_filter'])) { + $value = array_keys(array_filter($value)); + } + variable_set($key, $value); + } + } + + if ($op == t('Reset to defaults')) { + drupal_set_message(t('The configuration options have been reset to their default values.')); + } + else { + drupal_set_message(t('The configuration options have been saved.')); + } + +} + /* * Page to enable a disabled default view