I'm defining a Drupal 7 content type using the "long text and summary" field. When a new story is created, I'd like both the "summary" and "body" text fields to be present in the creation form, instead of making the user click the "edit summary" link. Is there a way to do this through the basic Drupal content type tools (I've looked, but can't find anything), or do I need to write a hook_form_alter handler or something comparable for the content type? Thanks!
5 Answers
I think you'll have to do it in a form alter hook as you suggest, I don't know of a way to do it in the UI.
Looking at the code for text summary fields the "Edit Summary" link and functionality are done completely through javascript, added in the form by setting an #attached
property on the element, which includes a JS file (modules/field/modules/text/text.js).
All that JS file does is add that "Edit Summary" link so it would be completely safe to just get rid of it. This example uses the standard page
content type:
function mymodule_form_page_node_form_alter(&$form, &$form_state, $form_id) {
unset($form['body'][LANGUAGE_NONE][0]['summary']['#attached']);
}
-
2Yeah -- I can't say I'm particularly proud of this solution (except in its obscurity), but, as you said, it's all in the javascript. I put together a #suffix property for $form['body'] that inserts a jQuery statement into the page: on document.ready, it exposes (display = block) the wrapper around the summary form element and hides the "edit summary" link. It seems to work, and only makes me feel a little icky. Feb 16, 2012 at 16:44
The above works as does hook_js_alter
function mymodule_js_alter(&$javascript) {
unset($javascript[drupal_get_path('module','field').'/modules/text/text.js']);
}
The 2018 answer to this question is to use the Text Summary Options module, which now has a version for Drupal 8, and allows you to store settings on whether to require and/or expose by default the summary field on any long_text_with_summary field, including the Drupal body field, but also any other custom fields as well.
There is also a patch for Drupal 8 core Issue #1704864: Add a "Required" and "Show by default" option for "Text area with a summary" widget.; I have not tested it though.
You can also use hook_field_widget_form_alter(), for example:
/**
* Implements hook_field_widget_form_alter().
*/
function example_field_widget_form_alter(&$element, &$form_state, $context) {
if (
isset($element['#bundle']) &&
$element['#bundle'] == 'page' &&
$element['#field_name'] == 'body'
) {
if ($element['summary']['#attached']['js'][0] == 'modules/field/modules/text/text.js') {
unset($element['summary']['#attached']['js'][0]);
}
}
}
One note about Clive answer: you could need to remove only text.js by using this
function mymodule_form_page_node_form_alter(&$form, &$form_state, $form_id) {
$attached_js = &$form['body'][LANGUAGE_NONE][0]['summary']['#attached']['js'];
$attached_js = array_diff($attached_js, array('modules/field/modules/text/text.js'));
}