You are here

public function JobItemForm::form in Translation Management Tool 8

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

Overrides ContentEntityForm::form

File

src/Form/JobItemForm.php, line 37

Class

JobItemForm
Form controller for the job item edit forms.

Namespace

Drupal\tmgmt\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $item = $this->entity;
  $form['#title'] = $this
    ->t('Job item @source_label', array(
    '@source_label' => $item
      ->getSourceLabel(),
  ));
  $form['info'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'tmgmt-ui-job-info',
        'clearfix',
      ),
    ),
    '#weight' => 0,
  );
  $url = $item
    ->getSourceUrl();
  $form['info']['source'] = array(
    '#type' => 'item',
    '#title' => t('Source'),
    '#markup' => $url ? Link::fromTextAndUrl($item
      ->getSourceLabel(), $url)
      ->toString() : $item
      ->getSourceLabel(),
    '#prefix' => '<div class="tmgmt-ui-source tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );
  $form['info']['sourcetype'] = array(
    '#type' => 'item',
    '#title' => t('Source type'),
    '#markup' => $item
      ->getSourceType(),
    '#prefix' => '<div class="tmgmt-ui-source-type tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );
  $form['info']['source_language'] = array(
    '#type' => 'item',
    '#title' => t('Source language'),
    '#markup' => $item
      ->getJob()
      ->getSourceLanguage()
      ->getName(),
    '#prefix' => '<div class="tmgmt-ui-source-language tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );
  $form['info']['target_language'] = array(
    '#type' => 'item',
    '#title' => t('Target language'),
    '#markup' => $item
      ->getJob()
      ->getTargetLanguage()
      ->getName(),
    '#prefix' => '<div class="tmgmt-ui-target-language tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );
  $form['info']['changed'] = array(
    '#type' => 'item',
    '#title' => t('Last change'),
    '#value' => $item
      ->getChangedTime(),
    '#markup' => $this->dateFormatter
      ->format($item
      ->getChangedTime()),
    '#prefix' => '<div class="tmgmt-ui-changed tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );
  $states = JobItem::getStates();
  $form['info']['state'] = array(
    '#type' => 'item',
    '#title' => t('State'),
    '#markup' => $states[$item
      ->getState()],
    '#prefix' => '<div class="tmgmt-ui-item-state tmgmt-ui-info-item">',
    '#suffix' => '</div>',
    '#value' => $item
      ->getState(),
  );
  $job = $item
    ->getJob();
  $form['info']['job'] = array(
    '#type' => 'item',
    '#title' => t('Job'),
    '#markup' => $job
      ->toLink()
      ->toString(),
    '#prefix' => '<div class="tmgmt-ui-job tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );

  // Display selected translator for already submitted jobs.
  if (!$item
    ->getJob()
    ->isSubmittable()) {
    $form['info']['translator'] = array(
      '#type' => 'item',
      '#title' => t('Provider'),
      '#markup' => $job
        ->getTranslatorLabel(),
      '#prefix' => '<div class="tmgmt-ui-translator tmgmt-ui-info-item">',
      '#suffix' => '</div>',
    );
  }

  // Actually build the review form elements...
  $form['review'] = array(
    '#type' => 'container',
  );

  // Build the review form.
  $data = $item
    ->getData();
  $this
    ->trackChangedSource(\Drupal::service('tmgmt.data')
    ->flatten($data), $form_state);
  $form_state
    ->set('has_preliminary_items', FALSE);
  $form_state
    ->set('all_preliminary', TRUE);

  // Need to keep the first hierarchy. So flatten must take place inside
  // of the foreach loop.
  foreach (Element::children($data) as $key) {
    $review_element = $this
      ->reviewFormElement($form_state, \Drupal::service('tmgmt.data')
      ->flatten($data[$key], $key), $key);
    if ($review_element) {
      $form['review'][$key] = $review_element;
    }
  }
  if ($form_state
    ->get('has_preliminary_items')) {
    $form['translation_changes'] = array(
      '#type' => 'container',
      '#markup' => $this
        ->t('The translations below are in preliminary state and can not be changed.'),
      '#attributes' => array(
        'class' => array(
          'messages',
          'messages--warning',
        ),
      ),
      '#weight' => -50,
    );
  }
  if ($view = View::load('tmgmt_job_item_messages')) {
    $form['messages'] = array(
      '#type' => 'details',
      '#title' => $view
        ->label(),
      '#open' => FALSE,
      '#weight' => 50,
    );
    $form['messages']['view'] = $view
      ->getExecutable()
      ->preview('block', array(
      $item
        ->id(),
    ));
  }
  $form['#attached']['library'][] = 'tmgmt/admin';

  // The reject functionality has to be implement by the translator plugin as
  // that process is completely unique and custom for each translation service.
  // Give the source ui controller a chance to affect the review form.
  $source = $this->sourceManager
    ->createUIInstance($item
    ->getPlugin());
  $form = $source
    ->reviewForm($form, $form_state, $item);

  // Give the translator ui controller a chance to affect the review form.
  if ($item
    ->getTranslator()) {
    $plugin_ui = $this->translatorManager
      ->createUIInstance($item
      ->getTranslator()
      ->getPluginId());
    $form = $plugin_ui
      ->reviewForm($form, $form_state, $item);
  }
  $form['footer'] = tmgmt_color_review_legend();
  return $form;
}