You are here

public function LingotekInterfaceTranslationForm::buildForm in Lingotek Translation 3.6.x

Same name and namespace in other branches
  1. 4.0.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()
  2. 3.2.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()
  3. 3.3.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()
  4. 3.4.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()
  5. 3.5.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()
  6. 3.7.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()
  7. 3.8.x src/Form/LingotekInterfaceTranslationForm.php \Drupal\lingotek\Form\LingotekInterfaceTranslationForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/LingotekInterfaceTranslationForm.php, line 95

Class

LingotekInterfaceTranslationForm
Class LingotekInterfaceTranslationForm.

Namespace

Drupal\lingotek\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if ($redirect = $this
    ->checkSetup()) {
    return $redirect;
  }
  if (!$this->moduleHandler
    ->moduleExists('potx')) {
    $form['missing_potx'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('The <a href=":potx">potx</a> module is required for interface translation with Lingotek', [
        ':potx' => 'https://www.drupal.org/project/potx',
      ]),
    ];
    return $form;
  }
  $form['theme_vtab'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Themes'),
  ];
  $form['module_vtab'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Modules'),
  ];
  $form['profile_vtab'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Profiles'),
  ];
  $form['theme']['custom'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom themes'),
    '#open' => TRUE,
    '#group' => 'theme_vtab',
  ];
  $form['theme']['contrib'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Contributed themes'),
    '#open' => FALSE,
    '#group' => 'theme_vtab',
  ];
  $form['theme']['core'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Drupal Core themes'),
    '#open' => FALSE,
    '#group' => 'theme_vtab',
  ];
  $form['module']['custom'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom modules'),
    '#open' => TRUE,
    '#group' => 'module_vtab',
  ];
  $form['module']['contrib'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Contributed modules'),
    '#open' => FALSE,
    '#group' => 'module_vtab',
  ];
  $form['module']['core'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Drupal Core modules'),
    '#open' => FALSE,
    '#group' => 'module_vtab',
  ];
  $form['profile']['custom'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom profiles'),
    '#open' => TRUE,
    '#group' => 'profile_vtab',
  ];
  $form['profile']['contrib'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Contributed profiles'),
    '#open' => FALSE,
    '#group' => 'profile_vtab',
  ];
  $form['profile']['core'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Drupal Core profiles'),
    '#open' => FALSE,
    '#group' => 'profile_vtab',
  ];
  $tables = [
    'core',
    'custom',
    'contrib',
  ];
  $componentTypes = [
    'theme',
    'module',
    'profile',
  ];
  foreach ($componentTypes as $componentType) {
    foreach ($tables as $table) {
      $form[$componentType][$table]['table'] = [
        '#type' => 'table',
        '#header' => [
          'label' => $this
            ->t('Label'),
          'machine_name' => $this
            ->t('Machine name'),
          'source' => $this
            ->t('Source'),
          'translations' => $this
            ->t('Translations'),
        ],
        '#empty' => $this
          ->t('No %component to translate in this category', [
          '%component' => $componentType,
        ]),
        '#rows' => [],
      ];
    }
  }
  $themes = $this->themeHandler
    ->listInfo();
  foreach ($themes as $theme => $themeInfo) {
    $component = $themeInfo
      ->getPath();
    $targetStatuses = $this->lingotekInterfaceTranslation
      ->getTargetStatuses($component);
    unset($targetStatuses['EN']);
    $row = [
      'label' => $themeInfo->info['name'],
      'machine_name' => $themeInfo
        ->getName(),
      'source' => [
        'data' => [
          '#type' => 'lingotek_source_status',
          '#ui_component' => $component,
          '#language' => $this->languageManager
            ->getLanguage('en'),
          '#status' => $this->lingotekInterfaceTranslation
            ->getSourceStatus($component),
        ],
      ],
      'translations' => [
        'data' => [
          '#type' => 'lingotek_target_statuses',
          '#ui_component' => $component,
          '#source_langcode' => 'en',
          '#statuses' => $this->lingotekInterfaceTranslation
            ->getTargetStatuses($component),
        ],
      ],
    ];
    [
      $path_part1,
      $path_part2,
    ] = explode('/', $themeInfo
      ->getPath());
    if ($path_part1 == 'core') {
      $form['theme']['core']['table']['#rows'][] = $row;
    }
    elseif ($path_part1 == 'contrib' || $path_part2 == 'contrib') {
      $form['theme']['contrib']['table']['#rows'][] = $row;
    }
    elseif ($path_part1 == 'custom' || $path_part2 == 'custom') {
      $form['theme']['custom']['table']['#rows'][] = $row;
    }
    else {
      $form['theme']['custom']['table']['#rows'][] = $row;
    }
  }
  if ($this->moduleHandler
    ->moduleExists('cohesion')) {
    $template_location = COHESION_TEMPLATE_PATH;

    // Get real path to templates and extract relative path for interface translation.
    if ($wrapper = \Drupal::service('stream_wrapper_manager')
      ->getViaUri($template_location)) {
      $template_path = $wrapper
        ->basePath() . '/cohesion/templates';
    }

    // This is a fake component.
    $component = $template_path;
    $row = [
      'label' => 'Cohesion templates',
      'machine_name' => 'cohesion_templates',
      'source' => [
        'data' => [
          '#type' => 'lingotek_source_status',
          '#ui_component' => $component,
          '#language' => $this->languageManager
            ->getLanguage('en'),
          '#status' => $this->lingotekInterfaceTranslation
            ->getSourceStatus($component),
        ],
      ],
      'translations' => [
        'data' => [
          '#type' => 'lingotek_target_statuses',
          '#ui_component' => $component,
          '#source_langcode' => 'en',
          '#statuses' => $this->lingotekInterfaceTranslation
            ->getTargetStatuses($component),
        ],
      ],
    ];
    $form['theme']['custom']['table']['#rows'][] = $row;
  }
  $modules = $this->moduleHandler
    ->getModuleList();
  foreach ($modules as $module => $moduleInfo) {
    $component = $moduleInfo
      ->getPath();
    $type = $moduleInfo
      ->getType();

    // We don't inject this service as its interface is not considered "stable".

    /** @var \Drupal\Core\Extension\ModuleExtensionList $moduleExtensionList */
    $moduleExtensionList = \Drupal::service('extension.list.module');
    $userFriendlyName = $moduleExtensionList
      ->getName($module);
    $row = [
      'label' => $userFriendlyName,
      'machine_name' => $moduleInfo
        ->getName(),
      'source' => [
        'data' => [
          '#type' => 'lingotek_source_status',
          '#ui_component' => $component,
          '#language' => $this->languageManager
            ->getLanguage('en'),
          '#status' => $this->lingotekInterfaceTranslation
            ->getSourceStatus($component),
        ],
      ],
      'translations' => [
        'data' => [
          '#type' => 'lingotek_target_statuses',
          '#ui_component' => $component,
          '#source_langcode' => 'en',
          '#statuses' => $this->lingotekInterfaceTranslation
            ->getTargetStatuses($component),
        ],
      ],
    ];
    [
      $path_part1,
      $path_part2,
    ] = explode('/', $moduleInfo
      ->getPath());
    if ($path_part1 == 'core') {
      $form[$type]['core']['table']['#rows'][] = $row;
    }
    elseif ($path_part1 == 'contrib' || $path_part2 == 'contrib') {
      $form[$type]['contrib']['table']['#rows'][] = $row;
    }
    elseif ($path_part1 == 'custom' || $path_part2 == 'custom') {
      $form[$type]['custom']['table']['#rows'][] = $row;
    }
    else {
      $form[$type]['custom']['table']['#rows'][] = $row;
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['clear_metadata'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Clear Lingotek interface translation metadata'),
    '#button_type' => 'danger',
    '#submit' => [
      [
        $this,
        'clearInterfaceMetadata',
      ],
    ],
  ];
  return $form;
}