abstract class TranslateFormBase in Drupal 9
Same name and namespace in other branches
- 8 core/modules/locale/src/Form/TranslateFormBase.php \Drupal\locale\Form\TranslateFormBase
 
Defines the locale user interface translation form base.
Provides methods for searching and filtering strings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\locale\Form\TranslateFormBase
 
 
Expanded class hierarchy of TranslateFormBase
File
- core/
modules/ locale/ src/ Form/ TranslateFormBase.php, line 16  
Namespace
Drupal\locale\FormView source
abstract class TranslateFormBase extends FormBase {
  /**
   * The locale storage.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected $localeStorage;
  /**
   * The state store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;
  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;
  /**
   * Filter values. Shared between objects that inherit this class.
   *
   * @var array|null
   */
  protected static $filterValues;
  /**
   * Constructs a new TranslationFormBase object.
   *
   * @param \Drupal\locale\StringStorageInterface $locale_storage
   *   The locale storage.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(StringStorageInterface $locale_storage, StateInterface $state, LanguageManagerInterface $language_manager) {
    $this->localeStorage = $locale_storage;
    $this->state = $state;
    $this->languageManager = $language_manager;
  }
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('locale.storage'), $container
      ->get('state'), $container
      ->get('language_manager'));
  }
  /**
   * Builds a string search query and returns an array of string objects.
   *
   * @return \Drupal\locale\TranslationString[]
   *   Array of \Drupal\locale\TranslationString objects.
   */
  protected function translateFilterLoadStrings() {
    $filter_values = $this
      ->translateFilterValues();
    // Language is sanitized to be one of the possible options in
    // translateFilterValues().
    $conditions = [
      'language' => $filter_values['langcode'],
    ];
    $options = [
      'pager limit' => 30,
      'translated' => TRUE,
      'untranslated' => TRUE,
    ];
    // Add translation status conditions and options.
    switch ($filter_values['translation']) {
      case 'translated':
        $conditions['translated'] = TRUE;
        if ($filter_values['customized'] != 'all') {
          $conditions['customized'] = $filter_values['customized'];
        }
        break;
      case 'untranslated':
        $conditions['translated'] = FALSE;
        break;
    }
    if (!empty($filter_values['string'])) {
      $options['filters']['source'] = $filter_values['string'];
      if ($options['translated']) {
        $options['filters']['translation'] = $filter_values['string'];
      }
    }
    return $this->localeStorage
      ->getTranslations($conditions, $options);
  }
  /**
   * Builds an array out of search criteria specified in request variables.
   *
   * @param bool $reset
   *   If the list of values should be reset.
   *
   * @return array
   *   The filter values.
   */
  protected function translateFilterValues($reset = FALSE) {
    if (!$reset && static::$filterValues) {
      return static::$filterValues;
    }
    $filter_values = [];
    $filters = $this
      ->translateFilters();
    $request = $this
      ->getRequest();
    $session_filters = $request
      ->getSession()
      ->get('locale_translate_filter', []);
    foreach ($filters as $key => $filter) {
      $filter_values[$key] = $filter['default'];
      // Let the filter defaults be overwritten by parameters in the URL.
      if ($request->query
        ->has($key)) {
        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        $value = $request->query
          ->get($key);
        if (!isset($filter['options']) || isset($filter['options'][$value])) {
          $filter_values[$key] = $value;
        }
      }
      elseif (isset($session_filters[$key])) {
        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        if (!isset($filter['options']) || isset($filter['options'][$session_filters[$key]])) {
          $filter_values[$key] = $session_filters[$key];
        }
      }
    }
    return static::$filterValues = $filter_values;
  }
  /**
   * Lists locale translation filters that can be applied.
   */
  protected function translateFilters() {
    $filters = [];
    // Get all languages, except English.
    $this->languageManager
      ->reset();
    $languages = $this->languageManager
      ->getLanguages();
    $language_options = [];
    foreach ($languages as $langcode => $language) {
      if (locale_is_translatable($langcode)) {
        $language_options[$langcode] = $language
          ->getName();
      }
    }
    // Pick the current interface language code for the filter.
    $default_langcode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();
    if (!isset($language_options[$default_langcode])) {
      $available_langcodes = array_keys($language_options);
      $default_langcode = array_shift($available_langcodes);
    }
    $filters['string'] = [
      'title' => $this
        ->t('String contains'),
      'description' => $this
        ->t('Leave blank to show all strings. The search is case sensitive.'),
      'default' => '',
    ];
    $filters['langcode'] = [
      'title' => $this
        ->t('Translation language'),
      'options' => $language_options,
      'default' => $default_langcode,
    ];
    $filters['translation'] = [
      'title' => $this
        ->t('Search in'),
      'options' => [
        'all' => $this
          ->t('Both translated and untranslated strings'),
        'translated' => $this
          ->t('Only translated strings'),
        'untranslated' => $this
          ->t('Only untranslated strings'),
      ],
      'default' => 'all',
    ];
    $filters['customized'] = [
      'title' => $this
        ->t('Translation type'),
      'options' => [
        'all' => $this
          ->t('All'),
        LOCALE_NOT_CUSTOMIZED => $this
          ->t('Non-customized translation'),
        LOCALE_CUSTOMIZED => $this
          ->t('Customized translation'),
      ],
      'states' => [
        'visible' => [
          ':input[name=translation]' => [
            'value' => 'translated',
          ],
        ],
      ],
      'default' => 'all',
    ];
    return $filters;
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            DependencySerializationTrait:: | 
                  protected | property | ||
| 
            DependencySerializationTrait:: | 
                  protected | property | ||
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            FormBase:: | 
                  protected | property | The config factory. | 3 | 
| 
            FormBase:: | 
                  protected | property | The request stack. | 1 | 
| 
            FormBase:: | 
                  protected | property | The route match. | |
| 
            FormBase:: | 
                  protected | function | Retrieves a configuration object. | |
| 
            FormBase:: | 
                  protected | function | Gets the config factory for this form. | 3 | 
| 
            FormBase:: | 
                  private | function | Returns the service container. | |
| 
            FormBase:: | 
                  protected | function | Gets the current user. | |
| 
            FormBase:: | 
                  protected | function | Gets the request object. | |
| 
            FormBase:: | 
                  protected | function | Gets the route match. | |
| 
            FormBase:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            FormBase:: | 
                  protected | function | Returns a redirect response object for the specified route. | |
| 
            FormBase:: | 
                  public | function | Resets the configuration factory. | |
| 
            FormBase:: | 
                  public | function | Sets the config factory for this form. | |
| 
            FormBase:: | 
                  public | function | Sets the request stack object to use. | |
| 
            FormBase:: | 
                  public | function | 
            Form validation handler. Overrides FormInterface:: | 
                  72 | 
| 
            FormInterface:: | 
                  public | function | Form constructor. | 192 | 
| 
            FormInterface:: | 
                  public | function | Returns a unique string identifying the form. | 264 | 
| 
            FormInterface:: | 
                  public | function | Form submission handler. | 214 | 
| 
            LoggerChannelTrait:: | 
                  protected | property | The logger channel factory service. | |
| 
            LoggerChannelTrait:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            LoggerChannelTrait:: | 
                  public | function | Injects the logger channel factory. | |
| 
            MessengerTrait:: | 
                  protected | property | The messenger. | 27 | 
| 
            MessengerTrait:: | 
                  public | function | Gets the messenger. | 27 | 
| 
            MessengerTrait:: | 
                  public | function | Sets the messenger. | |
| 
            RedirectDestinationTrait:: | 
                  protected | property | The redirect destination service. | 1 | 
| 
            RedirectDestinationTrait:: | 
                  protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| 
            RedirectDestinationTrait:: | 
                  protected | function | Returns the redirect destination service. | |
| 
            RedirectDestinationTrait:: | 
                  public | function | Sets the redirect destination service. | |
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | 4 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. | |
| 
            TranslateFormBase:: | 
                  protected static | property | Filter values. Shared between objects that inherit this class. | |
| 
            TranslateFormBase:: | 
                  protected | property | The language manager. | |
| 
            TranslateFormBase:: | 
                  protected | property | The locale storage. | |
| 
            TranslateFormBase:: | 
                  protected | property | The state store. | |
| 
            TranslateFormBase:: | 
                  public static | function | 
            Instantiates a new instance of this class. Overrides FormBase:: | 
                  |
| 
            TranslateFormBase:: | 
                  protected | function | Builds a string search query and returns an array of string objects. | |
| 
            TranslateFormBase:: | 
                  protected | function | Lists locale translation filters that can be applied. | |
| 
            TranslateFormBase:: | 
                  protected | function | Builds an array out of search criteria specified in request variables. | |
| 
            TranslateFormBase:: | 
                  public | function | Constructs a new TranslationFormBase object. |