You are here

public static function EtufHelper::translateLinkIfNotTranslated in Entity Translation Unified Form 8

1 call to EtufHelper::translateLinkIfNotTranslated()
EtufHelper::postCreateOrUpdateAutoTranslate in src/EtufHelper.php

File

src/EtufHelper.php, line 75

Class

EtufHelper
Helper class for the Etuf module.

Namespace

Drupal\entity_translation_unified_form

Code

public static function translateLinkIfNotTranslated($nid, $other_lang) {
  $menu_link_ids = [];
  $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
  $result = $menu_link_manager
    ->loadLinksByRoute('entity.node.canonical', [
    'node' => $nid,
  ]);
  $node = NULL;
  foreach ($result as $menu_item) {
    if (is_object($menu_item)) {
      $id = $menu_item
        ->getPluginDefinition()['metadata']['entity_id'];
      $menu_link = \Drupal::entityTypeManager()
        ->getStorage('menu_link_content')
        ->load($id);
      if (!$menu_link
        ->hasTranslation($other_lang)) {
        $node = Node::load($nid);
        if (isset($node) && !is_null($node)) {
          if ($node
            ->hasTranslation($other_lang)) {
            $title = $node
              ->getTranslation($other_lang)
              ->getTitle();
            $menu_link
              ->addTranslation($other_lang, [
              'title' => $title,
            ]);
            $menu_link
              ->save();
            $menu_link_ids[] = $id;
            $logmsg = t('Translated menu link mlid:@mlid for nid:@nid', [
              '@mlid' => $id,
              '@nid' => $nid,
            ]);
            static::addToLog($logmsg, TRUE);
          }
        }
      }
    }
  }
  return $menu_link_ids;
}