Render a view in a given language in a customized command

I’ve written a Drupal Console command that creates an enormous batch of HTML snippets. That is executed by rendering a view programmatically (as in this StackExchange reply)

Drupal Developer process

Now I’ve to create these snippets in a number of languages.

This works

If I name Drupal Developer view in my browser I can simply selected Drupal Developer language with Drupal Developer respective path prefix:

http://www.instance.com/de/path/to/Drupal Developer/view (german model)

http://www.instance.com/en/path/to/Drupal Developer/view (english model)

This doesn’t work

If I attempt to render Drupal Developer exact same view from a Drupal Console command I’m not in a position to set Drupal Developer goal language. Drupal Developer rendered HTML is at all times in Drupal Developer default language.

At first I assumed passing Drupal Developer langcode in Drupal Developer view() technique ought to suffice:

// Now render Drupal Developer node as HTML $viewBuilder = $this->entityTypeManager->getViewBuilder('node'); $construct       = $viewBuilder->view($node, 'full', $langcode); $html        = $this->renderer->renderPlain($construct); 

Then I attempted setConfigOverrideLanguage():

$this->languageManager->setConfigOverrideLanguage($language);  // … render Drupal Developer view right here …  $this->languageManager->setConfigOverrideLanguage($originalLanguage); 

However this didn’t work both. So it tried:

$this->configFactory->getEditable('system.web site')->set('langcode', $langcode)->save();  // … render Drupal Developer view right here …  $this->configFactory->getEditable('system.web site')->set('langcode', $originalLangcode)->save(); 

However I didn’t succeed. I even tried utilizing a customized LanguageNegotiator, however I ran right into a deadly error as a consequence of Drupal Developer undeniable fact that Drupal Developer service ‘language_manager’ doesn’t implement Drupal Developer ConfigurableLanguageManagerInterface, so it’s lacking Drupal Developer setNegotiator() and getNegotiator() strategies.

As soon as once more I’m completely caught and I do not know what I’m lacking (probably only a tiny bit). How in Drupal Developer world is it potential to alter Drupal Developer language of my rendered HTML???

Edit 2017-01-05:

I’ve to confess I’m fairly determined atm. I even tried every thing I dominated out above once more. Guess what, it nonetheless doesn’t work. Any concepts? I’m so shure that I’m lacking solely a tiny piece. However I haven’t got any thought the place to seek for 🙁

Edit 2017-01-17:

Clearly Drupal Developer console command isn’t conscious of (elements of) Drupal Developer context, even when I set Drupal Developer language. Simply to get it executed I’m contemplating placing collectively a request object which I might then have been dealt with by Drupal Developer kernel. If this could transform too sophisticated I might nonetheless curl Drupal Developer respective pages, however I’ve a sense that there needs to be one other approach of doing this.

Edit 2017-03-14:

To start with there there must be a translation (even when Drupal Developer node information is by no means language particular). If you’d like Drupal Developer full context it’s a must to use Drupal Developer Kernel’s deal with technique with a sub request.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Render a view in a given language in a customized command

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.