How to repurpose the body summary field as a title field in Drupal 8

The body summary field is often ignored because it's hidden by default in a collapsible div. A recent project necessitated the use of the body summary field for a content type. But not as a summary of the body content, but as a title to be displayed and hyperlinked in Views.

I could have added a separate text field for this purpose, but thought it made equal or more sense to repurpose the often underutilized body summary textarea.

Using hook_form_alter(), I first made the summary field (aka. title) visible by unsetting the class attributes.

I second changed the summary field type from "textarea" to "textfield".

I next changed the summary field label (to correspond with the body label) and removed the description text.

And finally, I added a validation callback handler to make the title field required when the body field has content.

It's always a good idea to ensure a field exists before making changes to its properties.

Copy and paste the following code into a custom module. The only things you'll need to change is the form_ids and validation function name. Note this technique can be used with any text field of type "Text (formatted, long, with summary)".

use \Drupal\Core\Form\FormStateInterface;

/**
* Implements hook_form_alter().
*/
function custom_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'node_segment_form':
    case 'node_segment_edit_form':
      // Repurpose the summary field to serve as a title field for body.
      // Sanity check to ensure the body field exists.
      if (isset($form['body'])) {
        // Remove default Summary field collapse.
        unset($form['body']['widget'][0]['summary']['#attributes']);
        // Change field type from textarea to textfield.
        $form['body']['widget'][0]['summary']['#type'] = 'textfield';
        // Rename default 'Summary' label.
        $form['body']['widget'][0]['summary']['#title'] = t('@title title', ['@title' => $form['body']['widget'][0]['#title'],]);
        // Remove helper text description below field.
        unset($form['body']['widget'][0]['summary']['#description']);
        // Define custom validation handler.
        $form['#validate'][] = 'custom_form_node_segment_body_summary_validate';
      }
      break;
  }
}

/**
* Custom validation handler for node_segment_form & node_segment_edit_form.
*/
function custom_form_node_segment_body_summary_validate(array &$form, FormStateInterface $form_state) {
  $body = reset($form_state->getValue('body'));
  if (empty($body['summary']) && !empty($body['value'])) {
    // Summary (aka. title) field must be defined when
    // the corresponding body field contains content.
    $form_state->setErrorByName('body', t('@title title is empty.', ['@title' => $form['body']['widget'][0]['#title'],]));
  }
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.