0

Is there any way to redirect page to homepage if its translated page not found.

Suppose my page url is /en/node/237, If I switch translation from English to German /ge/node/237, if it's translated page not found, URL should redirect to homepage of its native language like: /ge/home

Here is my code:

 use \Drupal\node\Entity\Node;

   $elements = [];
 $ids = \Drupal::entityQuery('node')
  ->condition('type', 'intro_2')
  ->condition('langcode', 'fr')
  ->condition('nid',1218)
  ->execute();

 $nodes = Node::loadMultiple($ids);
  $language = 'fr';

 foreach ($nodes as $node) {
 $translation = $node->getTranslation($language);
$elements[] = [
 'node' =>$translation->nid->value,
'title' => $translation->title->value,
'url' => $translation->url(),
];
  }
dpm($elements);

Above code working fine i have checked on /devel/php,but i am having only following issues:

1)How to get current page node type dynamically in database query

2)How to get current page language dynamically in database query

3)How to get current page node dynamically in database query

Can anyone modify the above code?

1

To get current node:

$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
  $node_type = $node->getType();
}

Current language:

$language = \Drupal::languageManager()->getCurrentLanguage();
$langcode = $language->getId();
$language_name = $language->getName();

Check if translation is not available and redirect:

use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;

$translation = $node->getTranslation($langcode);
if (!$translation) {
  $url = Url::fromRoute('<front>', [], ['language' => $language]);
  $response = new RedirectResponse($url->toString());
  $response->send();
}

P.S.: I believe entity query you posted is a simplified version of smth more complex in your logic. Otherwise I see no reason to pass node id to entity query condition to get it back eventually. If you always have just one node use Node::load($id) instead of Node::loadMultiple($ids), and you don't need foreach in this case.

  • your $url = Url::fromRoute('<front>', [], ['language' => $language]); $response = new RedirectResponse($url->toString()); condition not working – Vikram fz16 Apr 11 '18 at 11:18
1

Try using hasTranslation()

use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse

function YOUR_MODULE_preprocess_node(&$variables) {
  $languageCode = \Drupal::languageManager()->getCurrentLanguage()->getId();
  if (!$variable['node']->hasTranslation($languageCode)) {
    $url = Url::fromRoute('<front>', [], ['language' => $language]);
    $response = new RedirectResponse($url->toString());
    $response->send();
  }
}
0

Finally i solved the issue by myself :

use \Drupal\node\Entity\Node;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse
function hook_preprocess_node(&$variables) {
$elements = [];

$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$nid = $node->id();
 }
if($node) {
$node_type = $node->getType();

 }
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();

 $ids = \Drupal::entityQuery('node')
 ->condition('type', $node_type)
 ->condition('langcode', $language)
 ->condition('nid', $nid)
 ->execute();

$nodes = Node::loadMultiple($ids);
 foreach ($nodes as $node) {
 $translation = $node->getTranslation($language);
 $elements[] = [
 'node' =>$translation->nid->value,
 'title' => $translation->title->value,
 'url' => $translation->url(),
 ];
  }

 if(empty($elements)){
$language = Drupal::languageManager()->getLanguage($language);
$url = Url::fromRoute('<front>', [], ['language' => $language]);
$response = new RedirectResponse($url->toString());
 $response->send();
  }
 }
  • Nice, what function are you call this code in ? – GiorgosK Apr 11 '18 at 14:05
  • function YOUR_MODULE_preprocess_node(&$variables) { – Vikram fz16 Apr 12 '18 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.