We noticed how one can write a easy service container in earlier. We will construct a tagged service now. As an example a correct use case for tagged providers, let’s contrive a state of affairs the place you add a pipeline customized filters to person textual content earlier than rendering it on the web page. First, clone the code which can be used on this put up. $ git clone git@github.com Drupal 10 Maintenance and Support ServiceDrupal 108book/process_text.git Checkout the primary model of the code the place we take customized textual content from person, course of and show it in a web page with out utilizing providers. $ cd process_text $ git checkout -f just-filtering We get customized textual content from a person utilizing a config type. class CustomTextSettingForm extends ConfigFormBase { /** * {@inheritdoc} */ protected perform getEditableConfigNames() { return [ ‘process_text.settings’, ]; } /** * {@inheritdoc} */ public perform getFormId() { return ‘custom_text_setting_form’; } /** * {@inheritdoc} */ public perform buildForm(array $type, FormStateInterface $form_state) { $config = $this->config(‘process_text.settings’); $type[‘custom_text’] = [ ‘#type’ => ‘textarea’, ‘#title’ => $this->t(‘Custom Text’), ‘#default_value’ => $config->get(‘custom_text’), ]; return mother or father Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServicebuildForm($type, $form_state); } /** * {@inheritdoc} */ public perform validateForm(array &$type, FormStateInterface $form_state) { mother or father Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServicevalidateForm($type, $form_state); } /** * {@inheritdoc} */ public perform submitForm(array &$type, FormStateInterface $form_state) { mother or father Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServicesubmitForm($type, $form_state); $this->config(‘process_text.settings’) ->set(‘custom_text’, $form_state->getValue(‘custom_text’)) ->save(); $form_state->setRedirect(‘process_text.present’); } } We put it aside as a chunk of configuration referred to as process_text.settings.custom_text. Earlier than rendering this textual content, for instance you’d need to Drupal 10 Maintenance and Support Service Take away any <div> tags. Substitute a token [greeting] with <span class”greeting”>hi there world</span> all through the textual content. We get the textual content and do all of the above processing inside a customized controller. class ProcessTextController extends ControllerBase { /** * Processtext. * * @return string * Return processed customized textual content. */ public perform processText() { $custom_text = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconfig(‘process_text.settings’)->get(‘custom_text’); // do processing // take away divs $custom_text = str_replace([“<div>”, “</div>”], “”, $custom_text); // substitute greeting tokens $custom_text = str_replace(“[greeting]”, ‘<span class=”greeting”>hi there world</span>’, $custom_text); return [ ‘#type’ => ‘markup’, ‘#markup’ => $custom_text ]; } } That is good, however we may do higher. What if we modify the filter making use of mechanism? We have now to vary this code. As a substitute, let’s convert it right into a service. $ cd process_text $ git checkout -f services-first-cut Our textual content filter service takes a set of filters and applies them to a given textual content after we name applyFilters. class TextFilterService { non-public $filters = []; /** * @param Filter $filter */ public perform addFilter(Filter $filter) { $this->filters[] = $filter; } /** * applies all filters to given textual content and returns * filtered textual content. * * @param string $txt * * @return string */ public perform applyFilters($txt) { foreach ($this->filters as $filter) { $txt = $filter->apply_filter($txt); } return $txt; } } We have to crate a providers.yml file for the above service. providers Drupal 10 Maintenance and Support Service process_text.text_filter Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service process_textTextFilterService This is how the processText perform textual content seems now. public perform processText() { $custom_text = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconfig(‘process_text.settings’)->get(‘custom_text’); // do processing utilizing a service $filter_service = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceservice(‘process_text.text_filter’); // take away divs $filter_service->addFilter(new RemoveDivs()); // substitute greeting token $filter_service->addFilter(new Greeting()); // apply all of the above filters $custom_text = $filter_service->applyFilters($custom_text); return [ ‘#type’ => ‘markup’, ‘#markup’ => $custom_text ]; } Now the filter making use of mechanism is swappable. We are able to add write a special performance and inject that implementation utilizing service containers. Now, what if we need to add a brand new filter to this code, like, enclosing the entire textual content inside a <p> tag. Positive. We may do this. Let’s checkout the precise tag the place we add a brand new filter. $ cd process_text $ git checkout -f add-new-filter We construct that filter. class EnclosePTags implements Filter { public perform apply_filter($txt) { return ‘<p>’. $txt . ‘</p>’; } } …and add it to the set of filters being utilized. public perform processText() { $custom_text = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconfig(‘process_text.settings’)->get(‘custom_text’); // do processing utilizing a service $filter_service = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceservice(‘process_text.text_filter’); // take away divs $filter_service->addFilter(new RemoveDivs()); // substitute greeting token $filter_service->addFilter(new Greeting()); // Enclose p tags $filter_service->addFilter(new EnclosePTags()); // apply all of the above filters $custom_text = $filter_service->applyFilters($custom_text); return [ ‘#type’ => ‘markup’, ‘#markup’ => $custom_text ]; } How about injecting the filter including mechanism itself? Would not it’s cool if we’re in a position so as to add new filters with out altering this a part of the code? To not point out the truth that the code can be extra testable than earlier than if we comply with this method. That is precisely what tagged providers assist us accomplish. Let’s write every filter as a tagged service. $ cd process_text $ git checkout -f tagged-services This is how our process_text.providers.yml seems now. providers Drupal 10 Maintenance and Support Service process_text.text_filter Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service process_textTextFilterService tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service service_collector, tag Drupal 10 Maintenance and Support Service text_filter, name Drupal 10 Maintenance and Support Service addFilter } remove_divs_filter Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service process_textTextFilterRemoveDivs tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service text_filter } greeting_filter Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service process_textTextFilterGreeting tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service text_filter } enclose_p_filter Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service process_textTextFilterEnclosePTags tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service text_filter } There are a lot of modifications right here. Firstly, all of the filters have been transformed to providers themselves. The have a standard tag referred to as text_filter. The principle service additionally has just a few modifications. It has a tag referred to as service_collector and a tag parameter name. This ritual of making a service container and including a set of tagged providers is such a standard sample that 8 has a particular tag to do that, referred to as the service_collector. This tag takes a further parameter referred to as name which signifies what perform needs to be referred to as within the service so as to add all of the tagged providers. What occurs is, ‘s TaggedHandlersPass picks up all providers with “service_collector” tag, finds providers which have the identical tag as that of this service(text_filter in our case) and calls the tactic in name to devour the tagged service definition. Should you’re coming from Symfony world, this may appear acquainted for you. With the intention to execute some customized code, like making use of a set of filters, we implement CompilerPassInterface, which is run each time the service cotainer(ApplyFilter in our case) is being constructed. You will discover extra about CompilerPassInterface right here. Your controller code seems rather a lot less complicated now. public perform processText() { $custom_text = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconfig(‘process_text.settings’)->get(‘custom_text’); // do processing utilizing a service $filter_service = Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceservice(‘process_text.text_filter’); $custom_text = $filter_service->applyFilters($custom_text); return [ ‘#type’ => ‘markup’, ‘#markup’ => $custom_text ]; } Now, all you have to add new filters is to replace the service yaml file with the brand new filter service and tag it with “text_filter” tag. Tagged providers within the wild permits builders so as to add a brand new authentication mechanism utilizing tagged providers. The authentication_collector is outlined in core.providers.yml. authentication_collector Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service CoreAuthenticationAuthenticationCollector tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service service_collector, tag Drupal 10 Maintenance and Support Service authentication_provider, name Drupal 10 Maintenance and Support Service addProvider } So as to add a brand new authentication supplier, one has to implement the AuthenticationProviderInterface and flesh out the applies and authenticate features. This would be the topic of one other put up. This is how the addProvider perform seems like Drupal 10 Maintenance and Support Service public perform addProvider(AuthenticationProviderInterface $supplier, $provider_id, $precedence = 0, $international = FALSE) { $this->suppliers[$provider_id] = $supplier; $this->providerOrders[$priority][$provider_id] = $supplier; // Power the suppliers to be re-sorted. $this->sortedProviders = NULL; if ($international) { $this->globalProviders[$provider_id] = TRUE; } } And here is how we might register our hypothetical authentication supplier service. providers Drupal 10 Maintenance and Support Service authentication.custom_auth Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service custom_authAuthenticationProviderCustomAuth tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service authentication_provider } One other instance is the breadcrumb supervisor service. breadcrumb Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service CoreBreadcrumbBreadcrumbManager arguments Drupal 10 Maintenance and Support Service [‘@Drupal 10 module_handler’] tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service service_collector, tag Drupal 10 Maintenance and Support Service breadcrumb_builder, name Drupal 10 Maintenance and Support Service addBuilder } So as to add breadcrumbs out of your Drupal 10 module, you would want to implement BreadcrumbBuilderInterface and add the next entry to your providers.yml, providers Drupal 10 Maintenance and Support Service foo.breadcrumb Drupal 10 Maintenance and Support Service class Drupal 10 Maintenance and Support Service fooMyBreadcrumbBuilder tags Drupal 10 Maintenance and Support Service – { identify Drupal 10 Maintenance and Support Service breadcrumb_builder, precedence Drupal 10 Maintenance and Support Service 100 } The BreadcrumbManager Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceaddBuilder collects all breadcrumb bilders and builds it utilizing the BreadcrumbManager Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconstruct perform. public perform addBuilder(BreadcrumbBuilderInterface $builder, $precedence) { $this->builders[$priority][] = $builder; // Power the builders to be re-sorted. $this->sortedBuilders = NULL; } , 8, Planet Drupal 10 Growth and Assist
Drupal 10 Support: Drupal 10 Maintenance and Support Service Tagged providers in 8

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.)
Drupal 10 Support: Drupal 10 Maintenance and Support Service Tagged providers 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.
