예제 #1
0
 /**
  * Builds a translation form element.
  *
  * @param array $data
  *   Data of the translation.
  * @param LocalTaskItem $item
  *   The LocalTaskItem.
  * @param string $zebra
  *   Tell is translation is odd or even.
  *
  * @return array
  *   Render array with the translation element.
  */
 private function formElement(array $data, LocalTaskItem $item, &$zebra)
 {
     static $flip = array('even' => 'odd', 'odd' => 'even');
     $form = [];
     $job = $item->getJobItem()->getJob();
     $language_list = \Drupal::languageManager()->getLanguages();
     foreach (Element::children($data) as $key) {
         if (isset($data[$key]['#text']) && \Drupal::service('tmgmt.data')->filterData($data[$key])) {
             // The char sequence '][' confuses the form API so we need to replace
             // it.
             $target_key = str_replace('][', '|', $key);
             $zebra = $flip[$zebra];
             $form[$target_key] = array('#tree' => TRUE, '#theme' => 'tmgmt_local_translation_form_element', '#ajaxid' => Html::getUniqueId('tmgmt-local-element-' . $key), '#parent_label' => $data[$key]['#parent_label'], '#zebra' => $zebra);
             $form[$target_key]['status'] = array('#theme' => 'tmgmt_local_translation_form_element_status', '#value' => $this->entity->isCompleted() ? TMGMT_DATA_ITEM_STATE_COMPLETED : $data[$key]['#status']);
             // Manage the height of the texteareas, depending on the lenght of the
             // description. The minimum number of rows is 3 and the maximum is 15.
             $rows = ceil(strlen($data[$key]['#text']) / 100);
             if ($rows < 3) {
                 $rows = 3;
             } elseif ($rows > 15) {
                 $rows = 15;
             }
             $form[$target_key]['source'] = ['#type' => 'textarea', '#value' => $data[$key]['#text'], '#title' => t('Source'), '#disabled' => TRUE, '#rows' => $rows];
             $form[$target_key]['translation'] = ['#type' => 'textarea', '#default_value' => isset($data[$key]['#translation']['#text']) ? $data[$key]['#translation']['#text'] : NULL, '#title' => t('Translation'), '#disabled' => !$item->isPending(), '#rows' => $rows, '#allow_focus' => TRUE];
             if (!empty($data[$key]['#format']) && \Drupal::config('tmgmt.settings')->get('respect_text_format') == '1') {
                 $format_id = $data[$key]['#format'];
                 /** @var \Drupal\filter\Entity\FilterFormat $format */
                 $format = FilterFormat::load($format_id);
                 if ($format && $format->access('use')) {
                     // In case a user has permission to translate the content using
                     // selected text format, add a format id into the list of allowed
                     // text formats. Otherwise, no text format will be used.
                     $form[$target_key]['source']['#allowed_formats'] = [$format_id];
                     $form[$target_key]['translation']['#allowed_formats'] = [$format_id];
                     $form[$target_key]['source']['#type'] = 'text_format';
                     $form[$target_key]['translation']['#type'] = 'text_format';
                 }
             }
             $form[$target_key]['actions'] = array('#type' => 'container', '#access' => $item->isPending());
             $status = $item->getData(\Drupal::service('tmgmt.data')->ensureArrayKey($key), '#status');
             if ($status == TMGMT_DATA_ITEM_STATE_TRANSLATED) {
                 $form[$target_key]['actions']['reject-' . $target_key] = array('#type' => 'submit', '#value' => '✗', '#attributes' => array('title' => t('Reject')), '#name' => 'reject-' . $target_key, '#submit' => ['::save', '::submitStatus'], '#ajax' => array('callback' => '::ajaxReviewForm', 'wrapper' => $form[$target_key]['#ajaxid']), '#tmgmt_local_action' => 'reject', '#tmgmt_local_key' => str_replace('][', '|', $key));
             } else {
                 $form[$target_key]['actions']['finish-' . $target_key] = array('#type' => 'submit', '#value' => '✓', '#attributes' => array('title' => t('Finish')), '#name' => 'finish-' . $target_key, '#submit' => ['::save', '::submitStatus'], '#ajax' => array('callback' => '::ajaxReviewForm', 'wrapper' => $form[$target_key]['#ajaxid']), '#tmgmt_local_action' => 'finish', '#tmgmt_local_key' => str_replace('][', '|', $key));
             }
         }
     }
     return $form;
 }