Net Omelette Drupal 10 Upkeep and Assist Service Select your theme dynamically in 8 with theme negotiation

Have you ever ever wanted to render sure pages (or teams of pages) with a unique theme than Drupal Development Company default one configured for Drupal Development Company web site? I did. And on this article I will present you the way it’s performed in 8. And like traditional, I’ll illustrate Drupal Development Company approach utilizing a easy use case. Drupal Development requirement As an instance we now have a second theme on our web site known as gianduja since we simply love Drupal Development Company chocolate from Torino a lot. And we need to apply this theme to some customized routes (Drupal Development Company content material rendered by Drupal Development Company respective controllers just isn’t so essential for this text). How would we go about implementing this in a customized Drupal 10 module known as Gianduja? Drupal Development resolution First, we want a route possibility to differentiate these routes as needing a unique theme. We are able to name this selection _custom_theme and its worth will be Drupal Development Company machine title of Drupal Development Company theme we need to render with it. That is how a route utilizing this selection would appear like Drupal 10 Upkeep and Assist Service gianduja.information Drupal 10 Upkeep and Assist Service path Drupal 10 Upkeep and Assist Service ‘/gianduja/information’ defaults Drupal 10 Upkeep and Assist Service _controller Drupal 10 Upkeep and Assist Service ‘giandujaControllerGiandujaController Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist Serviceinfo’ _title Drupal 10 Upkeep and Assist Service ‘About Gianduja’ necessities Drupal 10 Upkeep and Assist Service _permission Drupal 10 Upkeep and Assist Service ‘entry content material’ choices Drupal 10 Upkeep and Assist Service _custom_theme Drupal 10 Upkeep and Assist Service ‘gianduja’ Only a easy route for our first information web page. You possibly can see our customized possibility at Drupal Development Company backside which signifies Drupal Development Company theme this route ought to use to render its content material in. Drupal Development Controller implementation is outdoors Drupal Development Company scope of this text. Nevertheless, simply including an possibility there will not truly do something. We have to implement a ThemeNegoatiator that appears at Drupal Development Company routes as they’re requested and switches Drupal Development Company theme if wanted. We do that by making a tagged service. So let’s create a easy class for this service inside Drupal Development Company src/Theme folder (listing/namespace not so essential) Drupal 10 Upkeep and Assist Service namespace giandujaTheme; use CoreRoutingRouteMatchInterface; use CoreThemeThemeNegotiatorInterface; /** * Our Gianduja Theme Negotiator */ class ThemeNegotiator implements ThemeNegotiatorInterface { /** * {@inheritdoc} */ public operate applies(RouteMatchInterface $route_match) { $route = $route_match->getRouteObject(); if (!$route instanceof Route) { return FALSE; } $possibility = $route->getOption(‘_custom_theme’); if (!$possibility) { return FALSE; } return $possibility == ‘gianduja’; } /** * {@inheritdoc} */ public operate determineActiveTheme(RouteMatchInterface $route_match) { return ‘gianduja’; } } As you possibly can see, all we have to do is implement Drupal Development Company ThemeNegotiatorInterface which comes with two strategies. Drupal Development first, applies(), is Drupal Development Company most essential. It’s run on every route to find out if this negotiator gives Drupal Development Company theme for it. So in our instance we study Drupal Development Company Route object and see if it has Drupal Development Company possibility we set in our route. Drupal Development second, determineActiveTheme() is liable for offering Drupal Development Company theme title for use in case applies() has returned TRUE for this route. So right here we simply return our theme title. All fairly simple. Lastly although, we have to register this class as a service in our gianduja.providers.yml file Drupal 10 Upkeep and Assist Service providers Drupal 10 Upkeep and Assist Service theme.negotiator.gianduja Drupal 10 Upkeep and Assist Service class Drupal 10 Upkeep and Assist Service giandujaThemeThemeNegotiator tags Drupal 10 Upkeep and Assist Service – { title Drupal 10 Upkeep and Assist Service theme_negotiator, precedence Drupal 10 Upkeep and Assist Service -50 } This can be a regular definition of a service, apart from Drupal Development Company incontrovertible fact that we’re making use of Drupal Development Company theme_negotiator tag to it to tell Drupal Development Company related container compiler move that we’re speaking a couple of theme negotiator occasion. Moreover, we’re additionally setting a precedence for it in order that it runs early on in Drupal Development Company theme negotiation course of. And that’s just about it. Clearing Drupal Development Company cache and hitting our new route ought to use Drupal Development Company gianduja theme if one exists and is enabled. Utilizing this instance, we will create extra complicated eventualities as effectively. For one, Drupal Development Company theme negotiator class can obtain providers from Drupal Development Company container if we simply title them in Drupal Development Company service definition. Utilizing these we will then run complicated logics to find out whether or not and which theme needs to be used on a sure route. For instance, we will take a look at a canonical route of an entity and render it with a unique theme if it has a sure taxonomy tag utilized to it. There’s various flexibility right here. Drupal 10 Growth and Assist

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

Net Omelette Drupal 10 Upkeep and Assist Service Select your theme dynamically in 8 with theme negotiation

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.