Drupal 10 Help: Drupal 10 Upkeep and Help Service Serialization Step-by-Step

In my earlier article about Drupal Development Company serializer element, I touched on Drupal Development Company fundamental ideas concerned when serializing an object. To summarize, serialization is Drupal Development Company mixture of encoding and normalization. Normalizers simplify advanced objects, like Person or ComplexDataDefinition. Denormalizers carry out Drupal Development Company reverse operation. Utilizing a structured array of knowledge, they generate advanced objects like Drupal Development Company ones listed above. On this article, I’ll give attention to Drupal Development Company integration of Drupal Development Company Symfony serializer element. For this, I’ll information you step-by-step via a Drupal 10 module I created for example. You could find Drupal Development Company Drupal 10 module at https Drupal 10 Upkeep and Help Service//github.com/e0ipso/entity_markdown. I’ve created a special commit for every step of Drupal Development Company course of, and this text features a hyperlink to Drupal Development Company code in every step at Drupal Development Company starting of every part. Nonetheless, you need to use GitHub UI to browse Drupal Development Company code at any time and see Drupal Development Company diff. undefined When this Drupal 10 module is completed, it is possible for you to to rework any content material entity right into a Markdown illustration of it. Rendering a content material entity with Markdown is perhaps helpful should you wished to ship an e mail abstract of a node, for example, however Drupal Development Company actual motivation is to indicate how serialization will be necessary outdoors Drupal Development Company context of internet companies. Add a brand new normalizer service These are Drupal Development Company adjustments for this step. You may browse Drupal Development Company state of Drupal Development Company code at this step in right here. Symfony’s serializer element begins with an inventory of normalizer courses. At any time when an object must be normalized or serialized Drupal Development Company serializer will loop via Drupal Development Company accessible normalizers to search out one which declares support for Drupal Development Company sort of object at hand, in our case a content material entity. If you wish to add a category to Drupal Development Company record of eligible normalizers you must create a brand new tagged service. A tagged service is only a common class definition that comes with an entry in Drupal Development Company myDrupal 10 module.companies.yml so Drupal Development Company service container can discover it and instantiate it at any time when applicable. For a service to be a tagged as a service you must add a tags property with a reputation. You can even add a precedence integer to convey priority with respect to companies tagged with Drupal Development Company similar identify. For a normalizer to be acknowledged by Drupal Development Company serialization Drupal 10 module, you must add Drupal Development Company tag identify normalizer. When core compiles Drupal Development Company service container, our newly created tagged service will probably be added to Drupal Development Company serializer record in what it’s known as a compiler move. That is Drupal Development Company place in core the place that occurs. Drupal Development Service service container is then cached for efficiency causes. That’s the reason you must clear your caches once you add a brand new normalizer. Our normalizer is an empty class at Drupal Development Company second. We are going to repair that in a second. First, we have to flip our consideration to a different assortment of companies that must be added to Drupal Development Company serializer, Drupal Development Company encoders. Embrace Drupal Development Company encoder for Drupal Development Company Markdown format These are Drupal Development Company adjustments for this step. You may browse Drupal Development Company state of Drupal Development Company code at this step in right here. Equally to a normalizer, Drupal Development Company encoder can be added to Drupal Development Company serialization system through a tagged service. It’s essential that this service implements `EncoderInterface`. Be aware that at this stage, Drupal Development Company encoder doesn’t comprise its most necessary methodology encode(). Nonetheless, you possibly can see that it comprises supportsEncoding(). When Drupal Development Company serializer element must encode an structured array, it can check all Drupal Development Company encoders accessible (these tagged companies) by executing supportsEncoding() and passing Drupal Development Company format specified by Drupal Development Company person. In our case, if Drupal Development Company person specifies Drupal Development Company ‘markdown’ format. Then, our encoder will probably be used to rework Drupal Development Company structured array right into a string, as a result of supportsEncoding() will return TRUE. To do Drupal Development Company precise encoding it can use Drupal Development Company encode() methodology. We are going to write that methodology later. First, let me describe Drupal Development Company normalization course of. Normalize content material entities Drupal Development Service normalization will differ every time. It is dependent upon Drupal Development Company format you need to flip your objects into, and it is dependent upon Drupal Development Company sort of objects you need to remodel. In our instance, we need to flip a content material entity right into a Markdown doc. For that to occur, Drupal Development Company serializer will want to have the ability to Drupal 10 Upkeep and Help Service Know when to make use of our normalizer class. Normalize Drupal Development Company content material entity. Normalize any discipline in Drupal Development Company content material entity. Normalize all Drupal Development Company properties in each discipline. Uncover our customized normalizer These are Drupal Development Company adjustments for this step. You may browse Drupal Development Company state of Drupal Development Company code at this step in right here. For a normalizer to be thought-about a superb match for a given object it wants to fulfill two situations Drupal 10 Upkeep and Help Service Implement Drupal Development Company `NormalizerInterface`. Return `TRUE` when calling `supportsNormalization()` with Drupal Development Company object to normalize and Drupal Development Company format to normalize to. Drupal Development Service course of is sort of Drupal Development Company similar as Drupal Development Company one we used to find out which encoder to make use of. Drupal Development Service essential distinction is that we additionally move Drupal Development Company object to normalize to Drupal Development Company supportsNormalization() methodology. That could be a crucial half since it is rather frequent to have a number of normalizers for Drupal Development Company similar format, relying on Drupal Development Company sort of object that must be normalized. A Node object could have completely different code that turns it into an structured array when in comparison with an HttpException. We take that under consideration in our instance by checking if Drupal Development Company object being normalized is an occasion of a ContentEntityInterface. Normalize Drupal Development Company content material entity These are Drupal Development Company adjustments for this step. You may browse Drupal Development Company state of Drupal Development Company code at this step in right here. This step comprises a primary try to normalize Drupal Development Company content material entity that will get handed as an argument to Drupal Development Company normalize() methodology into our normalizer. Think about that our necessities are that Drupal Development Company ensuing markdown doc wants to incorporate an introductory part with Drupal Development Company entity label, entity sort, bundle and language. After that, we’d like an inventory with all Drupal Development Company discipline names and Drupal Development Company values of their properties. For instance, Drupal Development Company physique discipline of a node will end in Drupal Development Company identify field_body and Drupal Development Company values for format, abstract and worth. Along with that any discipline will be single or multivalue, so we’ll take that into consideration. To satisfy these necessities, I’ve written a bunch of code that offers with Drupal Development Company particular use case of normalizing a content material entity into an structured array able to be encoded into Markdown. I don’t suppose that Drupal Development Company particular code is related to elucidate how normalization works, however I’ve added code feedback that will help you comply with my logic. You could have noticed Drupal Development Company presence of a helper methodology known as normalizeFieldItemValue() and a remark that claims Now remodel Drupal Development Company discipline right into a string model of it. These two are large crimson flags suggesting that our normalizer is doing greater than it ought to, and that it’s implicitly normalizing objects that aren’t of sort ContentEntityInterface however of sort FieldItemListInterface and FieldItemInterface. In Drupal Development Company subsequent part we’ll refactor Drupal Development Company code in ContentEntityNormalizer to defer that implicit normalization to Drupal Development Company serializer. Recursive normalization These are Drupal Development Company adjustments for this step. You may browse Drupal Development Company state of Drupal Development Company code at this step in right here. When Drupal Development Company serializer is initialized with Drupal Development Company record of normalizers, for each it checks in the event that they implement SerializerAwareInterface. For Drupal Development Company ones that do, Drupal Development Company serializer provides a reference to itself into them. That manner you possibly can serialize/normalize nested objects throughout Drupal Development Company normalization course of. You may see how our ContentEntityNormalizer extends from SerializerAwareNormalizer, which implements Drupal Development Company aforementioned interface. Drupal Development Service sensible affect of that’s that we will use $this->serializer->normalize() from inside our ContentEntityNormalizer. We are going to use that to normalize all Drupal Development Company discipline lists in Drupal Development Company entity and Drupal Development Company discipline objects within these. First flip your focus to Drupal Development Company new model of Drupal Development Company ContentEntityNormalizer. You may see how Drupal Development Company normalizer is split into elements which might be particular to Drupal Development Company entity, like Drupal Development Company label, Drupal Development Company entity sort, Drupal Development Company bundle, and Drupal Development Company language. Drupal Development Service normalization for every discipline merchandise record is now completed in a single line Drupal 10 Upkeep and Help Service $this->serializer->normalize($field_item_list, $format, $context);.  We have now decreased Drupal Development Company LOC to nearly half, and Drupal Development Company cyclomatic complexity of Drupal Development Company class even additional. This has an important affect on Drupal Development Company maintainability of Drupal Development Company code. All this code has now been moved to 2 completely different normalizers Drupal 10 Upkeep and Help Service FieldItemListNormalizer comprises Drupal Development Company code that offers with normalizing single and multivalue fields. It makes use of Drupal Development Company serializer to normalize every particular person discipline merchandise. FieldItemNormalizer comprises Drupal Development Company code that normalizes Drupal Development Company particular person discipline objects values and their properties/columns. You may see that for Drupal Development Company serializer to have the ability to acknowledge our new `FieldListItemNormalizer` and `FieldItemNormalizer` objects we have to add them to Drupal Development Company service container, similar to we did for Drupal Development Company ContentEntityIterface normalizer. A really good facet impact of this refactor, along with Drupal Development Company maintainability enchancment, is {that a} third celebration Drupal 10 module can construct upon our code extra simply. Think about that this third celebration Drupal 10 module needs to make all discipline labels daring. Earlier than Drupal Development Company refactor they would wish to introduce a normalizer for content material entities—and play with Drupal Development Company service precedence so it will get chosen earlier than ours. That normalizer would comprise a giant copy and paste of a giant blob of code so as to have the ability to make Drupal Development Company desired tweaks. After Drupal Development Company refactor, our third celebration would solely have to have a normalizer for Drupal Development Company discipline merchandise record (which outputs Drupal Development Company discipline label) with extra precedence than ours. That could be a nice win for extensibility. Implement Drupal Development Company encoder As we stated above Drupal Development Company most necessary a part of Drupal Development Company encoder is encapsulated in Drupal Development Company `encode()` methodology. That’s Drupal Development Company methodology in control of turning Drupal Development Company structured array from Drupal Development Company normalization course of right into a string. In our explicit case we deal with every entry of Drupal Development Company normalized array as a line in Drupal Development Company output, then we append any suffix or prefix that will apply. Additional development At this level Drupal Development Company Entity Markdown Drupal 10 module is able to take any entity and switch it right into a Markdown doc. Drupal Development Service solely query is easy methods to execute Drupal Development Company serializer. If you wish to execute it programmatically you solely want do Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceservice(‘serializer’)->serialize(Node Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help Serviceload(1), ‘markdown’); Nonetheless there are different choices. You might declare a REST format like Drupal Development Company HAL Drupal 10 module so you may make an HTTP request to http Drupal 10 Upkeep and Help Service//instance.org/node/1?_format=markdown and get a Markdown illustration of Drupal Development Company node in response (after configuring Drupal Development Company corresponding REST settings). Conclusion Drupal Development Service serialization system is a strong device that permits you to reform an object to fit your wants. Drupal Development Service key ideas that you must perceive when making a customized serialization are Drupal 10 Upkeep and Help Service Tagged companies for discovery How a normalizer and an encoder get chosen for Drupal Development Company activity How recursive serialization can enhance maintainability and restrict complexity. As soon as you’re conscious and conversant in Drupal Development Company serializer element you’ll begin noticing use circumstances for it. As an alternative of utilizing hard-coded options with poor extensibility and maintainability, begin leveraging Drupal Development Company serialization system. Drupal 10 Growth 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 Help: Drupal 10 Upkeep and Help Service Serialization Step-by-Step

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.