0

I have a task of making some content of movies in admin panel in drupal

https://i.stack.imgur.com/Ut6wG.jpg

and then after that to use a custom module and controller to show those movies onto my custom twig. I am unable to find a solution or hint how to do that, maybe i even did find something but because of my small knowledge of drupal i dont know what i have to do in order to achieve that. I created a custom module, controller , routing and everything while following the hello world part of drupal documentation but after that i just frooze and i am unable to continue further

Can someone assist me on this task, will be greatly appreciated because i have been stuck on it for 2 days now

1 Answers1

0

Drupal's way of getting content is by using views. You should create a view (Structure -> Views -> Add view from admin menu) to get that data. It's something like visual interface for creating database queries, and it's not hard as it might look at first sight.

Then, there are few way how to use that view.

  1. View can create a page, or a block, so content will appear on the page
  2. You can execute view from your controller and collect data view returned and pass it to twig template to render the data.
  3. You could embed view directly from twig, so no need for any kind of coding inside page controller.

Option 1 with "page view" (it's display type is "page") is probably easiest one, but doesn't fulfill request of custom controller.

With option 2 you could use: views_get_view_result() call to get results:

https://api.drupal.org/api/drupal/core%21modules%21views%21views.module/function/views_get_view_result/8.2.x

It should look something like:

$view = Views::getView('view_machine_name');
$view->execute();
foreach ($view->result as $row) { // iterating trough rows of results
  // Do something with $row
}

This option matches the best your request.

For option 3 you would have to Twig Tweak module to embed view from twig:

https://www.drupal.org/docs/8/modules/twig-tweak/twig-tweak-and-views

Beside that you could of course create custom database query, but that's not so "drupalish" way and queries can be pretty complicated since drupal's database structure is not the simplest one and you could get lost.

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • Yes Option 2 is something similiar that i need to do, except i need to involve entity_query and entity_type.manager and use that to get the node's from my content type(movies), because later on i will hare more functions to add to controller – Eduard Pinter Jul 09 '20 at 07:50
  • Views can return almost anything so i.e. they can return entity id (node id) and you could use entity manager to load full entity by that id. – MilanG Jul 09 '20 at 13:42