Textarea.php 1.08 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\config_translation\FormElement\Textarea.
 */

namespace Drupal\config_translation\FormElement;

10
use Drupal\Core\Language\LanguageInterface;
11
use Drupal\Core\StringTranslation\StringTranslationTrait;
12
use Drupal\Core\TypedData\DataDefinitionInterface;
13 14 15 16

/**
 * Defines the textarea element for the configuration translation interface.
 */
17 18
class Textarea implements ElementInterface {
  use StringTranslationTrait;
19 20 21 22

  /**
   * {@inheritdoc}
   */
23
  public function getFormElement(DataDefinitionInterface $definition, LanguageInterface $language, $value) {
24 25 26 27 28 29 30 31
    // Estimate a comfortable size of the input textarea.
    $rows_words = ceil(str_word_count($value) / 5);
    $rows_newlines = substr_count($value, "\n" ) + 1;
    $rows = max($rows_words, $rows_newlines);

    return array(
      '#type' => 'textarea',
      '#default_value' => $value,
32
      '#title' => $this->t($definition->getLabel()) . '<span class="visually-hidden"> (' . $language->getName() . ')</span>',
33
      '#rows' => $rows,
34
      '#attributes' => array('lang' => $language->getId()),
35 36 37 38
    );
  }

}