You are here

public function TranslatorForm::form in Translation Management Tool 8

Overrides Drupal\Core\Entity\EntityForm::form().

Overrides EntityForm::form

File

src/Form/TranslatorForm.php, line 65

Class

TranslatorForm
Form controller for the translator edit forms.

Namespace

Drupal\tmgmt\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  if ($this->operation == 'clone') {
    $this->entity = $this->entity
      ->createDuplicate();
  }
  $entity = $this->entity;

  // Check if the translator is currently in use.
  if ($busy = !$entity
    ->isNew() ? tmgmt_translator_busy($entity
    ->id()) : FALSE) {
    \Drupal::messenger()
      ->addWarning(t("This provider is currently in use. It cannot be deleted. The chosen provider Plugin cannot be changed."));
  }
  $available = $this->translatorManager
    ->getLabels();

  // If the translator plugin is not set, pick the first available plugin as the
  // default.
  if (!$entity
    ->hasPlugin()) {
    $entity
      ->setPluginID(key($available));
  }
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#description' => t('The label of the provider.'),
    '#default_value' => $entity
      ->label(),
    '#required' => TRUE,
    '#size' => 32,
    '#maxlength' => 64,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Machine name'),
    '#description' => t('The machine readable name of this provider. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
    '#default_value' => $entity
      ->id(),
    '#machine_name' => array(
      'exists' => '\\Drupal\\tmgmt\\Entity\\Translator::load',
      'source' => array(
        'label',
      ),
    ),
    '#disabled' => !$entity
      ->isNew(),
    '#size' => 32,
    '#maxlength' => 64,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('The description of the provider.'),
    '#default_value' => $entity
      ->getDescription(),
    '#size' => 32,
    '#maxlength' => 255,
  );
  $form['auto_accept'] = array(
    '#type' => 'checkbox',
    '#title' => t('Auto accept finished translations'),
    '#description' => t('This skips the reviewing process and automatically accepts all translations as soon as they are returned by the translation provider.'),
    '#default_value' => $entity
      ->isAutoAccept(),
  );
  $form['plugin_wrapper'] = array(
    '#type' => 'container',
    '#prefix' => '<div id="tmgmt-plugin-wrapper">',
    '#suffix' => '</div>',
  );

  // Pull the translator plugin info if any.
  if ($entity
    ->hasPlugin()) {
    $definition = $this->translatorManager
      ->getDefinition($entity
      ->getPluginId());
    $form['plugin_wrapper']['plugin'] = array(
      '#type' => 'select',
      '#title' => t('Provider plugin'),
      '#submit' => array(
        '::updateRemoteLanguagesMappings',
      ),
      '#limit_validation_errors' => array(
        array(
          'plugin',
        ),
      ),
      '#executes_submit_callback' => TRUE,
      '#description' => isset($definition['description']) ? Xss::filter($definition['description']) : '',
      '#options' => $available,
      '#default_value' => $entity
        ->getPluginId(),
      '#required' => TRUE,
      '#disabled' => $busy,
      '#ajax' => array(
        'callback' => array(
          $this,
          'ajaxTranslatorPluginSelect',
        ),
        'wrapper' => 'tmgmt-plugin-wrapper',
      ),
    );

    // Add the provider logo in the settings wrapper.
    if (isset($definition['logo'])) {
      $form['plugin_wrapper']['logo'] = $logo_render_array = [
        '#theme' => 'image',
        '#uri' => file_create_url(drupal_get_path('module', $definition['provider']) . '/' . $definition['logo']),
        '#alt' => $definition['label'],
        '#title' => $definition['label'],
        '#attributes' => [
          'class' => 'tmgmt-logo-settings',
        ],
        '#suffix' => '<div class="clearfix"></div>',
      ];
    }
    $form['plugin_wrapper']['settings'] = array(
      '#type' => 'details',
      '#title' => t('@plugin plugin settings', array(
        '@plugin' => $definition['label'],
      )),
      '#tree' => TRUE,
      '#open' => TRUE,
    );

    // Add the translator plugin settings form.
    $plugin_ui = $this->translatorManager
      ->createUIInstance($entity
      ->getPluginId());
    $form_state
      ->set('busy', $busy);
    $form['plugin_wrapper']['settings'] += $plugin_ui
      ->buildConfigurationForm($form['plugin_wrapper']['settings'], $form_state);
    if (!Element::children($form['plugin_wrapper']['settings'])) {
      $form['#description'] = t("The @plugin plugin doesn't provide any settings.", array(
        '@plugin' => $plugin_ui
          ->getPluginDefinition()['label'],
      ));
    }

    // If current translator is configured to provide remote language mapping
    // provide the form to configure mappings, unless it does not exists yet.
    if ($entity
      ->providesRemoteLanguageMappings()) {
      $form['plugin_wrapper']['remote_languages_mappings'] = array(
        '#tree' => TRUE,
        '#type' => 'details',
        '#title' => t('Remote languages mappings'),
        '#description' => t('Here you can specify mappings of your local language codes to the translator language codes.'),
        '#open' => TRUE,
      );
      $options = $entity
        ->getSupportedRemoteLanguages();
      foreach ($entity
        ->getRemoteLanguagesMappings() as $local_language => $remote_language) {
        $form['plugin_wrapper']['remote_languages_mappings'][$local_language] = array(
          '#type' => 'textfield',
          '#title' => \Drupal::languageManager()
            ->getLanguage($local_language)
            ->getName() . ' (' . $local_language . ')',
          '#default_value' => $remote_language,
          '#size' => 6,
        );
        if (!empty($options)) {
          $form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#type'] = 'select';
          $form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#options'] = $options;
          $form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#empty_option'] = ' - ';
          unset($form['plugin_wrapper']['remote_languages_mappings'][$local_language]['#size']);
        }
      }
    }
  }
  $form['#attached']['library'][] = 'tmgmt/admin';
  return $form;
}