You are here

abstract class TranslateFormBase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 21
Contains \Drupal\locale\Form\TranslateFormBase.

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 = array(
      'language' => $filter_values['langcode'],
    );
    $options = array(
      '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 = array();
    $filters = $this
      ->translateFilters();
    foreach ($filters as $key => $filter) {
      $filter_values[$key] = $filter['default'];

      // Let the filter defaults be overwritten by parameters in the URL.
      if ($this
        ->getRequest()->query
        ->has($key)) {

        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        $value = $this
          ->getRequest()->query
          ->get($key);
        if (!isset($filter['options']) || isset($filter['options'][$value])) {
          $filter_values[$key] = $value;
        }
      }
      elseif (isset($_SESSION['locale_translate_filter'][$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['locale_translate_filter'][$key]])) {
          $filter_values[$key] = $_SESSION['locale_translate_filter'][$key];
        }
      }
    }
    return static::$filterValues = $filter_values;
  }

  /**
   * Lists locale translation filters that can be applied.
   */
  protected function translateFilters() {
    $filters = array();

    // Get all languages, except English.
    $this->languageManager
      ->reset();
    $languages = $this->languageManager
      ->getLanguages();
    $language_options = array();
    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'] = array(
      'title' => $this
        ->t('String contains'),
      'description' => $this
        ->t('Leave blank to show all strings. The search is case sensitive.'),
      'default' => '',
    );
    $filters['langcode'] = array(
      'title' => $this
        ->t('Translation language'),
      'options' => $language_options,
      'default' => $default_langcode,
    );
    $filters['translation'] = array(
      'title' => $this
        ->t('Search in'),
      'options' => array(
        'all' => $this
          ->t('Both translated and untranslated strings'),
        'translated' => $this
          ->t('Only translated strings'),
        'untranslated' => $this
          ->t('Only untranslated strings'),
      ),
      'default' => 'all',
    );
    $filters['customized'] = array(
      'title' => $this
        ->t('Translation type'),
      'options' => array(
        'all' => $this
          ->t('All'),
        LOCALE_NOT_CUSTOMIZED => $this
          ->t('Non-customized translation'),
        LOCALE_CUSTOMIZED => $this
          ->t('Customized translation'),
      ),
      'states' => array(
        'visible' => array(
          ':input[name=translation]' => array(
            'value' => 'translated',
          ),
        ),
      ),
      'default' => 'all',
    );
    return $filters;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$loggerFactory protected property The logger factory.
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.
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::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 64
FormInterface::buildForm public function Form constructor. 157
FormInterface::getFormId public function Returns a unique string identifying the form. 212
FormInterface::submitForm public function Form submission handler. 167
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator protected function Returns the link generator.
LinkGeneratorTrait::l protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator public function Sets the link generator service.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service.
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.
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
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator protected function Returns the URL generator service.
UrlGeneratorTrait::redirect protected function Returns a redirect response object for the specified route.
UrlGeneratorTrait::setUrlGenerator public function Sets the URL generator service.
UrlGeneratorTrait::url protected function Generates a URL or path for a specific route based on the given parameters.