It's working fine on the Menu first level. but not working on the sub-level menus.

Comments

ThirstySix created an issue. See original summary.

jsobiecki’s picture

Ok, thanks for bug report. I'll investigate it and let you know.

thirstysix’s picture

Second Level

/**
 * Implements hook_preprocess().
 */
function translatable_menu_link_uri_preprocess(&$variables, $hook) {
  

  // Let's apply this on menu theme (menu block).
  if ($hook == 'menu' || $hook == 'menu__main') {

    // Iterate through all items in menu tree.
    foreach ($variables['items'] as &$item) {

      if(($item['below'])) {
          // Second Level
          foreach ($item['below'] as $key => $value) {

            $link = $item['below'][$key]['original_link'];
            $plugin_id = $link->getPluginId();
            //$url = NULL;

            // If menu link is generated by menu_link_content, let's go deeper into details.
            if (strpos($plugin_id, 'menu_link_content') !== FALSE) {
              $link_entity = \Drupal::service('entity.repository')
                ->loadEntityByUuid('menu_link_content', $link->getDerivativeId());
              
              //dump($link_entity);

              // We have entity, now, let's translate it.
              $translated_entity = \Drupal::service('entity.repository')->getTranslationFromContext($link_entity);

              // We inherit options from current (overriden) element.
              $options = $item['below'][$key]['url']->getOptions();
              //dump($translated_entity);
              $url = $translated_entity->link_override->first();

              $item['below'][$key]['url'] = $url->isEmpty() ? $item['below'][$key]['url'] : $url->getUrl();
              $item['below'][$key]['url']->setOptions($options);
            }
          }
      }


        /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
        $link = $item['original_link'];
        $plugin_id = $link->getPluginId();
        $url = NULL;

        // If menu link is generated by menu_link_content, let's go deeper into details.
        if (strpos($plugin_id, 'menu_link_content') !== FALSE) {
          $link_entity = \Drupal::service('entity.repository')
            ->loadEntityByUuid('menu_link_content', $link->getDerivativeId());
 
          // We have entity, now, let's translate it.
          $translated_entity = \Drupal::service('entity.repository')->getTranslationFromContext($link_entity);

          // We inherit options from current (overriden) element.
          $options = $item['url']->getOptions();
          //dump($translated_entity);
          /** @var Drupal\link\Plugin\Field\FieldType\LinkItem $url */
          $url = $translated_entity->link_override->first();
         
          $item['url'] = $url->isEmpty() ? $item['url'] : $url->getUrl();
          $item['url']->setOptions($options);
        }
        
    }
  }
}
jsobiecki’s picture

@ThirstySix - thank you for fix proposal. Could you please re-submit it as proper patch please?

In general, I understand approach here, but I think that it can be improved by using recursive approach, instead of making duplicate code in one method.

jsobiecki’s picture

Status: Active » Needs work
sanci91’s picture

I did a bit of refactoring, also integrating the fix in https://www.drupal.org/project/translatable_menu_link_uri/issues/2932662. Surely it could be better, but at least it uses recursion.

/**
 * Implements hook_preprocess().
 */
function translatable_menu_link_uri_preprocess(&$variables, $hook) {
  // Let's apply this on menu theme (menu block).
  if ($hook == 'menu' || preg_match('%^menu__%', $hook)) {
    foreach ($variables['items'] as &$item) {
      translatable_menu_link_uri_iterate_menu($item);
    }
  }
}

/**
 * Iterate over the menu and check for sublevel menu links
 */
function translatable_menu_link_uri_iterate_menu(&$item) {
  if(($item['below'])){
    foreach ($item['below'] as $key => $value) {
      translatable_menu_link_uri_iterate_menu($item['below'][$key]);
    }
  }
  /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
  $link = $item['original_link'];
  $plugin_id = $link->getPluginId();
  $url = NULL;
  // If menu link is generated by menu_link_content, let's go deeper into details.
  if (strpos($plugin_id, 'menu_link_content') !== FALSE) {
    $link_entity = \Drupal::service('entity.repository')
      ->loadEntityByUuid('menu_link_content', $link->getDerivativeId());

    // We have entity, now, let's translate it.
    $translated_entity = \Drupal::service('entity.repository')->getTranslationFromContext($link_entity);

    // We inherit options from current (overriden) element.
    $options = $item['url']->getOptions();

    /** @var Drupal\link\Plugin\Field\FieldType\Linkitem $url */
    $url = $translated_entity->link_override->first();
    $item['url'] = $url->isEmpty() ? $item['url'] : $url->getUrl();
    $item['url']->setOptions($options);
  }
}
jsobiecki’s picture

@sanci - thanks. Could you please post change in form of patch?

fant0m’s picture

fant0m’s picture

Status: Needs work » Needs review
fant0m’s picture

StatusFileSize
new3.05 KB
fant0m’s picture

StatusFileSize
new2.95 KB
rimibhagat’s picture

I implemented the patch but my site stopped working and I got the following error in the Recent Log Messages
TypeError: Argument 1 passed to Drupal\Core\Entity\EntityRepository::getTranslationFromContext() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /srv/bindings/0e54cc032c4140da8242a55169a483bd/code/modules/translatable_menu_link_uri/translatable_menu_link_uri.module on line 111 in Drupal\Core\Entity\EntityRepository->getTranslationFromContext() (line 82 of /srv/bindings/0e54cc032c4140da8242a55169a483bd/code/core/lib/Drupal/Core/Entity/EntityRepository.php)

Please help I need this to work with the sub menus too

valentine94’s picture

Status: Needs review » Needs work
amanire’s picture

The patch in #11 works for me and does not result in any errors in the watchdog log.

fant0m’s picture

Status: Needs work » Closed (cannot reproduce)

Can't reproduce. Works for me!

piggito’s picture

Status: Closed (cannot reproduce) » Needs review

Link override wasn't working for sublevel menus on my case too. Then by applying the patch in https://www.drupal.org/project/translatable_menu_link_uri/issues/2925482... it started working without messages on log.

jsobiecki’s picture

I see that patch doesn't apply, as there were few smaller changes. I'll do a rebase of patch, and introduce few small refactorization fixes (phpcbf).

  • Fant0m authored a5a5e1b on 8.x-1.x
    Issue #2925482 by Fant0m, jsobiecki, ThirstySix, amanire, piggito, sanci...
jsobiecki’s picture

Thank you for all your input, fix proposal and patch provided. As mentioned above, I slightly updated patch (made phpcs / phpmd happy) and commited it.

jsobiecki’s picture

Status: Needs review » Fixed
jsobiecki’s picture

Status: Fixed » Closed (fixed)