I wrote service to get all necessary data for my page. Now I wanted to load them with translations. But whey I try, I got error:

InvalidArgumentException: Invalid translation language (id of language here) specified.

When I open page in default language it works fine. Does it mean that I need to translate all of the things in order to make it work?

My service:

namespace service;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use entity\Entity;

class PostTypeService
{
    public static function getEntititiesByPostType($postType, $entities = array(), $nid = NULL)
    {

        if ($nid != NULL) {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->condition('nid', $nid)
                ->execute();
        } else {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->execute();

        }

        $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
        $returnValues = array();
        $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
        foreach ($nodes as $index => $node) {
            foreach ($entities as $entity) {
                $type = $entity->type;
                $entity_name = $entity->entity;
                if ($type == 'image') {
                    $returnValues[$postType][$index][$entity->entity] = file_create_url($node->$entity_name->entity->getFileUri());
                } else if ($type == 'url') {
                    $returnValues[$postType][$index][$entity->entity . '_uri'] = $node->$entity_name->uri;
                    $returnValues[$postType][$index][$entity->entity . '_title'] = $node->$entity_name->title;
                } else if ($type == 'list') {
                    foreach ($node->$entity_name as $list_item) {
                        $returnValues[$postType][$index][$entity->entity][] = $list_item->value;
                    }
                } else {
                    $translation = $node->getTranslation($language);
                    $returnValues[$postType][$index][$entity->entity] = $translation->$entity_name->$type;
                }
            }
            $returnValues[$postType][$index]['node_id'] = $index;
            $options = array('absolute' => TRUE);
            $url = Url::fromRoute('entity.node.canonical', ['node' => $index], $options)->toString();
            $returnValues[$postType][$index]['node_url'] = $url;
            $returnValues[$postType][$index]['lang'] = $language;
        }
        return $returnValues;
    }
  • I just checked and I was right. The problem is that not all of the content is translated – El Danielo Jun 4 '16 at 14:45
  • Enable error log with verbose information, check the backtrace. Core should always fall back, you might have custom or contrib code that forces the current language and doesn't check if it exists. – Berdir Jun 4 '16 at 15:31
  • 1
    Yes, you are right. I fixed it with this code: if (!array_key_exists($language, $node->getTranslationLanguages())) { $translation = $node->getTranslation($defaultLang); } else { $translation = $node->getTranslation($language); } – El Danielo Jun 4 '16 at 15:36
  • You should add your own answer. – mradcliffe Jun 4 '16 at 15:43
  • You can use hasTranslation(), but actually, you probably want to use getTranslationFromContext. api.drupal.org/api/drupal/… – Berdir Jun 4 '16 at 15:47

This solves the problem:


namespace service;

use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use entity\Entity;

class PostTypeService
{
    public static function getEntititiesByPostType($postType, $entities = array(), $nid = NULL)
    {



        if ($nid != NULL) {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->condition('nid', $nid)
                ->execute();
        } else {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->execute();

        }

        $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);


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

        $returnValues = array();
        foreach ($nodes as $index => $node) {

            if (!array_key_exists($language, $node->getTranslationLanguages())) {
                $translation = $node->getTranslation($defaultLang);
            } else {
                $translation = $node->getTranslation($language);
            }

            foreach ($entities as $entity) {
                $type = $entity->type;
                $entity_name = $entity->entity;
                if ($type == 'image') {
                    $returnValues[$postType][$index][$entity->entity] = file_create_url($translation->$entity_name->entity->getFileUri());
                } else if ($type == 'url') {
                    $returnValues[$postType][$index][$entity->entity . '_uri'] = $translation->$entity_name->uri;
                    $returnValues[$postType][$index][$entity->entity . '_title'] = $translation->$entity_name->title;
                } else if ($type == 'list') {
                    foreach ($translation->$entity_name as $list_item) {
                        $returnValues[$postType][$index][$entity->entity][] = $list_item->value;
                    }
                } else {
                    $returnValues[$postType][$index][$entity->entity] = $translation->$entity_name->$type;
                }
            }
            $returnValues[$postType][$index]['node_id'] = $index;
            $options = array('absolute' => TRUE);
            $url = Url::fromRoute('entity.node.canonical', ['node' => $index], $options)->toString();
            $returnValues[$postType][$index]['node_url'] = $url;
            $returnValues[$postType][$index]['lang'] = $language;
        }
        return $returnValues;
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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