{"id":48926,"date":"2017-07-17T14:09:50","date_gmt":"2017-07-17T08:39:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=48926"},"modified":"2021-09-06T11:56:15","modified_gmt":"2021-09-06T06:26:15","slug":"drupal-8-frequently-used-services-functions-statements","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/drupal-8-frequently-used-services-functions-statements\/","title":{"rendered":"Drupal 8 Frequently Used Services, Functions, and Statements"},"content":{"rendered":"<p>In <a title=\"Drupal Consulting Services\" href=\"http:\/\/www.tothenew.com\/wcm\/drupal-development-consulting-services\">Drupal 8<\/a>, some important services, functions and statements are frequently used in all projects. These are time-saving statements and perform a very crucial operation. Drupal 8 have multiple statements that are very useful in the project and it is also essential for the performance &amp; Drupal 8 standard.<\/p>\n<p><strong>Drupal 8<\/strong> follow a PSR-4 standard, so Drupal 8 recommended the use of the PSR-4 standard code for the better performance. Drupal 8 provides a lot of services, statements &amp; functions that are very prominent for a big project where <a title=\"Best Practices to Optimize Performance in Drupal 8\" href=\"http:\/\/www.tothenew.com\/blog\/best-practices-to-optimize-performance-in-drupal-8\/\">performance is the priority<\/a>. We need these statements very frequently in Drupal 8 projects and we start searching on Google, which is a very time-consuming task and sometimes it is not possible when you are working offline.<\/p>\n<p>In Drupal 8 project, these statements\/services play very a vital role in performance, stability, and reusability because these are short line code and increases code reusability.<\/p>\n<p><em>In Drupal 8, a service is any object managed by the services container.<\/em><br \/>\nDrupal 8 introduces a new concept of services to <a href=\"http:\/\/www.tothenew.com\/blog\/is-your-drupal-8-ready-for-writing-codes\/\">decouple reusability<\/a> and makes these services pluggable and replaceable by registering them with a service container. It&#8217;s a best practice to use these services provided by Drupal 8. <em>The Symfony 2 documentation has a wide introduction to services.<\/em><\/p>\n<h2>Following are frequently used statements, services and functions:<\/h2>\n<p><strong>1. Drupal 8, How to get Node alias or Taxonomy Alias by Node id or Term ID?<\/strong><\/p>\n<p>[php]<br \/>\nuse Drupal\\Core\\Url;<\/p>\n<p>$options = array(&#8216;absolute&#8217; =&gt; true);<br \/>\n\/\/return node alias<br \/>\n$url = Url::fromRoute(&#8216;entity.node.canonical&#8217;, [&#8216;node&#8217; =&gt; 1234], $options);<\/p>\n<p>\/\/return taxonomy alias<br \/>\n$url = Url::fromRoute(&#8216;entity.taxonomy_term.canonical&#8217;, [&#8216;taxonomy_term&#8217; =&gt; 1234], $options);<br \/>\n[\/php]<\/p>\n<p>Sometimes we need a relative path and sometimes we need an absolute path. Drupal 8 has options parameters where you can pass true or false in an absolute key.<\/p>\n<p><strong>Parameters description:<\/strong><br \/>\n-absolute true will return absolute path.<br \/>\n-absolute false will return relative path.<\/p>\n<p><strong>2. Drupal 8, How to get Node ID or Taxonomy term ID by Drupal alias or path?<\/strong><\/p>\n<p>[php]<br \/>\n$path = \\Drupal::service(&#8216;path.alias_manager&#8217;)-&gt;getPathByAlias(&#8216;\/alias-path&#8217;);<br \/>\nif(preg_match(&#8216;\/node\\\/(\\d+)\/&#8217;, $path, $matches))<br \/>\n$nodeID = $matches[1];<\/p>\n<p>$path = \\Drupal::service(&#8216;path.alias_manager&#8217;)-&gt;getPathByAlias(&#8216;\/alias-path&#8217;);<br \/>\nif(preg_match(&#8216;\/taxonomy\\\/term\\\/(\\d+)\/&#8217;, $path, $matches)) {$termID = $matches[1];}<\/p>\n<p>$path = \\Drupal::service(&#8216;path.alias_manager&#8217;)-&gt;getPathByAlias(&#8216;\/alias-path&#8217;);<br \/>\nif(preg_match(&#8216;\/user\\\/(\\d+)\/&#8217;, $path, $matches)) {$userID = $matches[1];}<br \/>\n[\/php]<\/p>\n<p>If alias-path does not exist, it will return the same argument string.<br \/>\nThe first example will work for the node, the second for the taxonomy, and the third for the users.<\/p>\n<p><strong>3. Drupal 8, How to get the object in the twig file?<\/strong><br \/>\nIf you are looking to print how many objects have <a href=\"http:\/\/www.tothenew.com\/blog\/overview-of-twig-extentions-in-drupal-8\/\">passed into twig file<\/a>, simply add a line in your .twig file.<\/p>\n<p>[php]<br \/>\n{{ kint() }}<br \/>\n[\/php]<\/p>\n<p><strong>4. How to get the current Path in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$currentPath = \\Drupal::service(&#8216;path.current&#8217;)-&gt;getPath();<br \/>\n[\/php]<\/p>\n<p>It will return the current path in Drupal 8. For the node pages it return node\/{node id},for taxonomy taxonomy\/term\/{term id}, for user user\/{user id) if exists otherwise it will return the current request URI.<\/p>\n<p><strong>5. How to get Affected rows by Delete or Update statement in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$query = &#8220;Delete FROM table_name where id=10 &#8220;;<br \/>\n$affectedRows = $this-&gt;database-&gt;query($query,[],array(&#8216;return&#8217; =&gt; Database::RETURN_AFFECTED));<\/p>\n<p>$query = &#8220;Update table_name set couln_name=value where id=10 &#8220;;<br \/>\n$affectedRows = $this-&gt;database-&gt;query($query,[],array(&#8216;return&#8217; =&gt; Database::RETURN_AFFECTED));<br \/>\n[\/php]<\/p>\n<p>The above statements will return the number of rows affected by the SQL statement.<\/p>\n<p><strong>6. How to check whether a module is installed or not in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$moduleHandler = \\Drupal::service(&#8216;module_handler&#8217;);<br \/>\nif ($moduleHandler-&gt;moduleExists(&#8216;module_name&#8217;)){<br \/>\n\/\/echo &#8220;module installed&#8221;;} else {\/\/echo &#8220;module does not installed&#8221;;}<br \/>\n[\/php]<\/p>\n<p><strong>7. How to get Route name in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$route = \\Drupal::routeMatch()-&gt;getRouteName();<br \/>\n[\/php]<\/p>\n<p>The above statement will return the Drupal route. It returns an entity.node.canonical for the nodes, system.404 for the 404 pages, entity.taxonomy_term.canonical for the taxonomy pages, entity.user.canonical for the users, and custom route name that we define in modulename.routing.yml file.<\/p>\n<p><strong>8. How to get the current page title in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$request = \\Drupal::request();<br \/>\nif ($route = $request-&gt;attributes-&gt;get(\\Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface::ROUTE_OBJECT)) {<br \/>\n$title = \\Drupal::service(&#8216;title_resolver&#8217;)-&gt;getTitle($request, $route);}<br \/>\n[\/php]<\/p>\n<p>The above statement will return the current page title. If you use the above statement in the controller, it will also return the page title.<\/p>\n<p><strong>9. How to get the current Document root path in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$image_path = \\Drupal::service(&#8216;file_system&#8217;)-&gt;realpath();<br \/>\n[\/php]<\/p>\n<p>The above statement will return the current document root path like <em>\/var\/www\/html\/project1<\/em>.<\/p>\n<p><strong>10. How to check the Frontpage in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$is_front = \\Drupal::service(&#8216;path.matcher&#8217;)-&gt;isFrontPage();<br \/>\n[\/php]<\/p>\n<p>The above statement will return either\u00a0<em>true or false<\/em>. Return True for the Frontpage otherwise false.<\/p>\n<p><strong>11. How to uncache a particular page in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n\\Drupal::service(&#8216;page_cache_kill_switch&#8217;)-&gt;trigger();<br \/>\n[\/php]<\/p>\n<p>The above statement doesn&#8217;t keep the page in cache. You can use this statement in node_preprocess, controller, etc. with the condition. When this line is executed, the page will not go into the Drupal cache.<\/p>\n<p><strong>12. How to redirect third-party URL in Drupal 8?<\/strong><\/p>\n<p>[php]<br \/>\n$response = new \\Drupal\\Core\\Routing\\TrustedRedirectResponse($absolute_url, 300);<br \/>\n;&#8221;&gt;$response-&gt;send();<br \/>\nexit(0);<br \/>\n[\/php]<\/p>\n<p>The above statement will redirect on the third-party website with Redirect permanent status. The URL must have an absolute path like <em>http:\/\/www.tothenew.com<\/em><\/p>\n<p>All the above statements are helpful in Drupal 8, so you should bookmark this blog page to speed up the project development, maintain the Drupal 8 standard and performance that is the key requirement for any Drupal website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Drupal 8, some important services, functions and statements are frequently used in all projects. These are time-saving statements and perform a very crucial operation. Drupal 8 have multiple statements that are very useful in the project and it is also essential for the performance &amp; Drupal 8 standard. Drupal 8 follow a PSR-4 standard, [&hellip;]<\/p>\n","protected":false},"author":1038,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[3602,1],"tags":[3601,3604,3603,4638,4405],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48926"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1038"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=48926"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48926\/revisions"}],"predecessor-version":[{"id":53897,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/48926\/revisions\/53897"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=48926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=48926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=48926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}