/** * 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(); }
/** * Builds the translation form element for a data item. * * @param array $item_element * The form element for the data item. * @param int $rows * The number of rows that should be displayed. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return array * The form element for the data item. */ protected function buildTranslation($item_element, $data_item, $rows, FormStateInterface $form_state) { if (!empty($data_item['#format']) && $this->config('tmgmt.settings')->get('respect_text_format') && !$form_state->has('accept_item')) { $item_element['translation'] = array('#type' => 'text_format', '#default_value' => isset($data_item['#translation']['#text']) ? $data_item['#translation']['#text'] : NULL, '#title' => t('Translation'), '#disabled' => $this->entity->isAccepted(), '#rows' => $rows, '#allowed_formats' => array($data_item['#format'])); } elseif ($form_state->has('accept_item')) { $item_element['translation'] = array('#type' => 'textarea', '#title' => t('Translation'), '#value' => t('This field has been disabled because you do not have sufficient permissions to edit it. It is not possible to review or accept this job item.'), '#disabled' => TRUE, '#rows' => $rows); } else { $item_element['translation'] = array('#type' => 'textarea', '#default_value' => isset($data_item['#translation']['#text']) ? $data_item['#translation']['#text'] : NULL, '#title' => t('Translation'), '#disabled' => $this->entity->isAccepted(), '#rows' => $rows); if (!empty($data_item['#max_length'])) { $item_element['translation']['#max_length'] = $data_item['#max_length']; $item_element['translation']['#element_validate'] = ['::validateMaxLength']; } } if (!empty($data_item['#translation']['#text_revisions'])) { $revisions = array(); foreach ($data_item['#translation']['#text_revisions'] as $revision) { $revisions[] = t('Origin: %origin, Created: %created<br />%text', array('%origin' => $revision['#origin'], '%created' => format_date($revision['#timestamp']), '%text' => Xss::filter($revision['#text']))); } $item_element['below']['revisions_wrapper'] = array('#type' => 'details', '#title' => t('Translation revisions'), '#open' => TRUE); $item_element['below']['revisions_wrapper']['revisions'] = array('#theme' => 'item_list', '#items' => $revisions); } return $item_element; }
/** * The _title_callback for the page that renders a single node in preview. * * @param \Drupal\tmgmt\JobItemInterface $tmgmt_job_item * The current node. * * @return string * The page title. */ public function title(JobItemInterface $tmgmt_job_item) { $target_language = $tmgmt_job_item->getJob()->getTargetLanguage()->getName(); $title = $this->entityTypeManager->getStorage($tmgmt_job_item->getItemType())->load($tmgmt_job_item->getItemId())->getTitle(); return t("Preview of @title for @target_language", ['@title' => $title, '@target_language' => $target_language]); }
/** * {@inheritdoc} */ public function rejectDataItem(JobItemInterface $job_item, array $key, array $values = NULL) { $key = '[' . implode('][', $key) . ']'; $job_item->addMessage('Rejected data item @key for job item @item in job @job.', array('@key' => $key, '@item' => $job_item->id(), '@job' => $job_item->getJobId())); return TRUE; }
/** * {@inheritdoc} */ public function getExistingLangCodes(JobItemInterface $job_item) { $query = db_select('locales_target', 'lt'); $query->fields('lt', array('language')); $query->condition('lt.lid', $job_item->getItemId()); $existing_lang_codes = array('en'); foreach ($query->execute() as $language) { $existing_lang_codes[] = $language->language; } return $existing_lang_codes; }
/** * {@inheritdoc} */ public function getType(JobItemInterface $job_item) { return ucfirst($job_item->getItemType()); }
/** * Builds the translation status render array with source and job item status. * * @param int $status * The source status: original, missing, current or outofdate. * @param \Drupal\tmgmt\JobItemInterface|NULL $job_item * The existing job item for the source. * * @return array * The render array for displaying the status. */ function buildTranslationStatus($status, JobItemInterface $job_item = NULL) { switch ($status) { case 'original': $label = t('Source language'); $icon = 'core/misc/icons/bebebe/house.svg'; break; case 'missing': $label = t('Not translated'); $icon = 'core/misc/icons/bebebe/ex.svg'; break; case 'outofdate': $label = t('Translation Outdated'); $icon = drupal_get_path('module', 'tmgmt') . '/icons/outdated.svg'; break; default: $label = t('Translation up to date'); $icon = 'core/misc/icons/73b355/check.svg'; } $build['source'] = ['#theme' => 'image', '#uri' => $icon, '#title' => $label, '#alt' => $label]; // If we have an active job item, wrap it in a link. if ($job_item) { $states_labels = JobItem::getStates(); $state_label = $states_labels[$job_item->getState()]; $label = t('Active job item: @state', array('@state' => $state_label)); $url = $job_item->toUrl(); $job = $job_item->getJob(); switch ($job_item->getState()) { case JobItem::STATE_ACTIVE: if ($job->isUnprocessed()) { $url = $job->toUrl(); $label = t('Active job item: @state', array('@state' => $state_label)); } $icon = drupal_get_path('module', 'tmgmt') . '/icons/hourglass.svg'; break; case JobItem::STATE_REVIEW: $icon = drupal_get_path('module', 'tmgmt') . '/icons/ready.svg'; break; } $url->setOption('query', \Drupal::destination()->getAsArray()); $url->setOption('attributes', array('title' => $label)); $item_icon = ['#theme' => 'image', '#uri' => $icon, '#title' => $label, '#alt' => $label]; $build['job_item'] = ['#type' => 'link', '#url' => $url, '#title' => $item_icon]; } return $build; }
/** * {@inheritdoc} */ public function getType(JobItemInterface $job_item) { if ($job_item->getItemType() == static::SIMPLE_CONFIG) { $definition = $this->configMapperManager->getDefinition($job_item->getItemId()); } else { $definition = $this->configMapperManager->getDefinition($job_item->getItemType()); } return $definition['title']; }
/** * {@inheritdoc} */ public function addExistingItem(JobItemInterface $item) { $item->tjid = $this->id(); $item->save(); }
/** * {@inheritdoc} */ public function getPreviewUrl(JobItemInterface $job_item) { if ($job_item->getJob()->isActive() && !($job_item->isAborted() || $job_item->isAccepted())) { return new Url('tmgmt_content.job_item_preview', ['tmgmt_job_item' => $job_item->id()], ['query' => ['key' => \Drupal::service('tmgmt_content.key_access')->getKey($job_item)]]); } else { return NULL; } }
/** * Asserts job item language codes. * * @param \Drupal\tmgmt\JobItemInterface $job_item * Job item to check. * @param string $expected_source_lang * Expected source language. * @param array $actual_lang_codes * Expected existing language codes (translations). */ function assertJobItemLangCodes(JobItemInterface $job_item, $expected_source_lang, array $actual_lang_codes) { $this->assertEqual($job_item->getSourceLangCode(), $expected_source_lang); $existing = $job_item->getExistingLangCodes(); sort($existing); sort($actual_lang_codes); $this->assertEqual($existing, $actual_lang_codes); }
/** * Generates a key from job item data that can be used in the URL. * * @param \Drupal\tmgmt\JobItemInterface $tmgmt_job_item * Job item. * * @return string * Returns hashed key that is safe to use in the URL. */ public function getKey(JobItemInterface $tmgmt_job_item) { return Crypt::hmacBase64($tmgmt_job_item->id(), Settings::getHashSalt()); }
/** * {@inheritdoc} */ public function addTaskItem(JobItemInterface $job_item) { // Save the task to get an id. if ($this->isNew()) { $this->save(); } $local_task = LocalTaskItem::create(array('tltid' => $this->id(), 'tjiid' => $job_item->id())); $local_task->save(); return $local_task; }
/** * {@inheritdoc} */ public function getSourceLangCode(JobItemInterface $job_item) { $source_languages = \Drupal::state()->get('tmgmt.test_source_languages', array()); if (isset($source_languages[$job_item->id()])) { return $source_languages[$job_item->id()]; } return 'en'; }
/** * {@inheritdoc} */ public function getData(JobItemInterface $job_item) { return array('dummy' => array('deep_nesting' => array('#text' => file_get_contents(drupal_get_path('module', 'tmgmt') . '/tests/testing_html/sample.html'), '#label' => 'Label for job item with type ' . $job_item->getItemType() . ' and id ' . $job_item->getItemId() . '.'))); }