Beispiel #1
0
 /**
  * Adds a job item to the xml export.
  *
  * @param $item
  *   The job item entity.
  */
 protected function addItem(JobItemInterface $item)
 {
     $this->startElement('group');
     $this->writeAttribute('id', $item->id());
     // Add a note for the source label.
     $this->writeElement('note', $item->getSourceLabel());
     // @todo: Write in nested groups instead of flattening it.
     $data = \Drupal::service('tmgmt.data')->filterTranslatable($item->getData());
     foreach ($data as $key => $element) {
         $this->addTransUnit($item->id() . '][' . $key, $element, $this->job);
     }
     $this->endElement();
 }
 /**
  * Preview job item entity data.
  *
  * @param \Drupal\tmgmt\JobItemInterface $tmgmt_job_item
  *   Job item to be previewed.
  * @param string $view_mode
  *   The view mode that should be used to display the entity.
  *
  * @return array
  *   A render array as expected by drupal_render().
  */
 public function preview(JobItemInterface $tmgmt_job_item, $view_mode)
 {
     // Load entity.
     $entity = $this->entityTypeManager->getStorage($tmgmt_job_item->getItemType())->load($tmgmt_job_item->getItemId());
     // We cannot show the preview for non-existing entities.
     if (!$entity) {
         throw new NotFoundHttpException();
     }
     $data = $tmgmt_job_item->getData();
     $target_langcode = $tmgmt_job_item->getJob()->getTargetLangcode();
     // Populate preview with target translation data.
     $preview = $this->makePreview($entity, $data, $target_langcode);
     // Build view for entity.
     $page = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId())->view($preview, $view_mode, $preview->language()->getId());
     // The preview is not cacheable.
     $page['#cache']['max-age'] = 0;
     return $page;
 }
Beispiel #3
0
 /**
  * Builds the notification and diff for source changes for a data item.
  *
  * @param array $item_element
  *   The form element for the data item.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The form state.
  * @param string $field_name
  *   The name of the form element.
  * @param string $key
  *   The data item key for the given structure.
  * @param string $ajax_id
  *   The ID used for ajax replacements.
  *
  * @return array
  *   The form element for the data item.
  */
 protected function buildChangedSource($item_element, FormStateInterface $form_state, $field_name, $key, $ajax_id)
 {
     // Check for source changes and offer actions.
     if (isset($form_state->get('source_changed')[$key])) {
         // Show diff if requested.
         if ($form_state->get('show_diff:' . $key)) {
             $keys = \Drupal::service('tmgmt.data')->ensureArrayKey($field_name);
             try {
                 $new_data = \Drupal::service('tmgmt.data')->flatten($this->entity->getSourceData());
             } catch (TMGMTException $e) {
                 $new_data = [];
             }
             $current_data = $this->entity->getData($keys);
             $diff_header = ['', t('Current text'), '', t('New text')];
             $current_lines = explode("\n", $current_data['#text']);
             $new_lines = explode("\n", isset($new_data[$key]) ? $new_data[$key]['#text'] : '');
             $diff_formatter = new DiffFormatter($this->configFactory());
             $diff = new Diff($current_lines, $new_lines);
             $diff_rows = $diff_formatter->format($diff);
             // Unset start block.
             unset($diff_rows[0]);
             $item_element['below']['source_changed']['diff'] = ['#type' => 'table', '#header' => $diff_header, '#rows' => $diff_rows, '#empty' => $this->t('No visible changes'), '#attributes' => ['class' => ['diff']]];
             $item_element['below']['source_changed']['#attached']['library'][] = 'system/diff';
             $item_element['below_actions']['resolve-diff'] = ['#type' => 'submit', '#value' => t('Resolve'), '#attributes' => ['title' => t('Apply the changes of the source.')], '#name' => 'resolve-diff-' . $field_name, '#data_item_key' => $key, '#submit' => ['::resolveDiff'], '#ajax' => ['callback' => '::ajaxReviewForm', 'wrapper' => $ajax_id]];
         } else {
             $item_element['below']['source_changed'] = ['#type' => 'container', '#attributes' => ['class' => ['tmgmt_source_changed', 'messages', 'messages--warning']]];
             // Display changed message.
             $item_element['below']['source_changed']['message'] = ['#markup' => '<span>' . $form_state->get('source_changed')[$key] . '</span>', '#attributes' => ['class' => ['tmgmt-review-message-inline']]];
             if (!isset($form_state->get('source_removed')[$key])) {
                 // Offer diff action.
                 $item_element['below']['source_changed']['diff_button'] = ['#type' => 'submit', '#value' => t('Show change'), '#name' => 'diff-button-' . $field_name, '#data_item_key' => $key, '#submit' => ['::showDiff'], '#attributes' => ['class' => ['tmgmt-review-message-inline']], '#ajax' => ['callback' => '::ajaxReviewForm', 'wrapper' => $ajax_id]];
             }
         }
     }
     return $item_element;
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 public function saveTranslation(JobItemInterface $job_item, $target_langcode)
 {
     $data = $job_item->getData();
     if (isset($data['singular'])) {
         $translation = $data['singular']['#translation']['#text'];
         // Update the locale string in the system.
         // @todo: Send error message to user if update fails.
         if ($this->updateTranslation($job_item->getItemId(), $target_langcode, $translation)) {
             return TRUE;
         }
     }
     return FALSE;
 }
 /**
  * {@inheritdoc}
  */
 public function saveTranslation(JobItemInterface $job_item, $target_langcode)
 {
     /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
     $entity = entity_load($job_item->getItemType(), $job_item->getItemId());
     if (!$entity) {
         $job_item->addMessage('The entity %id of type %type does not exist, the job can not be completed.', array('%id' => $job_item->getItemId(), '%type' => $job_item->getItemType()), 'error');
         return FALSE;
     }
     $data = $job_item->getData();
     $this->doSaveTranslations($entity, $data, $target_langcode);
     return TRUE;
 }
Beispiel #6
0
 /**
  * {@inheritdoc}
  */
 public function saveTranslation(JobItemInterface $job_item, $target_langcode)
 {
     // Set a variable that can be checked later for a given job item.
     \Drupal::state()->set('tmgmt_test_saved_translation_' . $job_item->getItemType() . '_' . $job_item->getItemId(), $job_item->getData());
     return TRUE;
 }