Lullabot: Drupal maintenance support plans Serialization Step-by-Step

Texas Born In 2000

Pixeldust Drupal Developers

Pixeldust is an expert software development agency and trusted Pantheon development partner specializing in Drupal Development, security, and support. In business since 2000, we have completed over 500 projects.

Pixeldust is Trusted by the Worlds Greatest Brands

Pixeldust offers premium Drupal development services.

We are committed to developing under Drupal best-practices, ensuring our clients have a stable, maintainable codebase.

Pixeldust is an expert software agency specializing in responsive frameworks, mobile applications, and online marketing services. Using the latest technologies, coupled with a healthy dose of imagination and expertise, we work closely with you to identify your needs, wants and provide a comprehensive, integrated solution to your online communication requirements.

Our Drupal developers are focused on quality, not quantity.

Our aim is to provide a responsive and personal approach to each project to ensure that our clients can benefit from their investment. We see each Drupal development project as an opportunity to grow your business—we aim to help you grow sales and improve retention while offering leading, aesthetically pleasing, and functional designs that suit your needs faultlessly.

What differentiates Pixeldust from other Drupal development is the effort to create a positive return on clients’ investments.

We have devoted years of effort to understanding the variables involved in user experience as well as online marketing strategies. Our team of inspired Web Designers, developers, and marketing specialists help to increase the exposure of your website, as well as provide a unique user engagement. Pixeldust is focused on creative and results-orientated solutions developed to maximize your website’s true earning and traffic potential.

Professional class Drupal Development is absolutely integral to the legitimacy and effectiveness of your online presence.

Our Developers boast superior technical know-how. In business since 1999, we have completed over 500 projects, giving us plenty of experience in developing beautiful, tailored websites while keeping your business interests in mind.

 

How does on-site Drupal SEO work?

Drupal 8 is the CMS of choice for many top enterprise websites because it was built from the jump the extensibility required to optimize every node, view, and code snippet for search engines. However, you have to know how to configure it. 

Search Engine Optimization (on-page Drupal SEO) has been around as long as search engines. If you run a website, you probably have at least a basic understanding of SEO. Drupal is phenomenal for SEO. We’ve worked in Drupal for 14 years and experienced firsthand how positively search engines respond to correctly configured Drupal sites. 

We have helped clients double their traffic, just by switching platforms. Drupal has some distinct technical advantages with on-site optimizations like RDF or AMP. The results are higher ranking, quicker, and more trafficked sites. Since Drupal 8 has scheduled feature releases every six months, you will be thinking in terms of months, not years, when new technology becomes a standard.

FREE Drupal maintenance support plans Security Audit

Why a Drupal site audit?

  • Security – Discover weaknesses in your Drupal implementation.
  • Performance – Identify areas where performance improvements can be made.
  • Site Acquisition – Do this before you buy a business as part of due diligence.
  • Implementation Verification – Check your site before it goes live to avoid critical issues that may appear under load.
  • Vendor Management – Make sure your current developer is doing a good job.
  • Support Transition – When moving to a new developer both sides need to know what they are working with.
FREE Drupal maintenance support plans Security Audit

Case Study: Mahindra USA Inc

Mahindra USA, Inc. manufactures agricultural machinery and equipment. They are the world’s largest selling tractor brand by volume, and the World’s number one tractor maker for over three decades.

Drupal Requirements

  • Support for a company-wide rebranding
  • Migration to a more robust and flexible platform in Drupal 8
  • Integration with third-party customer relationship management applications
  • Internationalization
  • Efficient scalability
  • Integration with sales SAS
  • Read Case Study

Inquiry

Call (512)730-0999 or submit an inquiry.
  • This field is for validation purposes and should be left unchanged.
Lullabot: Drupal maintenance support plans Serialization Step-by-Step

30-Day Drupal SEO Blast – On-page and Off-page SEO Overhaul

We start every project off with an introductory discovery call with key stakeholders to create a project plan, establish key contacts, and plan credential transfers. 

Pixeldust’s 30-Day SEO Blast is a campaign to overhaul your Drupal site for immediate improvement in search engine rankings. Think of it as a 30-day boot camp for your website. The process is divided into two areas of focus: on-site and off-site.

On-Site Drupal SEO
On-site (or On-page) SEO:  Changes are made 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. On-site optimization ensures your site is configured to provide information in a way that meets Search engine standards for optimal indexing.  

Off-site White Hat Drupal SEO
Off-site (or Off-page) Drupal SEO is the process of making your site more visible to humans across the internet and increasing its relevance. 

Vigilant Drupal Support Plans

