Hi all,

First of all I love your tool... Now without it I cannot imagine how our site managed without this tool....

What I really love also is the sources overview form. It has really easy look through what has been done, etc...

My question / request is - Could you point me of how to extend this table and ad other custom information?

I think it would be really cool if this could be extended to include other Content Admin related info such whether "Content" specific field been added / whether there are comments / etc....

So if you could point me to right direction I can start trying it out... I think it has to do with ConfigSourcePluginUi class and ConfigSource

Cheers,

Issue fork tmgmt-2920020

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

puogintas created an issue. See original summary.

Povilas Uogintas’s picture

Hey All,

I have writing custom views field plugin to show if translation is added or not for the content... I tried to re-use this source overview form code... For me it is useful as this can be used in many different views....

<?php
 
namespace Drupal\foooo\Plugin\views\field;
 
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\Component\Plugin\PluginBase;
 
use Drupal\tmgmt\SourcePluginUiBase;
use Drupal\tmgmt\TMGMTException;
use Drupal\tmgmt_config\Plugin\tmgmt\Source\ConfigSource;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt\JobItemInterface;

/**
 * Field handler to flag the node type.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("translation_status")
 */
class EntityTranslationStatus extends FieldPluginBase {
 
  private $entity;

  /**
   * @{inheritdoc}
   */
  public function query() {
    /* move along nothing to see here */
  }
 
  /**
   * Define the available options
   * @return array
   */
  protected function defineOptions() {
    $options = parent::defineOptions();

    /* defaults */
    $options['languages']['default'] = 'en';

    /* retrieve all available options */
    foreach (\Drupal::languageManager()->getLanguages() as $langcode => $language) {
      $data = (array) $language;
      $options['languages'][$langcode] = current($data);
    }

    return $options;
  }
 
  /**
   * Provide the options form.
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {

    $options = [];
    foreach (\Drupal::languageManager()->getLanguages() as $langcode => $language) {
      $data = (array) $language;
      $options[$langcode] = current($data);
    }

    $form['languages'] = [
      '#title' => $this->t('Which Language should be checked?'),
      '#type' => 'select',
      '#default_value' => $this->options['languages'],
      '#options' => $options,
    ];
 
    parent::buildOptionsForm($form, $form_state);
  }
 
  /**
   * @{inheritdoc}
   */
  public function render(ResultRow $values) {
    $langcode = $this->options['languages'];
    $this->entity = $values->_entity;
    $source_lang = $this->entity->language()->getId();

    $translation_status = 'current';
    $translations = $this->entity->getTranslationLanguages();

    $language = \Drupal::languageManager()->getLanguage($langcode);

    if($langcode == $source_lang) {
      $translation_status = 'original';
    } elseif (!isset($translations[$langcode])) {
      $translation_status = 'missing';
    } elseif ($translation = $this->entity->getTranslation($langcode)) {
      $metadata = \Drupal::service('content_translation.manager')->getTranslationMetadata($translation);
      if ($metadata->isOutdated()) {
        $translation_status = 'outofdate';
      }
    }

    $current_job_items = tmgmt_job_item_load_latest('content', $this->entity->getEntityTypeId(), $this->entity->id(), $source_lang);

    $build = $this->buildTranslationStatus($translation_status, isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL);

    if($translation_status != 'missing' && $this->entity->hasLinkTemplate('canonical')) {
      $build['source'] = [
        '#type' => 'link',
        '#type' => 'link',
        '#url' => $this->entity->toUrl('canonical', ['language' => $language]),
        '#title' => $build['source'],
      ];
    }

    $output = \Drupal::service('renderer')->render($build);

    return $output;
  }

  /**
  * Builds icon + link
  * @private
  * @method function
  * @param {Object} $status
  * @return {Object} description
  */
  private 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 ($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;
  }

  /**
   * Checks, if an entity is translated.
   *
   * @param string $langcode
   *   Language code.
   * @param string $name
   *   Configuration name.
   *
   * @return bool
   *   Returns TRUE, if it is translatable.
   */
  public function isTranslated($langcode, $name) {
    $config = \Drupal::languageManager()->getLanguageConfigOverride($langcode, $name);
    return !$config->isNew();
  }
}

And hook_views_data_alter()

function foooo_views_data_alter(&$data) {
  /* Translation status field */
  $data['node']['translation_status'] = [
    'title' => t('Status of Selected Language'),
    'group' => t('Content'),
    'field' => [
      'title' => t('Status of Selected Language'),
      'help' => t('Shows status of selected Language'),
      'id' => 'translation_status',
    ],
  ];
}

I know this could be improved greatly but if anybody founds this useful use it....
Now I am working on a filter which should filter translation statuses -> in this issue https://www.drupal.org/node/2921300

Cheers

Berdir made their first commit to this issue’s fork.

Berdir’s picture

Berdir’s picture