You are here

public function CartForm::buildForm in Translation Management Tool 8

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/CartForm.php, line 25

Class

CartForm
Source overview form.

Namespace

Drupal\tmgmt\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $plugin = NULL, $item_type = NULL) {
  $languages = tmgmt_available_languages();
  $options = array();
  $selected = [];
  foreach (tmgmt_cart_get()
    ->getJobItemsFromCart() as $item) {
    $url = $item
      ->getSourceUrl();
    $selected[$item
      ->id()] = TRUE;
    $options[$item
      ->id()] = array(
      $item
        ->getSourceType(),
      $url ? Link::fromTextAndUrl($item
        ->label(), $url)
        ->toString() : $item
        ->label(),
      isset($languages[$item
        ->getSourceLangCode()]) ? $languages[$item
        ->getSourceLangCode()] : t('Unknown'),
    );
  }
  $form['items'] = array(
    '#type' => 'tableselect',
    '#header' => array(
      t('Type'),
      t('Content'),
      t('Language'),
    ),
    '#empty' => t('There are no items in your cart.'),
    '#options' => $options,
    '#default_value' => $selected,
  );
  $form['enforced_source_language'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enforce source language'),
    '#description' => t('The source language is determined from the item\'s source language. If you wish to enforce a different language you can select one after ticking this checkbox. In such case the translation of the language you selected will be used as the source for the translation job.'),
  );
  $form['source_language'] = array(
    '#type' => 'select',
    '#title' => t('Source language'),
    '#description' => t('Select a language that will be enforced as the translation job source language.'),
    '#options' => $languages,
    '#states' => array(
      'visible' => array(
        ':input[name="enforced_source_language"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['target_language'] = array(
    '#type' => 'select',
    '#title' => t('Request translation into language/s'),
    '#multiple' => TRUE,
    '#options' => $languages,
    '#description' => t('If the item\'s source language will be the same as the target language the item will be ignored.'),
  );
  $form['request_translation'] = array(
    '#type' => 'submit',
    '#button_type' => 'primary',
    '#value' => t('Request translation'),
    '#submit' => array(
      '::submitRequestTranslation',
    ),
    '#validate' => array(
      'tmgmt_cart_source_overview_validate',
    ),
  );
  $form['remove_selected'] = array(
    '#type' => 'submit',
    '#button_type' => 'danger',
    '#value' => t('Remove selected item'),
    '#submit' => array(
      '::submitRemoveSelected',
    ),
    '#validate' => array(
      'tmgmt_cart_source_overview_validate',
    ),
  );
  $form['empty_cart'] = array(
    '#type' => 'submit',
    '#button_type' => 'danger',
    '#value' => t('Empty cart'),
    '#submit' => array(
      '::submitEmptyCart',
    ),
  );
  return $form;
}