cloudwords-8.x-1.x-dev/src/Form/CloudwordsSettingsForm.php

src/Form/CloudwordsSettingsForm.php
<?php

namespace Drupal\cloudwords\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Component\Utility\UrlHelper;

class CloudwordsSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'cloudwords_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['cloudwords.settings'];
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    global $base_url;

    $config = $this->config('cloudwords.settings');

    $form['cloudwords_client_mode'] = [
      '#type' => 'radios',
      '#title' => $this->t('Environment'),
      '#description' => $this->t('Configure whether to use the staging or production environment.'),
      '#required' => TRUE,
      '#default_value' => $config->get('cloudwords_client_mode'),
      '#options' => [
        'production' => $this->t('Production'),
        'stage' => $this->t('Stage'),
      ],
    ];

    $description = $this->t('The API authorization token generated by Cloudwords.');
    $description .= ' ' . $this->t('Follow the instructions to <a target="_blank" href="https://app.cloudwords.com/cust.htm#settings/myaccount/api">generate an API key</a> for your user.');
    //@todo save fieldset values
    $form['stage_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Authorization URLs'),
    ];

    $form['stage_fieldset']['cloudwords_app_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Cloudwords Application URL'),
      '#required' => FALSE,
      '#size' => 100,
      '#default_value' => $config->get('cloudwords_app_url'),
    ];

    $form['stage_fieldset']['cloudwords_api_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Cloudwords API URL'),
      '#required' => FALSE,
      '#size' => 100,
      '#default_value' => $config->get('cloudwords_api_url'),
    ];

    $form['cloudwords_auth_token'] = [
      '#type' => 'textfield',
      '#title' => $this->t('API Authorization Token'),
      '#description' => $description,
      '#required' => TRUE,
      '#size' => 70,
      '#default_value' => $config->get('cloudwords_auth_token'),
    ];

    $form['cloudwords_temp_directory'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Temp directory'),
      '#description' => $this->t('Set the temporary directory used by this module. Defaults to the Drupal temporary directory.  This is sometimes useful to set to a predictable temporary directory if the site is behind a load balancer or on distributed architecture and the temporary directory is not persistent.  If the Drupal temporary directory is unpredictable, it is recommended to use "private://cloudwords-temp" as the value here.'),
      '#required' => FALSE,
      '#size' => 70,
      '#default_value' => cloudwords_temp_directory(),
    ];
    
    $form['cloudwords_preview_bundle_enabled'] = [
      '#type' => 'select',
      '#title' => $this->t('Preview Bundle Feature'),
      '#options' => [
        1 => $this->t('Enabled'),
        0 => $this->t('Disabled'),
      ],
      '#description' => $this->t('Enabling the In-Context Review feature allows static translation and source pages to be delivered to Cloudwords for In-Context Review.  Note that this feature is currently only available for published content as the page needs to be accessed by an anonymous user; unpublished pages will not have a preview generated for them.'),
      '#required' => TRUE,
      '#default_value' => ($config->get('cloudwords_preview_bundle_enabled') == 1) ? 1 : 0,
    ];
    $form['cloudwords_preview_bundle_enabled_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this->t(''),
      '#description' => $this->t(''),
    ];
    $form['cloudwords_preview_bundle_enabled_fieldset']['cloudwords_drupal_base_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Base URL for preview generator'),
      '#description' => $this->t('The preview generator requires a base url to be set for the site.  This defaults to the global $base_url variable.  If the base url changes, this will need to be updated to reflect that.'),
      '#required' => TRUE,
      '#size' => 70,
      '#default_value' => (!empty($config->get('cloudwords_drupal_base_url'))) ? $config->get('cloudwords_drupal_base_url') : $base_url,
    ];

    $form['automatic_import_translation_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Automatically Import Translations'),
      '#description' => $this->t('Automatically import translations delivered to the Cloudwords.'),
    ];

    $form['automatic_import_translation_fieldset']['cloudwords_auto_import_translation_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enabled'),
      '#default_value' => $config->get('cloudwords_auto_import_translation_enabled'),
    ];

    $form['automatic_import_translation_fieldset']['cloudwords_auto_import_translation_frequency'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Frequency (in minutes)'),
      '#description' => $this->t('Set the frequency (in minutes) in which to automatically check and import translated content.  This runs on your Drupal system cron and is dependent on your %settings_cron.  Setting this value to something more frequent than your cron configuration will just trigger it to run on your next Drupal system cron.  Accepted values are whole integer number such as "60" for 1 hour or "120 for 2 hours."', [
        '%settings_cron' => \Drupal::l($this->t('cron configuration'), \Drupal\Core\Url::fromRoute('system.cron_settings'))
        ]),
      '#default_value' => $config->get('cloudwords_auto_import_translation_frequency'),
    ];

    $form['automatic_import_translation_fieldset']['cloudwords_auto_import_translation_max_process_items'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Max number of translations to import at a time.'),
      '#description' => $this->t('Set the maximum number of translations to import on a single cron run.'),
      '#default_value' => $config->get('cloudwords_auto_import_translation_max_process_items'),
    ];
