This course is archived!
This tutorial is built using Drupal 8.0. The fundamental concepts of Drupal 8 - like services & routing - are still valid, but newer versions of Drupal *do* have major differences.
02.
This course is archived!
This tutorial is built using Drupal 8.0. The fundamental concepts of Drupal 8 - like services & routing - are still valid, but newer versions of Drupal *do* have major differences.
Whoops, an error! Please, try again later.
15 Comments
I'm running this course on Drupal 9 (I know).
It works so far ...
In case anyone else is doing this, when updating
dino_roar.info.ymlyou need to changecore: 8.xto
core_version_requirement: ^8 || ^9Hey MonkeyBrain,
Thank you for this tip!
Cheers!
ok, thanks.
I would like get back to your code.
public function roar()
{
return new Response('ROOOOOAR!');
}
How can you retrieve or showing data on your "path: /the/dino/says" routing. "You are just returning a t('text') Response, but that's it.
For instance, in a project I have (doesn't apply to your project, because the code is different)
$content .= "<h1>Hello</h1>";
return array(
'#type' => 'markup'
'#markup' => $content,
),
How do you do that "new Response"?
I have used return new Response::nameofmyobject(); Doesn't work.
Does the data need to be json before you call it?
Thanks,
Hey Viktor,
Symfony HttpFoundation Component provides you a few response objects that extend the base Response object. You may read more about them in the docs: https://symfony.com/doc/current/components/http_foundation.html#response . So, regarding to your question, if you want to return JSON response - you can use JsonResponse object: https://symfony.com/doc/current/components/http_foundation.html#creating-a-json-response . And nope, in this case you even don't need to json_encode() data, the JsonResponse object will do it for you, just pass data as array and that's it:
But keep in mind that headers will be automatically set to "application/json" with it.
Cheers!
I'm sorry, one more question. If you are returning json_Response. You data is already in json:
For instance,
return new JsonResponse(
[
'data' => $this->mergeTwoArraysObjects(),
'method' => 'GET',
]
);
Returns
data:
0:
3089 "Abiu"
3090 "Cherry"
1:
0:
id: "3089"
title: "This is a title"
body: "This is my body"
Ok, this is good, but I want to know how to display that data in a website format. Do I need to use twig or I can just do it inside of Controller.php file? How to call that GET to display each of the variables I have?
Such as,
Abiu
Title: This is a title
More info: This is my body
-------
Cherry
Title: This is a title
More info: This is my body
And so on...
I was trying to do this, but it didn't works
This is part of a function from my Controller.php file
foreach ($ajax_response as $key1 => $finalValues) {
if ($key1 == 0) {
foreach ($finalValues as $disCrop) {
$content .= "<details>";
$content .= '<summary>' . $disCrop . '</summary>';
}
}
$content .= "</details>";
}
return new JsonResponse([
'#type' => 'markup',
'#markup' => $content,
]);
I'm sorry, I'm new in drupal 8. And, thank you for taking all this time.
Hey Viktor,
Ah, the question is why do you need Json response? :) Json responses usually are used for API endpoints, like when users send AJAX request and server returns AJAX response back, so you can then use the information in the response e.g. put it on the page or just update the page with new information, etc. But if you want to render a template - then you probably don't want JsonResponse, I probably misunderstood your first question.
I think to render a template instead you can do something like this:
And I suppose your template name should be "my-template.html.twig" in this case.
Cheers!
How about displaying real data from nodes
Hey Viktor!
That's outside the scope of this tutorial, as we're just covering the new services, routing & dependency injection type of stuff :). But I can point you at one cool feature, which is related to this: parameter upcasting: https://www.drupal.org/docs... - this is one way to get access to a Node from your controller, so that you can do something with it.
Cheers!
hi Ryan et all,
is it possible to annotate routes a' la symfony? I hate routing files and much prefer everything to be together.. as we have a symfony core is there a way of achieving this without too much pain?
cheers
Matt
Yo Matt!
As far as I can tell... surprisingly... this doesn't seem to be supported, or even too easily added into the core of Drupal! Part of the problem is that, in Symfony, some of the @Route functionality comes from SensioFrameworkExtraBundle - i.e. a *Bundle*... not actually from the core of Symfony, which is shared by Drupal.
So... it depends on how you define "without too much pain" :). And actually, it would be kind of fun to try out. Here is how (I think) it would work:
1) Obviously, start by adding some annotations to a controller. Use this class for the use statement: https://github.com/symfony/...
2) Create a subscriber on RoutingEvents::ALTER - as described here: https://www.drupal.org/docs...
3) In alterRoutes, instead of altering routes, we'll be adding new ones! How? Basically, you'll create a new instance of this class: https://github.com/doctrine... - then you'll need to do a little bit of magic to (A) scan for controller classes and (B) get a list of public methods via reflection and then use getMethodAnnotations to read them. What you're looking for is any Route annotations that you added. You can use these to manually create real Route objects (these: https://github.com/symfony/... and add those to the collection. I realize that I just smashed 5 big steps into one here... but I thought I'd get you started... IF you're even interested.
Honestly, this would make for a really good mini-screencast! Because it's totally possible! But, you need to understand a bit about how things work to make it happen. Anyways, if you actually want to make this happen and have some questions, let me know!
Cheers!
Cool, I had a little play about over the weekend but ran out of time. I'm going to give it a try, out of pure bloody mindedness if nothing else!
I'll keep you posted.
Cheers
Matt
Awesome :). If you make some progress, I'd love to hear about it!
The concept that custom pages are modules is new to me. I've been developing drupal sites since 4.7 and never heard that before. Is that a new concept in D8?
Pretty sure this was just intended to be a really simple demo module. You can still create pages as you always have in the UI.
Hey guys!
Yes, you can of course create nodes in the admin interface like always. By "custom pages", I mean creating custom paths for custom interactions - not just for reading node content. For example, you could build an entire mini-application inside Drupal using routes that has *nothing* to do with the CMS - e.g. a set of pages where uses can send and receive messages or a set of pages containing a multi-step form for creating an "order" of some sort.
Cheers!
"Houston: no signs of life"
Start the conversation!