Commit 8e29dd1d authored by Antonín Slejška's avatar Antonín Slejška
Browse files

Replace const FIELDS with private method fields() that translates the user facing string literals.

parent 0d326a10
Loading
Loading
Loading
Loading
+27 −21
Changes for src/Form/OpenSearchSettingsForm.php: 27 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -12,58 +12,64 @@ use Drupal\Core\Url;
 */
class OpenSearchSettingsForm extends ConfigFormBase {

  // Definitions for all text fields in the form.
  const FIELDS = [
  /**
   * Define all text fields in the form.
   *
   * @return array
   */
  private function fields() {
    return [
      [
      'title' => 'Search form path',
      'description' => 'Path to the search form, e.g.: "/search". If empty, the following value will be used: "/search/node".',
        'title' => $this->t('Search form path'),
        'description' => $this->t('Path to the search form, e.g.: "/search". If empty, the following value will be used: "/search/node".'),
        'name' => 'opensearchtab_search_form_path',
        'id' => 'search-form-path',
        'min_length' => 2,
      ],
      [
      'title' => 'Search path',
      'description' => 'Path to the search, e.g.: "/search?{searchTerms}". If empty, the following value will be used: "/search/node?keys={searchTerms}".',
        'title' => $this->t('Search path'),
        'description' => $this->t('Path to the search, e.g.: "/search?{searchTerms}". If empty, the following value will be used: "/search/node?keys={searchTerms}".'),
        'name' => 'opensearchtab_search_path',
        'id' => 'search-path',
        'min_length' => 15,
      ],
      [
      'title' => 'Suggestions path (JSON)',
      'description' => 'Path to the suggestions, e.g.: "/suggestions?{searchTerms}".',
        'title' => $this->t('Suggestions path (JSON)'),
        'description' => $this->t('Path to the suggestions, e.g.: "/suggestions?{searchTerms}".'),
        'name' => 'opensearchtab_suggestions_path_json',
        'id' => 'suggestions-path-json',
        'min_length' => 15,
      ],
      [
      'title' => 'Suggestions path (XML)',
      'description' => 'Path to the suggestions, e.g.: "/suggestions?{searchTerms}".',
        'title' => $this->t('Suggestions path (XML)'),
        'description' => $this->t('Path to the suggestions, e.g.: "/suggestions?{searchTerms}".'),
        'name' => 'opensearchtab_suggestions_path_xml',
        'id' => 'suggestions-path-xml',
        'min_length' => 15,
      ],
      [
      'title' => 'Short name',
      'description' => 'Short name will be displayed in the search box. If empty, the site name be used.',
        'title' => $this->t('Short name'),
        'description' => $this->t('Short name will be displayed in the search box. If empty, the site name be used.'),
        'name' => 'opensearchtab_shortname',
        'id' => 'shortname',
        'min_length' => 3,
      ],
      [
      'title' => 'Description',
      'description' => "Description of the search.",
        'title' => $this->t('Description'),
        'description' => $this->t('Description of the search.'),
        'name' => 'opensearchtab_description',
        'id' => 'description',
        'min_length' => 10,
      ],
      [
      'title' => 'Favicon path',
      'description' => 'Path to the Favicon, e.g.: "/themes/contrib/mytheme/favicon.ico".',
        'title' => $this->t('Favicon path'),
        'description' => $this->t('Path to the Favicon, e.g.: "/themes/contrib/mytheme/favicon.ico".'),
        'name' => 'opensearchtab_favicon_path',
        'id' => 'favicon-path',
        'min_length' => 5,
      ],
    ];
  }

  /**
   * {@inheritdoc}
@@ -94,12 +100,12 @@ class OpenSearchSettingsForm extends ConfigFormBase {
      '#open' => TRUE,
    ];

    foreach (self::FIELDS as $field) {
    foreach ($this->fields() as $field) {
      $form[$field['name']] = [
        '#type' => 'textfield',
        '#title' => $this->t('@title', ['@title' => $field['title']]),
        '#title' => $field['title'],
        '#default_value' => $config->get($field['id']),
        '#description' => $this->t('@description', ['@description' => $field['description']]),
        '#description' => $field['description'],
      ];
    }

@@ -111,7 +117,7 @@ class OpenSearchSettingsForm extends ConfigFormBase {
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    // Test the fields for the minimum length.
    foreach (self::FIELDS as $field) {
    foreach ($this->fields() as $field) {
      if (!$form_state->isValueEmpty($field['name'])) {
        if (strlen($form_state->getValue($field['name'])) < $field['min_length']) {
          $form_state->setErrorByName($field['name'],
@@ -133,7 +139,7 @@ class OpenSearchSettingsForm extends ConfigFormBase {
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    foreach (self::FIELDS as $field) {
    foreach ($this->fields() as $field) {
      $this->config('opensearchtab.settings')
        ->set($field['id'], $form_state->getValue($field['name']));
    }