class TranslateFormBase

Same name in other branches
  1. 9 core/modules/locale/src/Form/TranslateFormBase.php \Drupal\locale\Form\TranslateFormBase
  2. 8.9.x core/modules/locale/src/Form/TranslateFormBase.php \Drupal\locale\Form\TranslateFormBase
  3. 11.x 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

Expanded class hierarchy of TranslateFormBase

File

core/modules/locale/src/Form/TranslateFormBase.php, line 16

Namespace

Drupal\locale\Form
View 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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user. 2
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 57
FormInterface::buildForm public function Form constructor. 203
FormInterface::getFormId public function Returns a unique string identifying the form. 284
FormInterface::submitForm public function Form submission handler. 229
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TranslateFormBase::$filterValues protected static property Filter values. Shared between objects that inherit this class.
TranslateFormBase::$languageManager protected property The language manager.
TranslateFormBase::$localeStorage protected property The locale storage.
TranslateFormBase::$state protected property The state store.
TranslateFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create
TranslateFormBase::translateFilterLoadStrings protected function Builds a string search query and returns an array of string objects.
TranslateFormBase::translateFilters protected function Lists locale translation filters that can be applied.
TranslateFormBase::translateFilterValues protected function Builds an array out of search criteria specified in request variables.
TranslateFormBase::__construct public function Constructs a new TranslationFormBase object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.