When you subscribe to our Unlimited Drupal Support Plan in Houston or Austin, you have the comfort of knowing our trusted team of Drupal admins is at the ready; waiting to fix errors, broken functionality, layout issues, and anything else the pops up. Some of our Unlimited clients don’t even have to log in to their sites anymore. Even if you just need an article posted, or a new product added to your shop, no worries, we got you. If it takes less than 30 minutes per issue, we will take care of it.

We start every project off with an introductory discovery call with key stakeholders to create a project plan, establish key contacts, and plan credential transfers. 

  • Unlimited Repairs & Fixes
  • Unlimited Update Tasks
  • FREE Set-up
  • Same-Day Security Updates
  • Monthly Module Updates
  • Monthly Broken Link Scan
  • Monthly Security Scan
  • Monthly Manual Site Check
  • Monthly Speed Test
  • Offline Updating
  • GIT Version Control
  • Detailed Work Notes
  • Testing After All Updates
  • Security Guarantee
  • Hack/Malware/Down Recovery
  • Uptime Monitoring
  • Daily Offsite Backups
  • Free Basic Website Hosting & SSL
  • Helpdesk Support
REQUEST FOR PROPOSAL

Need a custom quote?

Submit the RFP form below and we will send you a project proposal in 48 hours. If you like what you see, we can schedule a call to discuss the project in greater detail.

Step 1 of 2

  • Contact Information

Lullabot: Drupal maintenance support plans Serialization Step-by-Step

Published on February 11, 2019

In my previous article about the serializer component, I touched on the basic concepts involved when serializing an object. To summarize, serialization is the combination of encoding and normalization. Normalizers simplify complex objects, like User or ComplexDataDefinition. Denormalizers perform the reverse operation. Using a structured array of data, they generate complex objects like the ones listed above.

In this article, I will focus on the Drupal maintenance support plans integration of the Symfony serializer component. For this, I will guide you step-by-step through a module I created as an example. You can find the module at https://github.com/e0ipso/entity_markdown. I have created a different commit for each step of the process, and this article includes a link to the code in each step at the beginning of each section. However, you can use GitHub UI to browse the code at any time and see the diff.

undefined

When this module is finished, you will be able to transform any content entity into a Markdown representation of it. Rendering a content entity with Markdown might be useful if you wanted to send an email summary of a node, for instance, but the real motivation is to show how serialization can be important outside the context of web services.

Add a new normalizer service

These are the changes for this step. You can browse the state of the code at this step in here.

Symfony’s serializer component begins with a list of normalizer classes. Whenever an object needs to be normalized or serialized the serializer will loop through the available normalizers to find one that declares support for the type of object at hand, in our case a content entity. If you want to add a class to the list of eligible normalizers you need to create a new tagged service.

A tagged service is just a regular class definition that comes with an entry in the mymodule.services.yml so the service container can find it and instantiate it whenever appropriate. For a service to be a tagged as a service you need to add a tags property with a name. You can also add a priority integer to convey precedence with respect to services tagged with the same name. For a normalizer to be recognized by the serialization module, you need to add the tag name normalizer.

When Drupal maintenance support plans core compiles the service container, our newly created tagged service will be added to the serializer list in what it’s called a compiler pass. This is the place in Drupal maintenance support plans core where that happens. The service container is then cached for performance reasons. That is why you need to clear your caches when you add a new normalizer.

Our normalizer is an empty class at the moment. We will fix that in a moment. First, we need to turn our attention to another collection of services that need to be added to the serializer, the encoders.

Include the encoder for the Markdown format

These are the changes for this step. You can browse the state of the code at this step in here.

Similarly to a normalizer, the encoder is also added to the serialization system via a tagged service. It is crucial that this service implements `EncoderInterface`. Note that at this stage, the encoder does not contain its most important method encode(). However, you can see that it contains supportsEncoding(). When the serializer component needs to encode an structured array, it will test all the encoders available (those tagged services) by executing supportsEncoding() and passing the format specified by the user. In our case, if the user specifies the ‘markdown’ format. Then, our encoder will be used to transform the structured array into a string, because supportsEncoding() will return TRUE. To do the actual encoding it will use the encode() method. We will write that method later. First, let me describe the normalization process.

Normalize content entities

The normalization will differ each time. It depends on the format you want to turn your objects into, and it depends on the type of objects you want to transform. In our example, we want to turn a content entity into a Markdown document.

For that to happen, the serializer will need to be able to:

Know when to use our normalizer class.
Normalize the content entity.
Normalize any field in the content entity.
Normalize all the properties in every field.

Discover our custom normalizer

These are the changes for this step. You can browse the state of the code at this step in here.

For a normalizer to be considered a good fit for a given object it needs to meet two conditions:

Implement the `NormalizerInterface`.
Return `TRUE` when calling `supportsNormalization()` with the object to normalize and the format to normalize to.

