You are here

function lingotek_form_alter in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8 lingotek.module \lingotek_form_alter()
  2. 8.2 lingotek.module \lingotek_form_alter()
  3. 6 lingotek.module \lingotek_form_alter()
  4. 7.4 lingotek.module \lingotek_form_alter()
  5. 4.0.x lingotek.module \lingotek_form_alter()
  6. 3.0.x lingotek.module \lingotek_form_alter()
  7. 3.1.x lingotek.module \lingotek_form_alter()
  8. 3.2.x lingotek.module \lingotek_form_alter()
  9. 3.3.x lingotek.module \lingotek_form_alter()
  10. 3.5.x lingotek.module \lingotek_form_alter()
  11. 3.6.x lingotek.module \lingotek_form_alter()
  12. 3.7.x lingotek.module \lingotek_form_alter()
  13. 3.8.x lingotek.module \lingotek_form_alter()

Implements hook_form_alter().

File

./lingotek.module, line 482
Implements Drupal-related hooks for the Lingotek Translation module.

Code

function lingotek_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state
    ->getFormObject();
  if ($form_object instanceof ContentEntityFormInterface) {

    // If it is a delete form, return.
    if ('delete' === $form_object
      ->getOperation()) {
      return;
    }
    $entity = $form_object
      ->getEntity();

    /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
    $content_translation_manager = \Drupal::service('content_translation.manager');

    /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_config */
    $lingotek_config = \Drupal::service('lingotek.configuration');

    // If content translation is not enabled and lingotek configured, don't add
    // the form element.
    if (!$content_translation_manager
      ->isEnabled($entity
      ->getEntityTypeId(), $entity
      ->bundle()) || !$lingotek_config
      ->isEnabled($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {
      return;
    }
    $default_profile = $lingotek_config
      ->getEntityProfile($entity);

    // We need to ensure it can't be NULL.
    if ($default_profile === NULL) {
      $default_profile = LingotekProfile::load(Lingotek::PROFILE_MANUAL);
    }
    $profile_options = $lingotek_config
      ->getProfileOptions();
    $current_user = \Drupal::currentUser();
    $can_assign_profile = $current_user
      ->hasPermission('assign lingotek translation profiles') || $current_user
      ->hasPermission('administer lingotek');
    $form['lingotek_translation_management'] = [
      '#type' => 'details',
      '#title' => t('Translation Management'),
      '#description' => t("The Lingotek Translation module was developed to help you translate your site. The module integrates the Lingotek translation management system directly into Drupal, so that your users can leverage the power of Lingotek's translation tools and services without ever having to leave the comfort of your Drupal environment."),
      '#tree' => TRUE,
      '#weight' => 0,
      '#access' => $can_assign_profile,
    ];
    if (isset($form['advanced'])) {
      $form['lingotek_translation_management']['#group'] = 'advanced';
      $form['lingotek_translation_management']['#weight'] = 15;
    }
    $form['lingotek_translation_management']['lingotek_translation_profile'] = [
      '#type' => 'select',
      '#title' => t('Translation Profile'),
      '#options' => $profile_options,
      // If the profile is invalid, default to not do anything for safety.
      '#default_value' => array_key_exists($default_profile
        ->id(), $profile_options) ? $default_profile
        ->id() : Lingotek::PROFILE_MANUAL,
      '#access' => $can_assign_profile,
    ];
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && $action != 'lingotek_metadata' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {

        // We need to act before the node itself is saved.
        $submit_actions = $form['actions'][$action]['#submit'];
        array_unshift($submit_actions, 'lingotek_form_content_entity_form_submit');
        $form['actions'][$action]['#submit'] = $submit_actions;
      }
    }
  }
}