summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2010-08-08 13:02:37 +0000
committerDries Buytaert <dries@buytaert.net>2010-08-08 13:02:37 +0000
commitf0b3fa4b1bb0b8c91424c3ad36dfef592ccbc954 (patch)
tree84679f95714d6b04ee10442ffc255c4b5bd1b4d2
parentf885d9f7dca9242b7906d6d441cae5feb2aba5b1 (diff)
downloadbrdo-f0b3fa4b1bb0b8c91424c3ad36dfef592ccbc954.tar.gz
brdo-f0b3fa4b1bb0b8c91424c3ad36dfef592ccbc954.tar.bz2
- Patch #504564 by tic2000, andypost, catch: the 'Length of trimmed content'
form input in the 'node type' edit form was way too harsh (affects the display of all 'trimmed / summary_or_trimmed' formatters on all text fields in all view modes), and irrelevant for text fields on non-node entities, which are currently all blocked to 'trim to 600 chars'. * Added field formatter settings for 'text_trimmed' and 'text_summary_or_trimmed' formatters * Removed 'teaser_length' settings for node types * Added upgrade path for 'teaser_length' variable in node_update_7011() * Fixed a test
-rw-r--r--modules/field/modules/text/text.module46
-rw-r--r--modules/node/content_types.inc8
-rw-r--r--modules/node/node.install20
-rw-r--r--modules/node/node.module6
-rw-r--r--modules/node/node.test12
5 files changed, 73 insertions, 19 deletions
diff --git a/modules/field/modules/text/text.module b/modules/field/modules/text/text.module
index d08b311ce..5862ae920 100644
--- a/modules/field/modules/text/text.module
+++ b/modules/field/modules/text/text.module
@@ -251,6 +251,7 @@ function text_field_formatter_info() {
'text_trimmed' => array(
'label' => t('Trimmed'),
'field types' => array('text', 'text_long', 'text_with_summary'),
+ 'settings' => array('trim_length' => 600),
),
// The 'summary or trimmed' field formatter for text_with_summary
@@ -260,11 +261,51 @@ function text_field_formatter_info() {
'text_summary_or_trimmed' => array(
'label' => t('Summary or trimmed'),
'field types' => array('text_with_summary'),
+ 'settings' => array('trim_length' => 600),
),
);
}
/**
+ * Implements hook_field_formatter_settings_form().
+ */
+function text_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
+ $display = $instance['display'][$view_mode];
+ $settings = $display['settings'];
+
+ $form = array();
+
+ if (strpos($display['type'], '_trimmed') !== FALSE) {
+ $form['trim_length'] = array(
+ '#title' => t('Trim length'),
+ '#type' => 'textfield',
+ '#size' => 10,
+ '#default_value' => $settings['trim_length'],
+ '#element_validate' => array('_element_validate_integer_positive'),
+ '#required' => TRUE,
+ );
+ }
+
+ return $form;
+}
+
+/**
+ * Implements hook_field_formatter_settings_summary().
+ */
+function text_field_formatter_settings_summary($field, $instance, $view_mode) {
+ $display = $instance['display'][$view_mode];
+ $settings = $display['settings'];
+
+ $summary = '';
+
+ if (strpos($display['type'], '_trimmed') !== FALSE) {
+ $summary = t('Trim length') . ': ' . $settings['trim_length'];
+ }
+
+ return $summary;
+}
+
+/**
* Implements hook_field_formatter_view().
*/
function text_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
@@ -276,7 +317,7 @@ function text_field_formatter_view($entity_type, $entity, $field, $instance, $la
foreach ($items as $delta => $item) {
$output = _text_sanitize($instance, $langcode, $item, 'value');
if ($display['type'] == 'text_trimmed') {
- $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL);
+ $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
}
$element[$delta] = array('#markup' => $output);
}
@@ -289,8 +330,7 @@ function text_field_formatter_view($entity_type, $entity, $field, $instance, $la
}
else {
$output = _text_sanitize($instance, $langcode, $item, 'value');
- $size = variable_get('teaser_length_' . $instance['bundle'], 600);
- $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $size);
+ $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
}
$element[$delta] = array('#markup' => $output);
}
diff --git a/modules/node/content_types.inc b/modules/node/content_types.inc
index e094ff3a9..aed87edf3 100644
--- a/modules/node/content_types.inc
+++ b/modules/node/content_types.inc
@@ -207,13 +207,6 @@ function node_type_form($form, &$form_state, $type = NULL) {
'#default_value' => variable_get('node_submitted_' . $type->type, TRUE),
'#description' => t('Author username and publish date will be displayed.'),
);
- $form['display']['teaser_length'] = array(
- '#type' => 'select',
- '#title' => t('Length of trimmed content'),
- '#default_value' => variable_get('teaser_length_' . $type->type, 600),
- '#options' => drupal_map_assoc(array(0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000), '_node_characters'),
- '#description' => t("The maximum number of characters used in the trimmed version of content.")
- );
$form['old_type'] = array(
'#type' => 'value',
'#value' => $type->type,
@@ -451,7 +444,6 @@ function node_type_delete_confirm($form, &$form_state, $type) {
function node_type_delete_confirm_submit($form, &$form_state) {
node_type_delete($form_state['values']['type']);
- variable_del('teaser_length_' . $form_state['values']['type']);
variable_del('node_preview_' . $form_state['values']['type']);
$t_args = array('%name' => $form_state['values']['name']);
drupal_set_message(t('The content type %name has been deleted.', $t_args));
diff --git a/modules/node/node.install b/modules/node/node.install
index b5e10d6b0..cc3d8ee97 100644
--- a/modules/node/node.install
+++ b/modules/node/node.install
@@ -496,11 +496,29 @@ function node_update_7006(&$sandbox) {
->fields('node_type')
->execute();
+ $default_trim_length = variable_get('teaser_length', 600);
// Add body field instances for existing node types.
foreach ($result as $node_type) {
if ($node_type->has_body) {
- node_add_body_field($node_type, $node_type->body_label);
+ $instance = node_add_body_field($node_type, $node_type->body_label);
+ // Update newly created instance to convert teaser_length variable
+ // into formatter settings.
+ $trim_length = variable_get('teaser_length_' . $node_type->type, $default_trim_length);
+ $instance_changed = FALSE;
+ foreach ($instance['display'] as $view_mode => $view_mode_info) {
+ if ($view_mode_info['type'] == 'text_trimmed' || $view_mode_info['type'] == 'text_summary_or_trimmed') {
+ if (!isset($view_mode_info['settings']['trim_length'])) {
+ $instance['display'][$view_mode]['settings']['trim_length'] = $trim_length;
+ $instance_changed = TRUE;
+ }
+ }
+ }
+ if ($instance_changed) {
+ field_update_instance($instance);
+ }
+ variable_del('teaser_length_' . $node_type->type);
}
+ // Leave 'teaser_length' variable for aggregator module upgrade.
$sandbox['node_types_info'][$node_type->type] = array(
'has_body' => $node_type->has_body,
diff --git a/modules/node/node.module b/modules/node/node.module
index b0c3bb744..f32625b8e 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -555,6 +555,9 @@ function node_type_save($info) {
* A node type object.
* @param $label
* The label for the body instance.
+ *
+ * @return
+ * Body field instance.
*/
function node_add_body_field($type, $label = 'Body') {
// Add or remove the body field, as needed.
@@ -588,8 +591,9 @@ function node_add_body_field($type, $label = 'Body') {
),
),
);
- field_create_instance($instance);
+ $instance = field_create_instance($instance);
}
+ return $instance;
}
/**
diff --git a/modules/node/node.test b/modules/node/node.test
index f5d3aefe2..028275b3e 100644
--- a/modules/node/node.test
+++ b/modules/node/node.test
@@ -565,11 +565,11 @@ class SummaryLengthTestCase extends DrupalWebTestCase {
$expected = 'What is a Drupalism?';
$this->assertRaw($expected, t('Check that the summary is 600 characters in length'), 'Node');
- // Edit the teaser length for "Basic page" content type
- $edit = array (
- 'teaser_length' => 200,
- );
- $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
+ // Change the teaser length for "Basic page" content type.
+ $instance = field_info_instance('node', 'body', $node->type);
+ $instance['display']['teaser']['settings']['trim_length'] = 200;
+ field_update_instance($instance);
+
// Attempt to access the front page again and check if the summary is now only 200 characters in length.
$this->drupalGet("node");
$this->assertNoRaw($expected, t('Check that the summary is not longer than 200 characters'), 'Node');
@@ -1072,7 +1072,7 @@ class NodeTypeTestCase extends DrupalWebTestCase {
$this->drupalGet('node/add/' . str_replace('_', '-', $type->name));
$this->assertResponse(200, 'The new content type can be accessed at node/add.');
-
+
// Create a content type via the user interface.
$web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types'));
$this->drupalLogin($web_user);