Drupal 10 Support: Drupal 10 Maintenance and Support Service Routing and controllers in 8

The routing system of 8 is an entire rewrite of 7 and its earlier variations’ hook_menu. A route is a URL path with a particular return content material. This return content material is often specified as a technique of a controller class which returns a render array. The routing system is powered by Symfony’s HTTP Kernel element, which we’ll revisit later. It isn’t essential to grasp this so as to work with routes. Let’s dive immediately and create a brand new route. First, we will create a Drupal 10 module to carry all of the code on this chapter. You may checkout the code Drupal 10 Maintenance and Support Service $ git clone git@github.com Drupal 10 Maintenance and Support ServiceDrupal 108book/myroute.git $ cd myroute $ git checkout -f simple-route or stroll together with me by typing the code or utilizing Drupal 10 console to generate it. $ Drupal 10 generate Drupal 10 Maintenance and Support ServiceDrupal 10 module Enter the brand new Drupal 10 module title Drupal 10 Maintenance and Support Service > myroute Enter the Drupal 10 module machine title [myroute] Drupal 10 Maintenance and Support Service > Enter the Drupal 10 module Path [/Drupal 10 modules/custom] Drupal 10 Maintenance and Support Service > Enter Drupal 10 module description [My Awesome Drupal 10 Support] Drupal 10 Maintenance and Support Service > Routing and controllers Enter bundle title [Custom] Drupal 10 Maintenance and Support Service > D8MD Enter Core model [8.x] Drupal 10 Maintenance and Support Service > Do you wish to generate a .Drupal 10 module file (sure/no) [yes] Drupal 10 Maintenance and Support Service > no Outline Drupal 10 module as characteristic (sure/no) [no] Drupal 10 Maintenance and Support Service > Do you wish to add a composer.json file to your Drupal 10 module (sure/no) [yes] Drupal 10 Maintenance and Support Service > no Would you want so as to add Drupal 10 module dependencies (sure/no) [no] Drupal 10 Maintenance and Support Service > Do you affirm technology? (sure/no) [yes] Drupal 10 Maintenance and Support Service > Generated or up to date information Website path Drupal 10 Maintenance and Support Service /var/www/html 1 – Drupal 10 modules/{custom}/myroute/myroute.data.yml Let’s write a easy controller which prints “howdy world” after we hit the trail /howdy. $ Drupal 10 generate Drupal 10 Maintenance and Support Servicecontroller Enter the Drupal 10 module title [email_management] Drupal 10 Maintenance and Support Service > myroute Enter the Controller class title [DefaultController] Drupal 10 Maintenance and Support Service > HelloWorldController Enter the Controller technique title (to cease including extra strategies, go away this empty) [ ] Drupal 10 Maintenance and Support Service > Good day World Enter the motion technique title [hello] Drupal 10 Maintenance and Support Service > Enter the route path [/myroute/hello/{name}] Drupal 10 Maintenance and Support Service > howdy Enter the Controller technique title (to cease including extra strategies, go away this empty) [ ] Drupal 10 Maintenance and Support Service > Do you wish to generate a unit take a look at class (sure/no) [yes] Drupal 10 Maintenance and Support Service > no Do you wish to load companies from the container (sure/no) [no] Drupal 10 Maintenance and Support Service > no Do you affirm technology? (sure/no) [yes] Drupal 10 Maintenance and Support Service > Generated or up to date information Website path Drupal 10 Maintenance and Support Service /var/www/html 1 – Drupal 10 modules/{custom}/myroute/src/Controller/HelloWorldController.php 2 – Drupal 10 modules/{custom}/myroute/myroute.routing.yml // router Drupal 10 Maintenance and Support Servicerebuild Rebuilding routes, wait a second please [OK] Completed rebuilding route(s). Be sure to allow the Drupal 10 module. $ drush en myroute -y Open Drupal 10 modules/{custom}/myroute/src/Controller/HelloWorldController.php and alter the markup textual content to “Good day World”. public operate howdy() { return [ ‘#type’ => ‘markup’, ‘#markup’ => $this->t(‘Hello World’) ]; } you may must rebuild cache. $ drush cr Hit /howdy. The equal 7 code for this is able to be one thing on the strains of // inside myroute.Drupal 10 module… operate myroute_menu() { $gadgets = array(); $gadgets[‘main’] = array( ‘title’ => Good day World’, ‘web page callback’ => myroute_hello’, ‘entry arguments’ => array(‘entry content material’), ‘kind’ => MENU_NORMAL_ITEM, ‘file’ => ‘myroute.pages.inc’ ); return $gadgets; } // inside myroute.pages.inc operate myroute_hello() { return t(‘Good day World’); } Routes with parameters That is nice, however learn how to add URL parameters? Let’s add a brand new route with a URL parameter. $ cd myroute $ git checkout -f route-with-params or for those who select to make use of Drupal 10 console, Drupal 10 generate Drupal 10 Maintenance and Support Servicecontroller Enter the Drupal 10 module title [email_management] Drupal 10 Maintenance and Support Service > myroute Enter the Controller class title [DefaultController] Drupal 10 Maintenance and Support Service > GreetingController Enter the Controller technique title (to cease including extra strategies, go away this empty) [ ] Drupal 10 Maintenance and Support Service > Greeting Enter the motion technique title [hello] Drupal 10 Maintenance and Support Service > greeting Enter the route path [/myroute/hello/{name}] Drupal 10 Maintenance and Support Service > howdy/{title} Enter the Controller technique title (to cease including extra strategies, go away this empty) [ ] Drupal 10 Maintenance and Support Service > Do you wish to generate a unit take a look at class (sure/no) [yes] Drupal 10 Maintenance and Support Service > no Do you wish to load companies from the container (sure/no) [no] Drupal 10 Maintenance and Support Service > Do you affirm technology? (sure/no) [yes] Drupal 10 Maintenance and Support Service > Generated or up to date information Website path Drupal 10 Maintenance and Support Service /var/www/html 1 – Drupal 10 modules/{custom}/myroute/src/Controller/GreetingController.php 2 – Drupal 10 modules/{custom}/myroute/myroute.routing.yml // router Drupal 10 Maintenance and Support Servicerebuild Rebuilding routes, wait a second please [OK] Completed rebuilding route(s). Edit the greeting controller operate to print the title. public operate greeting($title) { return [ ‘#type’ => ‘markup’, ‘#markup’ => $this->t(‘Hello, @name!’, [‘@name’ => $name]), ]; } Strive /howdy/Joe. /howdy/123-456 works too. Let’s tone it down just a little and add a validation standards for the parameter. Names can solely have alphabets and areas. myroute.greeting_controller_greeting Drupal 10 Maintenance and Support Service path Drupal 10 Maintenance and Support Service ‘howdy/{title}’ defaults Drupal 10 Maintenance and Support Service _controller Drupal 10 Maintenance and Support Service ‘myrouteControllerGreetingController Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Servicegreeting’ _title Drupal 10 Maintenance and Support Service ‘Greeting’ necessities Drupal 10 Maintenance and Support Service _permission Drupal 10 Maintenance and Support Service ‘entry content material’ title Drupal 10 Maintenance and Support Service ‘[a-zA-z ]+’ Rebuild the cache for this to take impact. $ drush cr /howdy/123-456 will now give a 404. Strive /howdy/Joepercent20Pesci. Customized permissions How about including {custom} permissions? What if we wish to present the /howdy web page solely to customers who can administer content material. $ cd myroute $ git checkout -f custom-access-check We first point out that the route makes use of {custom} permissions. myroute.hello_world_controller_hello Drupal 10 Maintenance and Support Service path Drupal 10 Maintenance and Support Service ‘howdy’ defaults Drupal 10 Maintenance and Support Service _controller Drupal 10 Maintenance and Support Service ‘myrouteControllerHelloWorldController Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Servicehowdy’ _title Drupal 10 Maintenance and Support Service ‘Good day World’ necessities Drupal 10 Maintenance and Support Service _custom_access Drupal 10 Maintenance and Support Service ‘myrouteControllerHelloWorldController Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Servicecustom_access_check’ Word that the _permission parameter has been changed by _custom_access parameter. Lets implement this practice entry technique contained in the HelloWorldController. // add the required namespaces on the prime. use CoreSessionAccountInterface; use CoreAccessAccessResult; /** * Customized entry verify * * @param CoreSessionAccountInterface $account * entry checking finished towards this account. */ public operate custom_access_check(AccountInterface $account) { return AccessResult Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceallowedIf($account->hasPermission(‘entry content material’) && $account->hasPermission(‘administer content material’)); } Rebuild cache and take a look at hitting /howdy as an anon consumer, it’s best to get an “Entry denied” web page. Of notice right here is the AccessResult class, which was launched to make entry verify associated information cacheable. Dynamic routes Defining routes through a YAML file applies to static paths. If we wish to route programmatically, now we have to outline and return a SymfonyComponentRoutingRoute object. $ cd myroute $ git checkout -f dynamic-routes For example the idea of dynamic routes, let’s take an imaginary requirement the place now we have a {custom} path, /content_types/{content_type}, which is able to show all of the fields of a content material kind {content_type}. As a way to create a dynamic route for a similar, now we have to create our personal Routing class contained in the src/Routing listing and override the routes() technique. That is equal to its YAML cousin, the myroute.routing.yml file, however written in PHP. <?php /** * @file * Accommodates myrouteRoutingCTRoutes. */ namespace myrouteRouting; use SymfonyComponentRoutingRoute; /** * Dynamic routes for content material sorts. */ class CTRoutes { /** * {@inheritdoc} */ public operate routes() { $routes = []; $content_types = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceservice(‘entity.supervisor’)->getStorage(‘node_type’)->loadMultiple(); foreach ($content_types as $content_type) { $routes[‘myroute.content_type_controller.’ . $content_type->id() ] = new Route( ‘/content_types/’ . $content_type->id(), array( ‘_controller’ => ‘myrouteControllerCTController Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Servicefields’, ‘_title’ => ‘Discipline data for ‘ . $content_type->label(), ‘content_type’ => $content_type->id(), ), array( ‘_permission’ => ‘entry content material’, ) ); } return $routes; } } The {custom} router above creates an array of routes for each content material kind within the system and returns it. As an example, if there are 3 content material sorts within the system, like web page, foo and bar, there will probably be 3 routes with paths /content_types/web page, /content_types/foo and /content_types/bar respectively. Let’s flesh out the CTController as nicely. <?php namespace myrouteController; use CoreControllerControllerBase; /** * Class CTController. * * @bundle myrouteController */ class CTController extends ControllerBase { /** * Record fields data of a content material kind. * * @return string * Return area listing. */ public operate fields($content_type) { $render = ‘<desk><tr><th>’ . $this->t(‘Discipline kind’) . ‘</th><th>’ . $this->t(‘Label’) . ‘</th></tr>’; $field_definitions = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceentityManager()->getFieldDefinitions(‘node’, $content_type); foreach ($field_definitions as $field_name => $field_definition) { if (!empty($field_definition->getTargetBundle())) { $render .= ‘<tr><td>’ . $field_definition->getType() . ‘</td><td>’ . $field_definition->getLabel() . ‘</td></tr>’; } } $render .= ‘</desk>’; return [ ‘#type’ => ‘markup’, ‘#markup’ => $render, ]; } } The sphere() technique seems to be up the sphere definitions of the contenttype argument and renders it as a HTML desk. Lastly, now we have to point to select up the {custom} Routing class we have added. This may be finished by including a _route_callbacks merchandise to the routing YAML file. route_callbacks Drupal 10 Maintenance and Support Service – ‘myrouteRoutingCTRoutes Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceroutes’ Rebuild the cache and you might be all set. Hit the content_types/article web page, it’s best to see one thing like this(offered you’ve gotten the article CT in your system). 8 Planet Drupal 10 Improvement and Help

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal 10 Support: Drupal 10 Maintenance and Support Service Routing and controllers in 8

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.