You are here

function _custom_shutdown_menu in Entity Translation Unified Form 8

Helper function for doing stuff after shutdown function to ensure previous db transaction is committed. Make sure the moderation state is processed correctly. The main point of this function is to make or update an alias for the other language record if it's not the same as the one that exists.

1 string reference to '_custom_shutdown_menu'
_custom_shutdown in ./entity_translation_unified_form.module
Helper function for doing stuff after shutdown function to ensure previous db transaction is committed. Make sure the moderation state is processed correctly.

File

./entity_translation_unified_form.module, line 1129

Code

function _custom_shutdown_menu($entity, $langcode) {
  $language = \Drupal::languageManager()
    ->getLanguage($langcode);
  $options = [
    'absolute' => TRUE,
    'language' => $language,
  ];
  $absolute_base_url = Url::fromRoute('<front>', [], $options)
    ->toString();
  $base_url = \Drupal::request()
    ->getBaseUrl();
  $pos_base = stripos($absolute_base_url, $base_url);
  $desired_base_url = substr($absolute_base_url, $pos_base);
  $base_url = $desired_base_url;
  if ($entity
    ->hasTranslation($langcode)) {
    $entity = $entity
      ->getTranslation($langcode);
  }
  $e_type_id = $entity
    ->getEntityTypeId();
  $path = \Drupal::service('path_alias.manager')
    ->getAliasByPath("/{$e_type_id}/" . $entity
    ->id(), $langcode);

  // Ensure base_url is used, to make this solution work for everyone.
  $path = $base_url . $path;
  $url = $entity
    ->toUrl('canonical', [], $options)
    ->toString();
  if (!is_string($url)) {
    $url = $path;
  }
  else {

    // Ensure base_url is used, to make this solution work for everyone.
    $url = $url;

    //$base_url . $url;
  }
  $debug = FALSE;
  EtufHelper::addToLog('path:' . $path, $debug);
  EtufHelper::addToLog('url:' . $url, $debug);

  // Because $path doesn't have this and we need to compare.
  $url = str_replace('/' . $langcode . '/', '/', $url);

  // Just in case, to compare path and url, give same treatment.
  $path = str_replace('/' . $langcode . '/', '/', $path);
  $test_alias = $path == "/{$e_type_id}/" . $entity
    ->id() ? TRUE : FALSE;
  $vid = 0;
  $latest_revision = etuf_latest_revision($entity
    ->id(), $vid, $langcode, $entity
    ->getEntityTypeId());

  // Ensure the alias gets updated.
  if ($test_alias || $url != $path) {
    if ($test_alias) {
      \Drupal::service('pathauto.generator')
        ->updateEntityAlias($latest_revision, 'insert', [
        'language' => $langcode,
      ]);
    }
    else {
      if ($url != $path) {
        \Drupal::service('pathauto.generator')
          ->updateEntityAlias($latest_revision, 'update', [
          'language' => $langcode,
        ]);
      }
    }
  }

  // Test plan with results.
  // New draft:.
  // select id, revision_id, langcode, path, alias from path_alias where path like '%node/3910%';
  // 9911 9911 en /node/3910 /example/english-title-1.
  // 9912 9912 fr /node/3910 /exemple/francais-titre-1.
  // Edit above draft changing title which is part of the path auto pattern:.
  // select id, revision_id, langcode, path, alias from path_alias where path like '%node/3910%';
  // 9911 9911 en /node/3910 /example/english-title-2.
  // 9912 9912 fr /node/3910 /exemple/francais-titre-2.
  // Conclusion: this code prevents duplicate alias entries and ensures all translations get their alias.
}