//
//
//    $form['auto_project_creation_fieldset'] = [
//      '#type' => 'fieldset',
//      '#title' => $this->t('Automatically Create Projects'),
//      '#description' => $this->t('Automatic project creation for out of date translations handled by the Cloudwords module.'),
//    ];
//
//    $form['auto_project_creation_fieldset']['cloudwords_auto_project_creation_enabled'] = [
//      '#type' => 'checkbox',
//      '#title' => $this->t('Enabled'),
//      '#default_value' => $config->get('cloudwords_auto_project_creation_enabled'),
//    ];
//
    $form = parent::buildForm($form, $form_state);

    $form['actions']['refresh'] = [
      '#type' => 'submit',
      '#value' => $this->t('Refresh translatables'),
      '#submit' => [
        'cloudwords_refresh_translatables'
        ],
    ];

    $form['#attached']['library'][] = 'cloudwords/cloudwords.settings_form';

    return $form;
  }

  public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    if ($form_state->getValue(['cloudwords_client_mode']) == 'stage') {
      if ($form_state->getValue(['cloudwords_app_url']) || $form_state->getValue(['cloudwords_api_url'])) {

        if ($form_state->getValue(['cloudwords_app_url']) && $form_state->getValue(['cloudwords_app_url'])) {
          $url_app_preffix = parse_url($form_state->getValue(['cloudwords_app_url']));
          $url_api_preffix = parse_url($form_state->getValue(['cloudwords_api_url']));

          if (UrlHelper::isValid($form_state->getValue(['cloudwords_app_url']), $absolute = TRUE) === FALSE || $url_app_preffix['scheme'] != 'https') {
            $form_state->setErrorByName('cloudwords_app_url', $this->t('Application URL is not a valid one or is not starting with https'));
          }
          if (UrlHelper::isValid($form_state->getValue(['cloudwords_api_url']), $absolute = TRUE) === FALSE || $url_api_preffix['scheme'] != 'https') {
            $form_state->setErrorByName('cloudwords_api_url', $this->t('API URL is not a valid one or is not starting with https'));
          }
        }
      }
      else {
        $form_state->setErrorByName('cloudwords_api_url', $this->t('Please, provide a valid API URL'));
        $form_state->setErrorByName('cloudwords_app_url', $this->t('Please, provide a valid Application URL'));
      }
    }

    if(empty($form_state->getValue('cloudwords_temp_directory'))){
      drupal_set_message('Temporary directory set back to Drupal temporary directory.');
    }else if($form_state->getValue('cloudwords_temp_directory') != file_directory_temp()){
      if(!is_dir($form_state->getValue('cloudwords_temp_directory'))){
        if (!file_prepare_directory($form_state->getValue('cloudwords_temp_directory'), FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
          $form_state->setErrorByName('cloudwords_temp_directory', $this->t('Unable to create alternate temporary directory.'));
        }
      }
    }
//    if (!is_numeric($form_state->getValue(['cloudwords_auto_import_translation_frequency']))) {
//      $form_state->setErrorByName('cloudwords_auto_import_translation_frequency', $this->t('Must be a positive number value.  This runs on cron.'));
//    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('cloudwords.settings')
      ->set('cloudwords_auth_token', $form_state->getValue('cloudwords_auth_token'))
      ->set('cloudwords_client_mode', $form_state->getValue('cloudwords_client_mode'))
      ->set('cloudwords_app_url', $form_state->getValue('cloudwords_app_url'))
      ->set('cloudwords_api_url', $form_state->getValue('cloudwords_api_url'))
      ->set('cloudwords_temp_directory', $form_state->getValue('cloudwords_temp_directory'))
      ->set('cloudwords_preview_bundle_enabled', $form_state->getValue('cloudwords_preview_bundle_enabled'))
      ->set('cloudwords_auto_import_translation_enabled', $form_state->getValue('cloudwords_auto_import_translation_enabled'))
      ->set('cloudwords_auto_import_translation_frequency', $form_state->getValue('cloudwords_auto_import_translation_frequency'))
      ->set('cloudwords_auto_import_translation_max_process_items', $form_state->getValue('cloudwords_auto_import_translation_max_process_items'))
      ->set('cloudwords_drupal_base_url', $form_state->getValue('cloudwords_drupal_base_url'))
      ->save();

    parent::submitForm($form, $form_state);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc