This is an instance of the class \Drupal\Core\Http\InputBag and it is essentially an collection of the parameters sent to the request.Īs an example, to get all of the query parameters as an array you can call the all() method on the query property. The path returned here will also contain any language information, which is quite common on Drupal sites.įinally, if you want to find the query parameters being sent to the page then the request object contains a "query" property that contains this information. If the getRequestUri() method is called from the front page of the site then it will return "/". This will obviously depend on the page you are visiting, but assuming that the page being visited is " " then the path returned will be /some/internal/page. If you want to get the path of the current page you can use the getRequestUri() method. $fullUrl = $request->getSchemeAndHttpHost() To get the scheme and domain address together you can use getSchemeAndHttpHost(), which will combine together the above methods into a single method. To get the current scheme of the site, which will be either http or https, you can use the getScheme() method. For example, using this on a site being served through port 8080. The getHttpHost() method calls the getHost() method and then will add on the port number in use if the site happens to be served through a port other than 80 (for http) and 443 (for https). The getHost() method will just return the domain name. To get the current site domain name of the site you can use a couple of methods. Once you have access to this object you can use it to inspect the current situation on the page. $request = \Drupal::service('request_stack')->getCurrentRequest() ** \Symfony\Component\HttpFoundation\Request $request */ This will give you the access to the same object. You can also use dependency injection to inject the service 'request_stack' into any service you are using and call the getCurrentRequest() method on that service. You can do this statically, like the following. Getting the Request object is a case of just asking Drupal for it. This object is an important starting point when looking for URL information as it's a good way of getting the current path, host, or other parameters being sent to the page. As you might see from the name, this is actually a Symfony component and therefore has no knowledge about Drupal. This is an instance of \Symfony\Component\HttpFoundation\Request and can be used to tell you all about the current request. When you respond to a page request in Drupal you will have access to a Request object. After that I will look at other ways in which links and URLs can be created. The main objects I will address here are \Symfony\Component\HttpFoundation\Request, \Drupal\Core\Url and \Drupal\Core\Link, all of which have their own uses. In this article I will go through some of the main things you should be aware of when using paths and URLs in Drupal and then look at examples of using each. Creating URLs and printing out links is perhaps the most common thing that needs to be done by developers creating themes, outside of changing the classes or markup of a block of HTML. What is surprising to me is that there is very little documentation on about this. ![]() I think that's why there are so many questions asking variations of this topic on sites like Stackoverflow. You might have a node object that you need to convert into a fully qualified path, or you want to print out the path of a route, each of which have different approaches. The difficulty is that finding or printing out a URL is very contextual and there is more than one way to get or use this information in Drupal. Now what is important to remember here is that by using the "P" in the format you get the timezone hours difference convertion and this is the only worry free way to always be updated about calculating and displaying the correct date time especially if you are using this in an API call like i do.Īnother important thing to notice here is that you should import your language *.This is probably not relevant to some people, but I find that I'm always searching for this information when I need to print out a URL or find the current page path. Print $drupalDateTime->format('Y-m-d H:i:s P') Print $drupalDateTime->format('Y-m-d H:i:s') $drupalDateTime = DrupalDateTime::createFromTimestamp($row->created) Here are some examples based on a timestamp value ($row->created = 1634136039). For my case i switched to Athens - Timezone. ![]() One important thing to do before messing with the DrupalDateTime formats and functionality is to set your correct Timezone in admin/config/regional/settings. With the new DrupalDateTime Class it is easier than ever to format a date with php.
0 Comments
Leave a Reply. |