"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php" (26 Nov 2020, 3267 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TextareaWithSummaryWidget.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\text\Plugin\Field\FieldWidget;
    4 
    5 use Drupal\Core\Form\FormStateInterface;
    6 use Symfony\Component\Validator\ConstraintViolationInterface;
    7 use Drupal\Core\Field\FieldItemListInterface;
    8 
    9 /**
   10  * Plugin implementation of the 'text_textarea_with_summary' widget.
   11  *
   12  * @FieldWidget(
   13  *   id = "text_textarea_with_summary",
   14  *   label = @Translation("Text area with a summary"),
   15  *   field_types = {
   16  *     "text_with_summary"
   17  *   }
   18  * )
   19  */
   20 class TextareaWithSummaryWidget extends TextareaWidget {
   21 
   22   /**
   23    * {@inheritdoc}
   24    */
   25   public static function defaultSettings() {
   26     return [
   27       'rows' => '9',
   28       'summary_rows' => '3',
   29       'placeholder' => '',
   30       'show_summary' => FALSE,
   31     ] + parent::defaultSettings();
   32   }
   33 
   34   /**
   35    * {@inheritdoc}
   36    */
   37   public function settingsForm(array $form, FormStateInterface $form_state) {
   38     $element = parent::settingsForm($form, $form_state);
   39     $element['summary_rows'] = [
   40       '#type' => 'number',
   41       '#title' => t('Summary rows'),
   42       '#default_value' => $this->getSetting('summary_rows'),
   43       '#description' => $element['rows']['#description'],
   44       '#required' => TRUE,
   45       '#min' => 1,
   46     ];
   47     $element['show_summary'] = [
   48       '#type' => 'checkbox',
   49       '#title' => t('Always show the summary field'),
   50       '#default_value' => $this->getSetting('show_summary'),
   51     ];
   52     return $element;
   53   }
   54 
   55   /**
   56    * {@inheritdoc}
   57    */
   58   public function settingsSummary() {
   59     $summary = parent::settingsSummary();
   60 
   61     $summary[] = t('Number of summary rows: @rows', ['@rows' => $this->getSetting('summary_rows')]);
   62     if ($this->getSetting('show_summary')) {
   63       $summary[] = t('Summary field will always be visible');
   64     }
   65 
   66     return $summary;
   67   }
   68 
   69   /**
   70    * {@inheritdoc}
   71    */
   72   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
   73     $element = parent::formElement($items, $delta, $element, $form, $form_state);
   74 
   75     $display_summary = $items[$delta]->summary || $this->getFieldSetting('display_summary');
   76     $required = empty($form['#type']) && $this->getFieldSetting('required_summary');
   77 
   78     $element['summary'] = [
   79       '#type' => $display_summary ? 'textarea' : 'value',
   80       '#default_value' => $items[$delta]->summary,
   81       '#title' => t('Summary'),
   82       '#rows' => $this->getSetting('summary_rows'),
   83       '#description' => !$required ? $this->t('Leave blank to use trimmed value of full text as the summary.') : '',
   84       '#attributes' => ['class' => ['js-text-summary', 'text-summary']],
   85       '#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
   86       '#suffix' => '</div>',
   87       '#weight' => -10,
   88       '#required' => $required,
   89     ];
   90 
   91     if (!$this->getSetting('show_summary') && !$required) {
   92       $element['summary']['#attached']['library'][] = 'text/drupal.text';
   93     }
   94 
   95     return $element;
   96   }
   97 
   98   /**
   99    * {@inheritdoc}
  100    */
  101   public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
  102     $element = parent::errorElement($element, $violation, $form, $form_state);
  103     return ($element === FALSE) ? FALSE : $element[$violation->arrayPropertyPath[0]];
  104   }
  105 
  106 }