3 namespace Drupal\locale\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\locale\StringStorageInterface;
8 use Drupal\Core\State\StateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Defines the locale user interface translation form base.
14 * Provides methods for searching and filtering strings.
16 abstract class TranslateFormBase extends FormBase {
21 * @var \Drupal\locale\StringStorageInterface
23 protected $localeStorage;
28 * @var \Drupal\Core\State\StateInterface
33 * The language manager.
35 * @var \Drupal\Core\Language\LanguageManagerInterface
37 protected $languageManager;
40 * Filter values. Shared between objects that inherit this class.
44 protected static $filterValues;
47 * Constructs a new TranslationFormBase object.
49 * @param \Drupal\locale\StringStorageInterface $locale_storage
51 * @param \Drupal\Core\State\StateInterface $state
53 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
54 * The language manager.
56 public function __construct(StringStorageInterface $locale_storage, StateInterface $state, LanguageManagerInterface $language_manager) {
57 $this->localeStorage = $locale_storage;
58 $this->state = $state;
59 $this->languageManager = $language_manager;
65 public static function create(ContainerInterface $container) {
67 $container->get('locale.storage'),
68 $container->get('state'),
69 $container->get('language_manager')
74 * Builds a string search query and returns an array of string objects.
76 * @return \Drupal\locale\TranslationString[]
77 * Array of \Drupal\locale\TranslationString objects.
79 protected function translateFilterLoadStrings() {
80 $filter_values = $this->translateFilterValues();
82 // Language is sanitized to be one of the possible options in
83 // translateFilterValues().
84 $conditions = ['language' => $filter_values['langcode']];
85 $options = ['pager limit' => 30, 'translated' => TRUE, 'untranslated' => TRUE];
87 // Add translation status conditions and options.
88 switch ($filter_values['translation']) {
90 $conditions['translated'] = TRUE;
91 if ($filter_values['customized'] != 'all') {
92 $conditions['customized'] = $filter_values['customized'];
97 $conditions['translated'] = FALSE;
102 if (!empty($filter_values['string'])) {
103 $options['filters']['source'] = $filter_values['string'];
104 if ($options['translated']) {
105 $options['filters']['translation'] = $filter_values['string'];
109 return $this->localeStorage->getTranslations($conditions, $options);
113 * Builds an array out of search criteria specified in request variables.
116 * If the list of values should be reset.
121 protected function translateFilterValues($reset = FALSE) {
122 if (!$reset && static::$filterValues) {
123 return static::$filterValues;
127 $filters = $this->translateFilters();
128 foreach ($filters as $key => $filter) {
129 $filter_values[$key] = $filter['default'];
130 // Let the filter defaults be overwritten by parameters in the URL.
131 if ($this->getRequest()->query->has($key)) {
132 // Only allow this value if it was among the options, or
133 // if there were no fixed options to filter for.
134 $value = $this->getRequest()->query->get($key);
135 if (!isset($filter['options']) || isset($filter['options'][$value])) {
136 $filter_values[$key] = $value;
139 elseif (isset($_SESSION['locale_translate_filter'][$key])) {
140 // Only allow this value if it was among the options, or
141 // if there were no fixed options to filter for.
142 if (!isset($filter['options']) || isset($filter['options'][$_SESSION['locale_translate_filter'][$key]])) {
143 $filter_values[$key] = $_SESSION['locale_translate_filter'][$key];
148 return static::$filterValues = $filter_values;
152 * Lists locale translation filters that can be applied.
154 protected function translateFilters() {
157 // Get all languages, except English.
158 $this->languageManager->reset();
159 $languages = $this->languageManager->getLanguages();
160 $language_options = [];
161 foreach ($languages as $langcode => $language) {
162 if (locale_is_translatable($langcode)) {
163 $language_options[$langcode] = $language->getName();
167 // Pick the current interface language code for the filter.
168 $default_langcode = $this->languageManager->getCurrentLanguage()->getId();
169 if (!isset($language_options[$default_langcode])) {
170 $available_langcodes = array_keys($language_options);
171 $default_langcode = array_shift($available_langcodes);
174 $filters['string'] = [
175 'title' => $this->t('String contains'),
176 'description' => $this->t('Leave blank to show all strings. The search is case sensitive.'),
180 $filters['langcode'] = [
181 'title' => $this->t('Translation language'),
182 'options' => $language_options,
183 'default' => $default_langcode,
186 $filters['translation'] = [
187 'title' => $this->t('Search in'),
189 'all' => $this->t('Both translated and untranslated strings'),
190 'translated' => $this->t('Only translated strings'),
191 'untranslated' => $this->t('Only untranslated strings'),
196 $filters['customized'] = [
197 'title' => $this->t('Translation type'),
199 'all' => $this->t('All'),
200 LOCALE_NOT_CUSTOMIZED => $this->t('Non-customized translation'),
201 LOCALE_CUSTOMIZED => $this->t('Customized translation'),
205 ':input[name=translation]' => ['value' => 'translated'],