The process is nearly the same as the one we used to determine which encoder to use. The main difference is that we also pass the object to normalize to the supportsNormalization() method. That is a critical part since it is very common to have multiple normalizers for the same format, depending on the type of object that needs to be normalized. A Node object will have different code that turns it into an structured array when compared to an HttpException. We take that into account in our example by checking if the object being normalized is an instance of a ContentEntityInterface.

Normalize the content entity

These are the changes for this step. You can browse the state of the code at this step in here.

This step contains a first attempt to normalize the content entity that gets passed as an argument to the normalize() method into our normalizer.

Imagine that our requirements are that the resulting markdown document needs to include an introductory section with the entity label, entity type, bundle and language. After that, we need a list with all the field names and the values of their properties. For example, the body field of a node will result in the name field_body and the values for format, summary and value. In addition to that any field can be single or multivalue, so we will take that into consideration.

To fulfill these requirements, I’ve written a bunch of code that deals with the specific use case of normalizing a content entity into an structured array ready to be encoded into Markdown. I don’t think that the specific code is relevant to explain how normalization works, but I’ve added code comments to help you follow my logic.

You may have spotted the presence of a helper method called normalizeFieldItemValue() and a comment that says Now transform the field into a string version of it. Those two are big red flags suggesting that our normalizer is doing more than it should, and that it’s implicitly normalizing objects that are not of type ContentEntityInterface but of type FieldItemListInterface and FieldItemInterface. In the next section we will refactor the code in ContentEntityNormalizer to defer that implicit normalization to the serializer.

Recursive normalization

These are the changes for this step. You can browse the state of the code at this step in here.

When the serializer is initialized with the list of normalizers, for each one it checks if they implement SerializerAwareInterface. For the ones that do, the serializer adds a reference to itself into them. That way you can serialize/normalize nested objects during the normalization process. You can see how our ContentEntityNormalizer extends from SerializerAwareNormalizer, which implements the aforementioned interface. The practical impact of that is that we can use $this->serializer->normalize() from within our ContentEntityNormalizer. We will use that to normalize all the field lists in the entity and the field items inside of those.

First turn your focus to the new version of the ContentEntityNormalizer. You can see how the normalizer is divided into parts that are specific to the entity, like the label, the entity type, the bundle, and the language. The normalization for each field item list is now done in a single line: $this->serializer->normalize($field_item_list, $format, $context);.  We have reduced the LOC to almost half, and the cyclomatic complexity of the class even further. This has a great impact on the maintainability of the code.

All this code has now been moved to two different normalizers:

FieldItemListNormalizer contains the code that deals with normalizing single and multivalue fields. It uses the serializer to normalize each individual field item.
FieldItemNormalizer contains the code that normalizes the individual field items values and their properties/columns.

You can see that for the serializer to be able to recognize our new `FieldListItemNormalizer` and `FieldItemNormalizer` objects we need to add them to the service container, just like we did for the ContentEntityIterface normalizer.

A very nice side effect of this refactor, in addition to the maintainability improvement, is that a third party module can build upon our code more easily. Imagine that this third party module wants to make all field labels bold. Before the refactor they would need to introduce a normalizer for content entities—and play with the service priority so it gets selected before ours. That normalizer would contain a big copy and paste of a big blob of code in order to be able to make the desired tweaks. After the refactor, our third party would only need to have a normalizer for the field item list (which outputs the field label) with more priority than ours. That is a great win for extensibility.

Implement the encoder

As we said above the most important part of the encoder is encapsulated in the `encode()` method. That is the method in charge of turning the structured array from the normalization process into a string. In our particular case we treat each entry of the normalized array as a line in the output, then we append any suffix or prefix that may apply.

Further development

At this point the Entity Markdown module is ready to take any entity and turn it into a Markdown document. The only question is how to execute the serializer. If you want to execute it programmatically you only need do:

Drupal maintenance support plans::service(‘serializer’)->serialize(Node::load(1), ‘markdown’);

However there are other options. You could declare a REST format like the HAL module so you can make an HTTP request to http://example.org/node/1?_format=markdown and get a Markdown representation of the node in response (after configuring the corresponding REST settings).

Conclusion

The serialization system is a powerful tool that allows you to reform an object to suit your needs. The key concepts that you need to understand when creating a custom serialization are:

Tagged services for discovery
How a normalizer and an encoder get chosen for the task
How recursive serialization can improve maintainability and limit complexity.

Once you are aware and familiar with the serializer component you will start noticing use cases for it. Instead of using hard-coded solutions with poor extensibility and maintainability, start leveraging the serialization system.

Source: New feed

Lullabot: Drupal maintenance support plans Serialization Step-by-Step
Shopping Cart
There are no products in the cart!
Continue Shopping
0