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.
$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).
$renderableView
instead.