1

I've created a Controller to get all content teaser of a specific type and in a specific language.

namespace Drupal\xxxxxx\Controller;

use Drupal\Core\Controller\ControllerBase;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

use Drupal\views\Views;

class ContentTeaserController extends ControllerBase {

public function getTeaser(Request $request) {
    $params = [];

    $quantity = $request->query->get('quantity');
    $nodetypes = $request->query->get('nodetypes');
    $tags = $request->query->get('tags');
    $style = $request->query->get('style');
    $page = $request->query->get('page');
    $language = $request->query->get('language'); // <---

    $view = Views::getView('contentteaser');
    $view->setDisplay($style);
    $view->setItemsPerPage($quantity);
    $view->setOffset($page * $quantity);

    $arguments = [
        (($tags) ? $tags : '*'),
        (($nodetypes) ? $nodetypes : null),
        (($language) ? $language : 'en'), // <---
    ];
    $view->setArguments($arguments);

    $view->execute();
    $result = $view->result;


    $renderableView = $view->buildRenderable(null, array('language' => $arguments[2]), false);



    $renderableView['#embed'] = TRUE;
    $content = \Drupal::service('renderer')->render($renderableView);

    $response = new Response();
    $response->setContent($content);
    return $response;
  }

}

I only added the two lines marked by // <---.

So when I call the route i configured for this controller, I only get the results in German.

I also tried to set the arguments statically by replacing the $arguments array by

$arguments = [
        #(($tags) ? $tags : '*'),
        #(($nodetypes) ? $nodetypes : null),
        #(($language) ? $language : 'en'),
        '*',
        'employee_article+event+news',
        'ru',
    ];

I must add; When I'm configuring the view in the Drupal backend and use the live preview at the bottom of the page, everything works fine.

My thought is, that it's the renderer that doesn't work properly.

This is the configuration of the View.

Basic configuration

Proof that the view works well

9
  • Any reason you are using a regular Symfony response over returning a build array?
    – Kevin
    Commented Apr 30, 2018 at 13:14
  • Because I get the following error when I try to return $content: LogicException: The controller must return a response (Object(Drupal\Core\Render\Markup) given). in Symfony\Component\HttpKernel\HttpKernel->handleRaw() (line 162 of vendor/symfony/http-kernel/HttpKernel.php). Commented Apr 30, 2018 at 13:17
  • You shouldn't do what the error message suggests, see drupal.stackexchange.com/a/199855, return the unrendered $renderableView instead.
    – 4uk4
    Commented Apr 30, 2018 at 14:03
  • Thank you @4k4 but I still get the result in german. Commented Apr 30, 2018 at 14:18
  • It's hard to tell, you have a lot of details about the controller, this is the reason the comments are about what is wrong there, but you don't provide any details about the view.
    – 4uk4
    Commented Apr 30, 2018 at 15:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.