TextareaWithSummaryWidget.php 2.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php

/**
 * @file
 * Definition of Drupal\text\Plugin\field\widget\TextareaWithSummaryWidget.
 */

namespace Drupal\text\Plugin\field\widget;

use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

/**
 * Plugin implementation of the 'text_textarea_with_summary' widget.
 *
 * @Plugin(
 *   id = "text_textarea_with_summary",
 *   module = "text",
 *   label = @Translation("Text area with a summary"),
 *   field_types = {
 *     "text_with_summary"
 *   },
 *   settings = {
 *     "rows" = "9",
25 26
 *     "summary_rows" = "3",
 *     "placeholder" = ""
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 *   }
 * )
 */
class TextareaWithSummaryWidget extends TextareaWidget {

  /**
   * Overrides TextareaWidget::formElement().
   */
  function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
    $element = parent::formElement($items, $delta, $element, $langcode, $form, $form_state);

    $display_summary = !empty($items[$delta]['summary']) || $this->instance['settings']['display_summary'];
    $element['summary'] = array(
      '#type' => $display_summary ? 'textarea' : 'value',
      '#default_value' => isset($items[$delta]['summary']) ? $items[$delta]['summary'] : NULL,
      '#title' => t('Summary'),
      '#rows' => $this->getSetting('summary_rows'),
      '#description' => t('Leave blank to use trimmed value of full text as the summary.'),
      '#attached' => array(
        'js' => array(drupal_get_path('module', 'text') . '/text.js'),
      ),
      '#attributes' => array('class' => array('text-summary')),
      '#prefix' => '<div class="text-summary-wrapper">',
      '#suffix' => '</div>',
      '#weight' => -10,
    );

    return $element;
  }

  /**
   * Overrides TextareaWidget::errorElement().
   */
  public function errorElement(array $element, array $error, array $form, array &$form_state) {
    switch ($error['error']) {
      case 'text_summary_max_length':
        $error_element = $element['summary'];
        break;

      default:
        $error_element = $element;
        break;
    }

    return $error_element;
  }

}