simple_tmgmt-1.0.x-dev/modules/simple_tmgmt_machine/simple_tmgmt_machine.module

modules/simple_tmgmt_machine/simple_tmgmt_machine.module
<?php

/**
 * @file
 * Contains simple_tmgmt_machine.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;
use Drupal\tmgmt\JobItemInterface;
use Drupal\simple_tmgmt\SimpleTmgmt;
use Drupal\simple_tmgmt\Plugin\tmgmt\Translator\MachineTranslatorInterface;

/**
 * Implements hook_help().
 */
function simple_tmgmt_machine_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the simple_tmgmt_deepl module.
    case 'help.page.simple_tmgmt_deepl':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_entity_base_field_info().
 *
 * Adds the 'machine translation' flag.
 */
function simple_tmgmt_machine_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  // Other entity types could be covered.
  // Currently limited to node to not produce noise.
  if ($entity_type->id() === 'node') {
    $fields['simple_tmgmt_machine_translation'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Is translated automatically'))
      ->setDescription(t('Indicates that this content is based machine translation, so a status can be used on the frontend. Can be unchecked if a translation has been reviewed manually.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDefaultValue(0)
      ->setDisplayOptions('view', ['region' => 'hidden'])
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'weight' => 99,
        'settings' => [
          'display_label' => TRUE,
        ],
      ])
      ->setDisplayConfigurable('view', FALSE)
      ->setDisplayConfigurable('form', TRUE);
  }

  return $fields;
}

/**
 * Implements hook_ENTITY_TYPE_translation_create().
 *
 * Handles the 'machine translation' flag.
 */
function simple_tmgmt_machine_node_translation_create(EntityInterface $translation) {
  // This implementation is currently limited to Node.
  assert($translation instanceof NodeInterface);

  // Prevents to copy the machine_translation value while creating a translation
  // (manually or via a manual TMGMT plugin).
  if (
    $translation->hasField('simple_tmgmt_machine_translation') &&
    !empty($translation->get('simple_tmgmt_machine_translation')->getValue()[0]['value'])
  ) {
    $translation->set('simple_tmgmt_machine_translation', []);
  }

  $stateKey = SimpleTmgmt::getStateKey($translation, $translation->language()->getId());
  if ($stateKey !== NULL) {
    // This flag is only set while creating a machine translation.
    if (\Drupal::state()->get($stateKey) === MachineTranslatorInterface::translatorType) {
      $translation->set('simple_tmgmt_machine_translation', ['value' => '1']);
    }
    \Drupal::state()->delete($stateKey);
  }

  $translation->get('simple_tmgmt_machine_translation')->preSave();
}

/**
 * Implements hook_form_ENTITY_ID_alter().
 *
 * Alters the node entity form, adds the 'machine translation' flag
 * in the details.
 */
function simple_tmgmt_machine_form_node_form_alter(array &$form, FormStateInterface $form_state) {
  // Do not display the 'machine translation' flag on
  // - new translations, as they are manual
  // - source (default translation)
  $formObject = $form_state->getFormObject();
  if ($formObject instanceof EntityForm) {
    /** @var \Drupal\node\NodeInterface $entity */
    $entity = $formObject->getEntity();
    if (!$entity->isDefaultTranslation() && !$entity->isNewTranslation()) {
      // Add a group.
      $form['simple_tmgmt'] = [
        '#type' => 'details',
        '#group' => 'advanced',
        '#weight' => 15,
        '#title' => t('Simple TMGMT'),
        '#attributes' => [
          'class' => ['entity-simple_tmgmt__header'],
        ],
      ];
      // Add the field in the group.
      $form['simple_tmgmt_machine_translation']['#group'] = 'simple_tmgmt';
      // @todo add field in the content_translation group
      //   see ContentTranslationHandler in content_translation module.
    }
    else {
      $form['simple_tmgmt_machine_translation']['#access'] = FALSE;
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Job item edit form.
 */
function simple_tmgmt_machine_form_tmgmt_job_item_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  /** @var \Drupal\tmgmt\JobItemInterface $jobItem */
  $jobItem = \Drupal::routeMatch()->getParameter('tmgmt_job_item');
  if (
    !$jobItem instanceof JobItemInterface ||
    !$jobItem->getSourceUrl() instanceof Url
  ) {
    return;
  }

  // If the JobItem is translated via machine translation, add a state
  // so once the translation is created, we can set the 'machine translation'
  // flag to true.
  // There might be another way to get the entity?
  $options = $jobItem->getSourceUrl()->getOptions();
  if (array_key_exists('entity', $options)) {
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $options['entity'];
    $targetLangCode = $jobItem->getJob()->getTargetLangcode();
    $stateKey = SimpleTmgmt::getStateKey($entity, $targetLangCode);
    if ($jobItem->getTranslatorPlugin() instanceof MachineTranslatorInterface) {
      \Drupal::state()->set($stateKey, MachineTranslatorInterface::translatorType);
    }
    // Elseif ($jobItem->getTranslatorPlugin() instanceof ManualTranslatorInterface) {
    //      \Drupal::state()->set($stateKey, ManualTranslatorInterface::translatorType);
    //    }.
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc