### Eclipse Workspace Patch 1.0
#P drupal_test_7
Index: modules/node/node.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.pages.inc,v
retrieving revision 1.58
diff -u -r1.58 node.pages.inc
--- modules/node/node.pages.inc 26 Mar 2009 13:31:25 -0000 1.58
+++ modules/node/node.pages.inc 26 Mar 2009 19:04:23 -0000
@@ -112,7 +112,7 @@
$form['#prefix'] = $form_state['node_preview'];
}
$node = (object)$node;
- foreach (array('body', 'title', 'format') as $key) {
+ foreach (array('title') as $key) {
if (!isset($node->$key)) {
$node->$key = NULL;
}
@@ -273,48 +273,6 @@
}
/**
- * Return a node body field, with format and teaser.
- */
-function node_body_field(&$node, $label, $word_count) {
-
- // Check if we need to restore the teaser at the beginning of the body.
- $include = !isset($node->teaser) || ($node->teaser == substr($node->body, 0, strlen($node->teaser)));
-
- $form = array(
- '#after_build' => array('node_teaser_js', 'node_teaser_include_verify'));
-
- $form['#prefix'] = '
';
- $form['#suffix'] = '
';
-
- $form['teaser_js'] = array(
- '#type' => 'textarea',
- '#rows' => 10,
- '#teaser' => 'edit-body',
- '#teaser_checkbox' => 'edit-teaser-include',
- '#disabled' => TRUE,
- );
-
- $form['teaser_include'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show summary in full view'),
- '#default_value' => $include,
- '#prefix' => '',
- '#suffix' => '
',
- );
-
- $form['body'] = array(
- '#type' => 'textarea',
- '#title' => check_plain($label),
- '#default_value' => $include ? $node->body : ($node->teaser . $node->body),
- '#rows' => 20,
- '#required' => ($word_count > 0),
- '#text_format' => isset($node->format) ? $node->format : FILTER_FORMAT_DEFAULT,
- );
-
- return $form;
-}
-
-/**
* Button submit function: handle the 'Delete' button on the node form.
*/
function node_form_delete_submit($form, &$form_state) {
@@ -375,16 +333,6 @@
$node->changed = REQUEST_TIME;
- // Extract a teaser, if it hasn't been set (e.g. by a module-provided
- // 'teaser' form item).
- if (!isset($node->teaser)) {
- $node->teaser = empty($node->body) ? '' : node_teaser($node->body, $node->format);
- // Chop off the teaser from the body if needed.
- if (!$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
- $node->body = substr($node->body, strlen($node->teaser));
- }
- }
-
// Display a preview of the node.
// Previewing alters $node so it needs to be cloned.
if (!form_get_errors()) {
@@ -410,28 +358,20 @@
$output = '';
$preview_trimmed_version = FALSE;
- // Do we need to preview trimmed version of post as well as full version?
- if (isset($node->teaser) && isset($node->body)) {
- $teaser = trim($node->teaser);
- $body = trim(str_replace('', '', $node->body));
-
- // Preview trimmed version if teaser and body will appear different;
- // also (edge case) if both teaser and body have been specified by the user
- // and are actually the same.
- if ($teaser != $body || ($body && strpos($node->body, '') === 0)) {
- $preview_trimmed_version = TRUE;
- }
- }
- if ($preview_trimmed_version) {
+ $trimmed = drupal_render(node_build(clone $node, TRUE));
+ $full = drupal_render(node_build($node, FALSE));
+
+ // Do we need to preview trimmed version of post as well as full version?
+ if ($trimmed != $full) {
drupal_set_message(t('The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication. You can insert the delimiter "<!--break-->" (without the quotes) to fine-tune where your post gets split.'));
$output .= '
' . t('Preview trimmed version') . '
';
- $output .= drupal_render(node_build(clone $node, TRUE));
+ $output .= $trimmed;
$output .= '' . t('Preview full version') . '
';
- $output .= drupal_render(node_build($node, FALSE));
+ $output .= $full;
}
else {
- $output .= drupal_render(node_build($node, FALSE));
+ $output .= $full;
}
$output .= "\n";
Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.1033
diff -u -r1.1033 node.module
--- modules/node/node.module 26 Mar 2009 13:31:25 -0000 1.1033
+++ modules/node/node.module 26 Mar 2009 19:04:23 -0000
@@ -46,6 +46,11 @@
define('NODE_BUILD_PRINT', 5);
/**
+ * Name of the default body field.
+ */
+define('NODE_BODY_FIELD', 'node_body');
+
+/**
* Implementation of hook_help().
*/
function node_help($path, $arg) {
@@ -583,6 +588,7 @@
if (!empty($type->old_type) && $type->old_type != $type->type) {
field_attach_rename_bundle($type->old_type, $type->type);
}
+ node_configure_fields($type);
module_invoke_all('node_type', 'update', $type);
return SAVED_UPDATED;
}
@@ -591,13 +597,68 @@
db_insert('node_type')->fields($fields)->execute();
field_attach_create_bundle($type->type);
-
+ node_configure_fields($type);
module_invoke_all('node_type', 'insert', $type);
return SAVED_NEW;
}
}
/**
+ * Manage the field(s) for a node type.
+ */
+function node_configure_fields($type) {
+
+ // Add or remove the body field, as needed.
+ $field = field_info_field(NODE_BODY_FIELD);
+ $instance = field_info_instance(NODE_BODY_FIELD, $type->type);
+ if ($type->has_body) {
+ if (empty($field)) {
+ $field = array(
+ 'field_name' => NODE_BODY_FIELD,
+ 'type' => 'text_with_summary',
+ );
+ $field = field_create_field($field);
+ }
+ if (empty($instance)) {
+ $instance = array(
+ 'field_name' => NODE_BODY_FIELD,
+ 'bundle' => $type->type,
+ 'label' => $type->body_label,
+ 'widget_type' => 'text_textarea_with_summary',
+ 'settings' => array('display_summary' => TRUE),
+
+ // With no UI in core, we have to define default
+ // formatters for the teaser and full view.
+ // This may change if the method of handling displays
+ // is changed or if a UI gets into core.
+ 'display' => array(
+ 'full' => array(
+ 'label' => 'hidden',
+ 'type' => 'text_default',
+ 'exclude' => 0,
+ ),
+ 'teaser' => array(
+ 'label' => 'hidden',
+ 'type' => 'text_summary_or_trimmed',
+ 'exclude' => 0,
+ ),
+ ),
+ );
+ field_create_instance($instance);
+ }
+ else {
+ $instance['label'] = $type->body_label;
+ $instance['settings']['display_summary'] = TRUE;
+ field_update_instance($instance);
+ }
+ }
+ elseif (!empty($instance)) {
+ field_delete_instance($instance);
+ }
+
+}
+
+/**
* Deletes a node type from the database.
*
* @param $type
@@ -974,7 +1035,8 @@
// Make sure the body has the minimum number of words.
// TODO : use a better word counting algorithm that will work in other languages
- if (!empty($type->min_word_count) && isset($node->body) && count(explode(' ', $node->body)) < $type->min_word_count) {
+ if (!empty($type->min_word_count) && isset($node->{NODE_BODY_FIELD}[0]['value']) && count(explode(' ', $node->{NODE_BODY_FIELD}[0]['value'])) < $type->min_word_count) {
+ // TODO: Use Field API to set this error.
form_set_error('body', t('The body of your @type is too short. You need at least %words words.', array('%words' => $type->min_word_count, '@type' => $type->name)));
}
@@ -1011,25 +1073,6 @@
// Convert the node to an object, if necessary.
$node = (object)$node;
- // Generate the teaser, but only if it hasn't been set (e.g. by a
- // module-provided 'teaser' form item).
- if (!isset($node->teaser)) {
- if (isset($node->body)) {
- $node->format = (!empty($node->body_format) ? $node->body_format : FILTER_FORMAT_DEFAULT);
- $node->teaser = node_teaser($node->body, isset($node->format) ? $node->format : NULL);
- // Chop off the teaser from the body if needed. The teaser_include
- // property might not be set (eg. in Blog API postings), so only act on
- // it, if it was set with a given value.
- if (isset($node->teaser_include) && !$node->teaser_include && $node->teaser == substr($node->body, 0, strlen($node->teaser))) {
- $node->body = substr($node->body, strlen($node->teaser));
- }
- }
- else {
- $node->teaser = '';
- $node->format = 0;
- }
- }
-
if (user_access('administer nodes')) {
// Populate the "authored by" field.
if ($account = user_load_by_name($node->name)) {
@@ -1069,16 +1112,6 @@
if (!isset($node->log)) {
$node->log = '';
}
-
- // For the same reasons, make sure we have $node->teaser and
- // $node->body. We should consider making these fields nullable
- // in a future version since node types are not required to use them.
- if (!isset($node->teaser)) {
- $node->teaser = '';
- }
- if (!isset($node->body)) {
- $node->body = '';
- }
}
elseif (!empty($node->revision)) {
$node->old_vid = $node->vid;
@@ -1213,30 +1246,6 @@
}
/**
- * Apply filters and build the node's standard elements.
- */
-function node_prepare($node, $teaser = FALSE) {
- // First we'll overwrite the existing node teaser and body with
- // the filtered copies! Then, we'll stick those into the content
- // array and set the read more flag if appropriate.
- $node->readmore = (strlen($node->teaser) < strlen($node->body));
-
- if ($teaser == FALSE) {
- $node->body = check_markup($node->body, $node->format, $node->language, FALSE);
- }
- else {
- $node->teaser = check_markup($node->teaser, $node->format, $node->language, FALSE);
- }
-
- $node->content['body'] = array(
- '#markup' => $teaser ? $node->teaser : $node->body,
- '#weight' => 0,
- );
-
- return $node;
-}
-
-/**
* Builds a structured array representing the node's content.
*
* @param $node
@@ -1246,7 +1255,7 @@
*
* @return
* An structured array containing the individual elements
- * of the node's body.
+ * of the node's content.
*/
function node_build_content($node, $teaser = FALSE) {
@@ -1255,21 +1264,33 @@
$node->build_mode = NODE_BUILD_NORMAL;
}
- // Remove the delimiter (if any) that separates the teaser from the body.
- $node->body = isset($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 = node_invoke($node, 'view', $teaser);
}
- else {
- $node = node_prepare($node, $teaser);
- }
// Build fields content.
+ if (empty($node->content)) {
+ $node->content = array();
+ };
$node->content += field_attach_view('node', $node, $teaser);
+ // Always display a read more link on teasers because we have no way
+ // to know when a teaser view is different than a full view.
+ $links = array();
+ if ($teaser) {
+ $links['node_readmore'] = array(
+ 'title' => t('Read more'),
+ 'href' => 'node/' . $node->nid,
+ 'attributes' => array('rel' => 'tag', 'title' => strip_tags($node->title))
+ );
+ }
+ $node->content['links']['node'] = array(
+ '#type' => 'node_links',
+ '#value' => $links
+ );
+
// Allow modules to make their own additions to the node.
node_invoke_node($node, 'view', $teaser);
@@ -1489,16 +1510,16 @@
// Load results.
$results = array();
foreach ($find as $item) {
- // Build the node body.
+ // Render the node.
$node = node_load($item->sid);
$node->build_mode = NODE_BUILD_SEARCH_RESULT;
$node = node_build_content($node, FALSE, FALSE);
- $node->body = drupal_render($node->content);
+ $node->rendered = drupal_render($node->content);
// Fetch comments for snippet.
- $node->body .= module_invoke('comment', 'node', $node, 'update_index');
+ $node->rendered .= module_invoke('comment', 'node', $node, 'update_index');
// Fetch terms for snippet.
- $node->body .= module_invoke('taxonomy', 'node', $node, 'update_index');
+ $node->rendered .= module_invoke('taxonomy', 'node', $node, 'update_index');
$extra = node_invoke_node($node, 'search_result');
@@ -1511,7 +1532,7 @@
'node' => $node,
'extra' => $extra,
'score' => $total ? ($item->calculated_score / $total) : 0,
- 'snippet' => search_excerpt($keys, $node->body),
+ 'snippet' => search_excerpt($keys, $node->rendered),
);
}
return $results;
@@ -1620,7 +1641,7 @@
$links = array();
if ($type == 'node') {
- if ($teaser == 1 && $node->teaser && !empty($node->readmore)) {
+ if ($teaser == 1) {
$links['node_read_more'] = array(
'title' => t('Read more'),
'href' => "node/$node->nid",
@@ -1926,24 +1947,11 @@
if (node_hook($item, 'view')) {
$item = node_invoke($item, 'view', $teaser, FALSE);
}
- else {
- $item = node_prepare($item, $teaser);
- }
// Allow modules to change $node->content before the node is rendered.
node_invoke_node($item, 'view', $teaser, FALSE);
- // Set the proper node property, then unset unused $node property so that a
- // bad theme can not open a security hole.
- $content = drupal_render($item->content);
- if ($teaser) {
- $item->teaser = $content;
- unset($item->body);
- }
- else {
- $item->body = $content;
- unset($item->teaser);
- }
+ $item->rendered = drupal_render($item->content);
// Allow modules to modify the fully-built node.
node_invoke_node($item, 'alter', $teaser, FALSE);
@@ -1961,13 +1969,11 @@
// Prepare the item description
switch ($item_length) {
case 'fulltext':
- $item_text = $item->body;
+ $item_text = $item->rendered;
break;
case 'teaser':
- $item_text = $item->teaser;
- if (!empty($item->readmore)) {
- $item_text .= '' . l(t('read more'), 'node/' . $item->nid, array('absolute' => TRUE, 'attributes' => array('target' => '_blank'))) . '
';
- }
+ $item_text = $item->rendered;
+ $item_text .= '' . l(t('read more'), 'node/' . $item->nid, array('absolute' => TRUE, 'attributes' => array('target' => '_blank'))) . '
';
break;
case 'title':
$item_text = '';
@@ -2100,12 +2106,12 @@
// save the changed time of the most recent indexed node, for the search results half-life calculation
variable_set('node_cron_last', $node->changed);
- // Build the node body.
+ // Render the node.
$node->build_mode = NODE_BUILD_SEARCH_INDEX;
$node = node_build_content($node, FALSE, FALSE);
- $node->body = drupal_render($node->content);
+ $node->rendered = drupal_render($node->content);
- $text = '' . check_plain($node->title) . '
' . $node->body;
+ $text = '' . check_plain($node->title) . '
' . $node->rendered;
// Fetch extra data normally not visible
$extra = node_invoke_node($node, 'update_index');
@@ -2309,10 +2315,6 @@
if (empty($account)) {
$account = $user;
}
- // If the node is in a restricted format, disallow editing.
- if ($op == 'update' && !filter_access($node->format)) {
- return FALSE;
- }
if (user_access('bypass node access', $account)) {
return TRUE;
@@ -2789,10 +2791,6 @@
);
}
- if ($type->has_body) {
- $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
- }
-
return $form;
}
Index: modules/node/node.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.install,v
retrieving revision 1.14
diff -u -r1.14 node.install
--- modules/node/node.install 17 Mar 2009 12:41:54 -0000 1.14
+++ modules/node/node.install 26 Mar 2009 19:04:22 -0000
@@ -239,18 +239,6 @@
'not null' => TRUE,
'default' => '',
),
- 'body' => array(
- 'description' => 'The body of this version.',
- 'type' => 'text',
- 'not null' => TRUE,
- 'size' => 'big',
- ),
- 'teaser' => array(
- 'description' => 'The teaser of this version.',
- 'type' => 'text',
- 'not null' => TRUE,
- 'size' => 'big',
- ),
'log' => array(
'description' => 'The log entry explaining the changes in this version.',
'type' => 'text',
@@ -263,12 +251,6 @@
'not null' => TRUE,
'default' => 0,
),
- 'format' => array(
- 'description' => "The text format used by this version's body.",
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 0,
- ),
),
'indexes' => array(
'nid' => array('nid'),
@@ -326,7 +308,7 @@
'default' => '',
),
'has_body' => array(
- 'description' => 'Boolean indicating whether this type uses the {node_revision}.body field.',
+ 'description' => 'Boolean indicating whether this type has the body field attached.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
@@ -417,5 +399,89 @@
}
/**
+ * Convert body and teaser from node properties to fields.
+ */
+function node_update_7003(&$sandbox) {
+ $ret = array('#finished' => 0);
+
+ // OMG, do not frickin' tell me that in D7 I *still* can't assume
+ // that enabled core modules are fully loaded at this point...
+ field_init();
+
+ if (!isset($sandbox['from'])) {
+ // Initial invocation.
+
+ // Re-save node types to create body field instances.
+ $node_types = node_get_types('types');
+ foreach ($node_types as $type => $info) {
+ if ($info->has_body) {
+ node_type_save($info);
+ }
+ }
+
+ // Initialize state for future calls.
+ $sandbox['from'] = 0;
+ $sandbox['count'] = db_result(db_query("SELECT COUNT(*) FROM {node_revision}"));
+ }
+ else {
+ // Subsequent invocations.
+
+ // Operate on every revision of every node (whee!), in batches.
+ $batch_size = 100;
+ $query = db_select('node', 'n');
+ $nr = $query->innerJoin('node_revision', 'nr', 'n.vid = nr.vid');
+ $query->fields('n', array('type'))->fields($nr);
+ $query->range($sandbox['from'], $batch_size);
+ $revisions = $query->execute();
+
+ // Load each reversion of each node, set up NODE_BODY_FIELD
+ // appropriately, and save the node's field data. Note that
+ // node_load() will not return the body or teaser values from
+ // {node_revision} because those columns have been removed from the
+ // schema structure in memory (but not yet from the database),
+ // so we get the values from the explicit query of the table
+ // instead.
+ //
+ // We have to use node_load() and field_attach_insert() to avoid
+ // making assumptions about field storage. We cannot use
+ // node_load_multiple() because we are loading by vid, not nid.
+ //
+ // TODO: Actually, we probably can assume default field storage
+ // since this is a core update and no contrib modules are
+ // enabled yet, and it would be WAY faster.
+ $found = FALSE;
+ foreach ($revisions as $revision) {
+ $found = TRUE;
+
+ $node = node_load($revision->nid, $revision->vid);
+ unset($node->{NODE_BODY_FIELD});
+ $node->{NODE_BODY_FIELD}[0]['value'] = $revision->body;
+ if (!empty($revision->teaser) && $revision->teaser != text_teaser($revision->body)) {
+ $node->{NODE_BODY_FIELD}[0]['summary'] = $revision->teaser;
+ }
+ $node->{NODE_BODY_FIELD}[0]['format'] = $revision->format;
+ field_attach_insert('node', $node);
+ }
+
+ $ret['#finished'] = min(0.99, $sandbox['from']/$sandbox['count']);
+ $sandbox['from'] += $batch_size;
+
+ if (!$found) {
+ // All nodes are processed.
+
+ // Remove the now-obsolete body info from node_revision.
+ db_drop_field($ret, 'node_revision', 'body');
+ db_drop_field($ret, 'node_revision', 'teaser');
+ db_drop_field($ret, 'node_revision', 'format');
+
+ // We're done.
+ $ret['#finished'] = 1;
+ }
+ }
+
+ return $ret;
+}
+
+/**
* End of 6.x to 7.x updates
*/
Index: modules/field/modules/number/number.info
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/number/number.info,v
retrieving revision 1.2
diff -u -r1.2 number.info
--- modules/field/modules/number/number.info 5 Feb 2009 03:42:57 -0000 1.2
+++ modules/field/modules/number/number.info 26 Mar 2009 19:04:20 -0000
@@ -4,3 +4,4 @@
package = Core - fields
core = 7.x
files[]=number.module
+required = TRUE
Index: modules/field/modules/text/text.info
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.info,v
retrieving revision 1.2
diff -u -r1.2 text.info
--- modules/field/modules/text/text.info 5 Feb 2009 03:42:57 -0000 1.2
+++ modules/field/modules/text/text.info 26 Mar 2009 19:04:20 -0000
@@ -4,3 +4,4 @@
package = Core - fields
core = 7.x
files[]=text.module
+required = TRUE
Index: modules/field/modules/text/text.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.test,v
retrieving revision 1.2
diff -u -r1.2 text.test
--- modules/field/modules/text/text.test 26 Mar 2009 13:31:25 -0000 1.2
+++ modules/field/modules/text/text.test 26 Mar 2009 19:04:21 -0000
@@ -14,6 +14,9 @@
function setUp() {
parent::setUp('field', 'text', 'field_test');
+
+ $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
+ $this->drupalLogin($web_user);
}
// Test fields.
@@ -57,6 +60,68 @@
* Test textarea widget.
*/
+
+ /**
+ * Test 'formatted_text' setting.
+ */
+ function testTextfieldFormattedForm() {
+ $entity_type = 'test_entity';
+
+ $this->field_name = drupal_strtolower($this->randomName(). '_field_name');
+ $this->field = array('field_name' => $this->field_name, 'type' => 'text', );
+ field_create_field($this->field);
+ $this->instance = array(
+ 'field_name' => $this->field_name,
+ 'bundle' => FIELD_TEST_BUNDLE,
+ 'label' => $this->randomName(). '_label',
+ 'settings' => array(
+ 'text_processing' => TRUE,
+ ),
+ 'widget' => array(
+ 'type' => 'text_textfield',
+ )
+ );
+ field_create_instance($this->instance);
+
+ // Allow the user to use the 'Full HTML' format.
+ db_update('filter_format')->fields(array('roles' => ',2,'))->condition('format', 2)->execute();
+
+ // Display creation form.
+ $this->drupalGet('test-entity/add/test-bundle');
+ $this->assertFieldByName($this->field_name . '[0][value]', '', 'Widget is displayed');
+ $this->assertFieldByName($this->field_name . '[0][value_format]', '1', 'Format selector is displayed');
+
+ // Submit with data that should be filtered.
+ $value = $this->randomName() . '
' . $this->randomName();
+ $edit = array(
+ $this->field_name . '[0][value]' => $value,
+ $this->field_name . '[0][value_format]' => 1,
+ );
+ $this->drupalPost(NULL, $edit, t('Save'));
+ preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
+ $id = $match[1];
+ $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created');
+
+ // Display the object.
+ $entity = field_test_entity_load($id);
+ $entity->content = field_attach_view($entity_type, $entity);
+ $this->content = drupal_render($entity->content);
+ $this->assertNoRaw($value, 'Filtered tags are not displayed');
+ $this->assertRaw(str_replace('
', '', $value), 'Filtered value is displayed correctly');
+
+ // Edit and change the format to 'Full HTML'.
+ $edit = array(
+ $this->field_name . '[0][value_format]' => 2,
+ );
+ $this->drupalPost('test-entity/' . $id . '/edit', $edit, t('Save'));
+ $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated');
+
+ // Display the object.
+ $entity = field_test_entity_load($id);
+ $entity->content = field_attach_view($entity_type, $entity);
+ $this->content = drupal_render($entity->content);
+ $this->assertRaw($value, 'Value is displayed unfiltered');
+ }
// Test formatters.
/**
*
Index: modules/field/modules/text/text.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/text/text.module,v
retrieving revision 1.5
diff -u -r1.5 text.module
--- modules/field/modules/text/text.module 26 Mar 2009 13:31:25 -0000 1.5
+++ modules/field/modules/text/text.module 26 Mar 2009 19:04:21 -0000
@@ -14,6 +14,9 @@
'text_textarea' => array(
'arguments' => array('element' => NULL),
),
+ 'text_textarea_with_summary' => array(
+ 'arguments' => array('element' => NULL),
+ ),
'text_textfield' => array(
'arguments' => array('element' => NULL),
),
@@ -26,11 +29,23 @@
'field_formatter_text_trimmed' => array(
'arguments' => array('element' => NULL),
),
+ 'field_formatter_text_summary_or_trimmed' => array(
+ 'arguments' => array('element' => NULL),
+ ),
);
}
/**
* Implementation of hook_field_info().
+ *
+ * @param $max_length
+ * The maximum length for a varchar field.
+ * @param $text_processing
+ * Whether text input filters should be used.
+ * @param $display_summary
+ * Whether the summary field should be displayed. When empty
+ * and not displayed the summary will take its value from the trimmed
+ * value of the main text field.
*/
function text_field_info() {
return array(
@@ -39,7 +54,6 @@
'description' => t('This field stores varchar text in the database.'),
'settings' => array('max_length' => 255),
'instance_settings' => array('text_processing' => 0),
- 'widget_settings' => array('size' => 60),
'default_widget' => 'text_textfield',
'default_formatter' => 'text_default',
),
@@ -47,10 +61,16 @@
'label' => t('Long text'),
'description' => t('This field stores long text in the database.'),
'instance_settings' => array('text_processing' => 0),
- 'widget_settings' => array('rows' => 5),
'default_widget' => 'text_textarea',
'default_formatter' => 'text_default',
),
+ 'text_with_summary' => array(
+ 'label' => t('Long text with a summary field'),
+ 'description' => t('This field stores long text in the database along with optional summary/teaser text.'),
+ 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
+ 'default_widget' => 'text_textarea_with_summary',
+ 'default_formatter' => 'text_summary_or_trimmed',
+ ),
);
}
@@ -65,6 +85,30 @@
'size' => 'big',
'not null' => FALSE,
),
+ 'format' => array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => FALSE,
+ ),
+ );
+ }
+ else if ($field['type'] == 'text_with_summary') {
+ $columns = array(
+ 'value' => array(
+ 'type' => 'text',
+ 'size' => 'big',
+ 'not null' => FALSE,
+ ),
+ 'summary' => array(
+ 'type' => 'text',
+ 'size' => 'big',
+ 'not null' => FALSE,
+ ),
+ 'format' => array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => FALSE,
+ ),
);
}
else {
@@ -74,15 +118,13 @@
'length' => $field['settings']['max_length'],
'not null' => FALSE,
),
+ 'format' => array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => FALSE,
+ ),
);
}
- $columns += array(
- 'format' => array(
- 'type' => 'int',
- 'unsigned' => TRUE,
- 'not null' => FALSE,
- ),
- );
return $columns;
}
@@ -90,16 +132,27 @@
* Implementation of hook_field_validate().
*
* Possible error codes:
- * - 'text_max_length': The value exceeds the maximum length.
+ * - 'text_value_max_length': The value exceeds the maximum length.
+ * - 'text_summary_max_length': The summary exceeds the maximum length.
*/
function text_field_validate($obj_type, $object, $field, $instance, $items, &$errors) {
foreach ($items as $delta => $item) {
- if (!empty($item['value'])) {
- if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
- $errors[$field['field_name']][$delta][] = array(
- 'error' => 'text_max_length',
- 'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
- );
+ foreach (array('value', 'summary') as $column) {
+ if (!empty($item[$column])) {
+ if (!empty($field['settings']['max_length']) && drupal_strlen($item[$column]) > $field['settings']['max_length']) {
+ switch ($column) {
+ case 'value':
+ $message = t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
+ break;
+ case 'summary':
+ $message = t('%name: the summary may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length']));
+ break;
+ }
+ $errors[$field['field_name']][$delta][] = array(
+ 'error' => "text_{$column}_max_length",
+ 'message' => $message,
+ );
+ }
}
}
}
@@ -109,14 +162,22 @@
global $language;
foreach ($items as $delta => $item) {
// TODO D7 : this code is really node-related.
+ $format = $item['format'];
if (!empty($instance['settings']['text_processing'])) {
$check = is_null($object) || (isset($object->build_mode) && $object->build_mode == NODE_BUILD_PREVIEW);
- $text = isset($item['value']) ? check_markup($item['value'], $item['format'], isset($object->language) ? $object->language : $language, $check) : '';
+ $text = isset($item['value']) ? check_markup($item['value'], $format, isset($object->language) ? $object->language : $language, $check) : '';
+ if ($field['type'] == 'text_with_summary') {
+ $summary = isset($item['summary']) ? check_markup($item['summary'], $format, isset($object->language) ? $object->language : $language, $check) : '';
+ }
}
else {
$text = check_plain($item['value']);
+ if ($field['type'] == 'text_with_summary') {
+ $summary = check_plain($item['summary']);
+ }
}
$items[$delta]['safe'] = $text;
+ $items[$delta]['safe_summary'] = isset($summary) ? $summary : '';
}
}
@@ -137,14 +198,14 @@
return array(
'text_default' => array(
'label' => t('Default'),
- 'field types' => array('text', 'text_long'),
+ 'field types' => array('text', 'text_long', 'text_with_summary'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
),
'text_plain' => array(
'label' => t('Plain text'),
- 'field types' => array('text', 'text_long'),
+ 'field types' => array('text', 'text_long', 'text_with_summary'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
@@ -156,6 +217,13 @@
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
),
+ 'text_summary_or_trimmed' => array(
+ 'label' => t('Summary or Trimmed'),
+ 'field types' => array('text_with_summary'),
+ 'behaviors' => array(
+ 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
+ ),
+ ),
);
}
@@ -179,7 +247,136 @@
function theme_field_formatter_text_trimmed($element) {
$field = field_info_field($element['#field_name']);
$instance = field_info_instance($element['#field_name'], $element['#bundle']);
- return $instance['settings']['text_processing'] ? $element['#item']['format'] : NULL;
+ return text_teaser($element['#item']['safe'], $instance['settings']['text_processing'] ? $element['#item']['format'] : NULL);
+}
+
+/**
+ * Theme function for 'summary or trimmed' text field formatter.
+ */
+function theme_field_formatter_text_summary_or_trimmed($element) {
+ $field = field_info_field($element['#field_name']);
+ $instance = field_info_instance($element['#field_name'], $element['#bundle']);
+
+ if (!empty($element['#item']['safe_summary'])) {
+ return $element['#item']['safe_summary'];
+ }
+ else {
+ return text_teaser($element['#item']['safe'], $instance['settings']['text_processing'] ? $element['#item']['format'] : NULL);
+ }
+}
+
+/**
+ * Generate a trimmed, formatted version of a text field value.
+ *
+ * If the end of the teaser is not indicated using the delimiter
+ * then we generate the teaser automatically, trying to end it at a sensible
+ * place such as the end of a paragraph, a line break, or the end of a
+ * sentence (in that order of preference).
+ *
+ * @param $body
+ * The content for which a teaser will be generated.
+ * @param $format
+ * The format of the content. If the content contains PHP code, we do not
+ * split it up to prevent parse errors. If the line break filter is present
+ * then we treat newlines embedded in $body as line breaks.
+ * @param $size
+ * The desired character length of the teaser. If omitted, the default
+ * value will be used. Ignored if the special delimiter is present
+ * in $body.
+ * @return
+ * The generated teaser.
+ */
+function text_teaser($body, $format = NULL, $size = NULL) {
+
+ if (!isset($size)) {
+ $size = variable_get('teaser_length', 600);
+ }
+
+ // Find where the delimiter is in the body
+ $delimiter = strpos($body, '');
+
+ // If the size is zero, and there is no delimiter, the entire body is the teaser.
+ if ($size == 0 && $delimiter === FALSE) {
+ return $body;
+ }
+
+ // If a valid delimiter has been specified, use it to chop off the teaser.
+ if ($delimiter !== FALSE) {
+ return substr($body, 0, $delimiter);
+ }
+
+ // We check for the presence of the PHP evaluator filter in the current
+ // format. If the body contains PHP code, we do not split it up to prevent
+ // parse errors.
+ if (isset($format)) {
+ $filters = filter_list_format($format);
+ if (isset($filters['php/0']) && strpos($body, '') !== FALSE) {
+ return $body;
+ }
+ }
+
+ // If we have a short body, the entire body is the teaser.
+ if (drupal_strlen($body) <= $size) {
+ return $body;
+ }
+
+ // If the delimiter has not been specified, try to split at paragraph or
+ // sentence boundaries.
+
+ // The teaser may not be longer than maximum length specified. Initial slice.
+ $teaser = truncate_utf8($body, $size);
+
+ // Store the actual length of the UTF8 string -- which might not be the same
+ // as $size.
+ $max_rpos = strlen($teaser);
+
+ // How much to cut off the end of the teaser so that it doesn't end in the
+ // middle of a paragraph, sentence, or word.
+ // Initialize it to maximum in order to find the minimum.
+ $min_rpos = $max_rpos;
+
+ // Store the reverse of the teaser. We use strpos on the reversed needle and
+ // haystack for speed and convenience.
+ $reversed = strrev($teaser);
+
+ // Build an array of arrays of break points grouped by preference.
+ $break_points = array();
+
+ // A paragraph near the end of sliced teaser is most preferable.
+ $break_points[] = array('' => 0);
+
+ // If no complete paragraph then treat line breaks as paragraphs.
+ $line_breaks = array('
' => 6, '
' => 4);
+ // Newline only indicates a line break if line break converter
+ // filter is present.
+ if (isset($filters['filter/1'])) {
+ $line_breaks["\n"] = 1;
+ }
+ $break_points[] = $line_breaks;
+
+ // If the first paragraph is too long, split at the end of a sentence.
+ $break_points[] = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
+
+ // Iterate over the groups of break points until a break point is found.
+ foreach ($break_points as $points) {
+ // Look for each break point, starting at the end of the teaser.
+ foreach ($points as $point => $offset) {
+ // The teaser is already reversed, but the break point isn't.
+ $rpos = strpos($reversed, strrev($point));
+ if ($rpos !== FALSE) {
+ $min_rpos = min($rpos + $offset, $min_rpos);
+ }
+ }
+
+ // If a break point was found in this group, slice and return the teaser.
+ if ($min_rpos !== $max_rpos) {
+ // Don't slice with length 0. Length must be <0 to slice from RHS.
+ return ($min_rpos === 0) ? $teaser : substr($teaser, 0, 0 - $min_rpos);
+ }
+ }
+
+ // If a break point was not found, still return a teaser.
+ return $teaser;
}
/**
@@ -213,6 +410,15 @@
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
+ 'text_textarea_with_summary' => array(
+ 'label' => t('Text area with a summary'),
+ 'field types' => array('text_with_summary'),
+ 'settings' => array('rows' => 20, 'summary_rows' => 5),
+ 'behaviors' => array(
+ 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
+ 'default value' => FIELD_BEHAVIOR_DEFAULT,
+ ),
+ ),
);
}
@@ -234,12 +440,21 @@
'#input' => TRUE,
'#columns' => array('value'), '#delta' => 0,
'#process' => array('text_textfield_process'),
+ '#theme_wrapper' => 'text_textfield',
'#autocomplete_path' => FALSE,
),
'text_textarea' => array(
'#input' => TRUE,
'#columns' => array('value', 'format'), '#delta' => 0,
'#process' => array('text_textarea_process'),
+ '#theme_wrapper' => 'text_textarea',
+ '#filter_value' => FILTER_FORMAT_DEFAULT,
+ ),
+ 'text_textarea_with_summary' => array(
+ '#input' => TRUE,
+ '#columns' => array('value', 'format', 'summary'), '#delta' => 0,
+ '#process' => array('text_textarea_with_summary_process'),
+ '#theme_wrapper' => 'text_textarea_with_summary',
'#filter_value' => FILTER_FORMAT_DEFAULT,
),
);
@@ -289,7 +504,17 @@
* Implementation of hook_field_widget_error().
*/
function text_field_widget_error($element, $error) {
- form_error($element['value'], $error['message']);
+ switch ($error['error']) {
+ case 'text_summary_max_length':
+ $error_element = $element[$element['#columns'][1]];
+ break;
+
+ default:
+ $error_element = $element[$element['#columns'][0]];
+ break;
+ }
+
+ form_error($error_element, $error['message']);
}
/**
@@ -317,24 +542,18 @@
'#autocomplete_path' => $element['#autocomplete_path'],
'#size' => $instance['widget']['settings']['size'],
'#attributes' => array('class' => 'text'),
- // The following values were set by the field module and need
- // to be passed down to the nested element.
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
- '#field_name' => $element['#field_name'],
- '#bundle' => $element['#bundle'],
- '#delta' => $element['#delta'],
- '#columns' => $element['#columns'],
- );
+ );
$element[$field_key]['#maxlength'] = !empty($field['settings']['max_length']) ? $field['settings']['max_length'] : NULL;
if (!empty($instance['settings']['text_processing'])) {
- $filter_key = $element['#columns'][1];
+ $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
- $parents = array_merge($element['#parents'] , array($filter_key));
- $element[$filter_key] = filter_form($format, 1, $parents);
+ $element[$field_key]['#text_format'] = $format;
+ $element['#element_validate'][] = 'text_field_widget_validate';
}
return $element;
@@ -353,28 +572,88 @@
$instance = $form['#fields'][$element['#field_name']]['instance'];
$field_key = $element['#columns'][0];
$delta = $element['#delta'];
+
$element[$field_key] = array(
'#type' => 'textarea',
'#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
'#rows' => $instance['widget']['settings']['rows'],
'#weight' => 0,
- // The following values were set by the field module and need
- // to be passed down to the nested element.
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
- '#field_name' => $element['#field_name'],
- '#bundle' => $element['#bundle'],
- '#delta' => $element['#delta'],
- '#columns' => $element['#columns'],
);
if (!empty($instance['settings']['text_processing'])) {
$filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
- $parents = array_merge($element['#parents'] , array($filter_key));
- $element[$filter_key] = filter_form($format, 1, $parents);
+ $element[$field_key]['#text_format'] = $format;
+ $element['#element_validate'][] = 'text_field_widget_validate';
}
+
+ return $element;
+}
+
+/**
+ * Process an individual element.
+ *
+ * Build the form element. When creating a form using FAPI #process,
+ * note that $element['#value'] is already set.
+ *
+ * The $field and $instance arrays are in $form['#fields'][$element['#field_name']].
+ */
+function text_textarea_with_summary_process($element, $edit, $form_state, $form) {
+ $field = $form['#fields'][$element['#field_name']]['field'];
+ $instance = $form['#fields'][$element['#field_name']]['instance'];
+ $delta = $element['#delta'];
+
+ $field_key = $element['#columns'][1];
+ $display = !empty($element['#value'][$field_key]) || !empty($instance['settings']['display_summary']);
+ $element[$field_key] = array(
+ '#title' => t('Summary'),
+ '#type' => $display ? 'textarea' : 'value',
+ '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
+ '#rows' => $instance['widget']['settings']['summary_rows'],
+ '#weight' => 0,
+ '#title' => t('Summary'),
+ '#description' => t('Leave blank to use trimmed value of full text as the summary.'),
+ '#required' => $element['#required'],
+ '#display' => $display,
+ );
+
+ $field_key = $element['#columns'][0];
+ $element[$field_key] = array(
+ '#type' => 'textarea',
+ '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
+ '#rows' => $instance['widget']['settings']['rows'],
+ '#weight' => 1,
+ '#title' => $display ? t('Full text') : $element['#title'],
+ '#description' => $element['#description'],
+ '#required' => $element['#required'],
+ '#required' => $instance['required'],
+ );
+
+ if (!empty($instance['settings']['text_processing'])) {
+ $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
+ $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
+ $element[$field_key]['#text_format'] = $format;
+ $element['#element_validate'][] = 'text_field_widget_validate';
+ }
+
+ return $element;
+}
+
+/**
+ * FAPI validation of an individual text widget.
+ *
+ * Extract the input format into the expected 'format' value.
+ */
+function text_field_widget_validate($element, &$form_state) {
+ $field_key = $element['#columns'][0];
+ $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
+
+ $element['#value'][$filter_key] = $element['#value'][$field_key . '_format'];
+ unset($element['#value'][$field_key . '_format']);
+ form_set_value($element, $element['#value'], $form_state);
}
/**
@@ -395,3 +674,20 @@
function theme_text_textarea($element) {
return $element['#children'];
}
+
+function theme_text_textarea_with_summary($element) {
+ // If displaying both a textarea and a summary field, wrap them
+ // in a fieldset to make it clear they belong together.
+ $field_key = $element['#columns'][1];
+ if (!empty($element[$field_key]['#display'])) {
+ $fieldset = array(
+ '#title' => $element['#title'],
+ '#value' => $element['#children'],
+ '#attributes' => array('class' => 'text-textarea'),
+ );
+ return theme('fieldset', $fieldset);
+ }
+ else {
+ return $element['#children'];
+ }
+}
Index: modules/field/theme/field.css
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/theme/field.css,v
retrieving revision 1.3
diff -u -r1.3 field.css
--- modules/field/theme/field.css 10 Mar 2009 09:45:32 -0000 1.3
+++ modules/field/theme/field.css 26 Mar 2009 19:04:21 -0000
@@ -32,7 +32,11 @@
margin: .5em 0 0;
}
-.form-item .number {
+form .form-item .text {
display: inline;
width: auto;
-}
\ No newline at end of file
+}
+form .form-item .number {
+ display: inline;
+ width: auto;
+}
Index: modules/field/modules/options/options.info
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/options/options.info,v
retrieving revision 1.1
diff -u -r1.1 options.info
--- modules/field/modules/options/options.info 3 Feb 2009 17:30:11 -0000 1.1
+++ modules/field/modules/options/options.info 26 Mar 2009 19:04:20 -0000
@@ -4,3 +4,4 @@
package = Core - fields
core = 7.x
files[]=options.module
+required = TRUE
Index: modules/field/modules/list/list.info
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/list/list.info,v
retrieving revision 1.2
diff -u -r1.2 list.info
--- modules/field/modules/list/list.info 5 Feb 2009 03:42:57 -0000 1.2
+++ modules/field/modules/list/list.info 26 Mar 2009 19:04:20 -0000
@@ -4,3 +4,4 @@
package = Core - fields
core = 7.x
files[]=list.module
+required = TRUE