getTranslationElement example

/** * Defines the textfield element for the configuration translation interface. */
class Textfield extends FormElementBase {

  /** * {@inheritdoc} */
  public function getTranslationElement(LanguageInterface $translation_language$source_config$translation_config) {
    return [
      '#type' => 'textfield',
    ] + parent::getTranslationElement($translation_language$source_config$translation_config);
  }

}
public function getTranslationElement(LanguageInterface $translation_language$source_config$translation_config) {
    // Estimate a comfortable size of the input textarea.     if (is_string($translation_config)) {
      $rows_words = ceil(str_word_count($translation_config) / 5);
      $rows_newlines = substr_count($translation_config, "\n") + 1;
      $rows = max($rows_words$rows_newlines);
    }

    return [
      '#type' => 'textarea',
      '#rows' => $rows ?? 1,
    ] + parent::getTranslationElement($translation_language$source_config$translation_config);
  }

}
$build['#theme'] = 'config_translation_manage_form_element';

    // For accessibility we make source and translation appear next to each     // other in the source for each element, which is why we utilize the     // 'source' and 'translation' sub-keys for the form. The form values,     // however, should mirror the configuration structure, so that we can     // traverse the configuration schema and still access the right     // configuration values in ConfigTranslationFormBase::setConfig().     // Therefore we make the 'source' and 'translation' keys the top-level     // keys in $form_state['values'].     $build['source'] = $this->getSourceElement($source_language$source_config);
    $build['translation'] = $this->getTranslationElement($translation_language$source_config$translation_config);

    $build['source']['#parents'] = array_merge(['source']$parents);
    $build['translation']['#parents'] = array_merge(['translation']$parents);
    return $build;
  }

  /** * Returns the source element for a given configuration definition. * * This can be either a render array that actually outputs the source values * directly or a read-only form element with the source values depending on * what is considered to provide a more intuitive user interface for the * translator. * * @param \Drupal\Core\Language\LanguageInterface $source_language * Thee source language of the configuration object. * @param mixed $source_config * The configuration value of the element in the source language. * * @return array * A render array for the source value. */
return [
      '#type' => 'textfield',
      '#description' => $description,
      '#field_suffix' => ' <small data-drupal-date-formatter="preview">' . $format . '</small>',
      '#attributes' => [
        'data-drupal-date-formatter' => 'source',
      ],
      '#attached' => [
        'drupalSettings' => ['dateFormats' => $date_formatter->getSampleDateFormats($translation_language->getId())],
        'library' => ['system/drupal.system.date'],
      ],
    ] + parent::getTranslationElement($translation_language$source_config$translation_config);
  }

}

class TextFormat extends FormElementBase {

  /** * {@inheritdoc} */
  public function getSourceElement(LanguageInterface $source_language$source_config) {
    // Instead of the formatted output show a disabled textarea. This allows for     // easier side-by-side comparison, especially with formats with text     // editors.     return $this->getTranslationElement($source_language$source_config$source_config) + [
      '#value' => $source_config['value'],
      '#disabled' => TRUE,
      '#allow_focus' => TRUE,
    ];
  }

  /** * {@inheritdoc} */
  public function getTranslationElement(LanguageInterface $translation_language$source_config$translation_config) {
    return [
      
Home | Imprint | This part of the site doesn't use cookies.