Drupal 10 Assist: Drupal 10 Upkeep and Assist Service The way to create and expose computed properties to Drupal Development REST API in 8

In 8.5.0, Drupal Development “processed” property of textual content fields is out there in REST which signifies that REST apps can render Drupal Development HTML output of a textarea with out worrying about Drupal Development filter codecs. On this publish, I’ll present you how one can add your individual processed fields to be output by way of Drupal Development REST API. by Jibran Ijaz / 18 June 2021 Drupal Developer “processed” property talked about above is what is called a computed property on Drupal Development textarea area. Drupal Developer capacity to make Drupal Development computed properties obtainable for Drupal Development REST API like this may be very useful. For instance, when Drupal Development consumer inputs Drupal Development uncooked worth and performs some advanced logical operations on it earlier than exhibiting Drupal Development output. fieldable entities also can have computed properties and people properties can be uncovered by way of REST. I used Drupal Development following answer to show Drupal Development knowledge of an entity area which takes uncooked knowledge from Drupal Development customers and carry out some advanced calculations on it. To start with, we have to write hook_entity_bundle_field_info so as to add Drupal Development property and since it’s a computed area we need not implement hook_entity_field_storage_info. id() === ‘node’ && $bundle === ‘my_bundle’) { // It’s not a basefield so we want a customized area storage definition see // https Drupal 10 Upkeep and Assist Service//www.Drupal 10.org/undertaking/Drupal 10/points/2346347#comment-12206126 $fields[‘my_computed_property’] = FieldStorageDefinition Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist Servicecreate(‘string’) ->setLabel(t(‘My computed property’)) ->setDescription(t(‘That is my computed property.’)) ->setComputed(TRUE) ->setClass(MyComputedItemList Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist Serviceclass) ->setReadOnly(FALSE) ->setInternal(FALSE) ->setDisplayOptions(‘view’, [ ‘label’ => ‘hidden’, ‘region’ => ‘hidden’, ‘weight’ => -5, ]) ->setDisplayOptions(‘kind’, [ ‘label’ => ‘hidden’, ‘region’ => ‘hidden’, ‘weight’ => -5, ]) ->setTargetEntityTypeId($entity_type->id()) ->setTargetBundle($bundle) ->setName(‘my_computed_property’) ->setDisplayConfigurable(‘kind’, FALSE) ->setDisplayConfigurable(‘view’, FALSE); } return $fields; } Then we want Drupal Development MyComputedItemList class to carry out some magic. This class will enable us to set Drupal Development computed area worth. getEntity(); if ($entity->getEntityTypeId() !== ‘node’ || $entity->bundle() !== ‘my_bundle’ || $entity->my_some_other_field->isEmpty()) { return; } $some_string = some_magic($entity->my_some_other_field); $this->record[0] = $this->createItem(0, $some_string); } Drupal Developer area we add is just not a base area so we will not use CoreFieldBaseFieldDefinition. There may be an open core challenge to handle that https Drupal 10 Upkeep and Assist Service//www.Drupal 10.org/undertaking/Drupal 10/points/2346347 however in checks there’s a workaround utilizing a replica of entity_testFieldStorageDefinition Drupal 10 Upkeep and Assist Service <?php // my_Drupal 10 module/src/FieldStorageDefinition.php namespace my_Drupal 10 module; use CoreFieldBaseFieldDefinition; /** * A customized area storage definition class. * * For comfort we lengthen from BaseFieldDefinition though this could not * implement FieldDefinitionInterface. * * @todo Present and make use of a correct FieldStorageDefinition class as a substitute Drupal 10 Upkeep and Assist Service * https Drupal 10 Upkeep and Assist Service//www.Drupal 10.org/node/2280639. */ class FieldStorageDefinition extends BaseFieldDefinition { /** * {@inheritdoc} */ public operate isBaseField() { return FALSE; } } Final however not least we have to announce our property definition to Drupal Development entity system in order that it could maintain monitor of it. As it’s an current bundle we will write an replace hook. In any other case, we might must implement hook_entity_bundle_create. setLabel(t(‘My computed property’)) ->setDescription(t(‘That is my computed property.’)) ->setComputed(TRUE) ->setClass(MyComputedItemList Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist Serviceclass) ->setReadOnly(FALSE) ->setInternal(FALSE) ->setDisplayOptions(‘view’, [ ‘label’ => ‘hidden’, ‘region’ => ‘hidden’, ‘weight’ => -5, ]) ->setDisplayOptions(‘kind’, [ ‘label’ => ‘hidden’, ‘region’ => ‘hidden’, ‘weight’ => -5, ]) ->setTargetEntityTypeId(‘node’) ->setTargetBundle(‘my_bundle’) ->setName(‘my_computed_property’) ->setDisplayConfigurable(‘kind’, FALSE) ->setDisplayConfigurable(‘view’, FALSE); // Notify Drupal Development storage about Drupal Development new area. Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist Serviceservice(‘field_definition.listener’)->onFieldDefinitionCreate($fields[‘my_computed_property’]); } Drupal Developer fantastic thing about this answer is that I haven’t got to write down a customized serializer to normalize Drupal Development output. Typed Knowledge API is doing all Drupal Development heavy lifting. Associated core points Drupal 10 Upkeep and Assist Service Embody processed textual content in normalizations Drupal 10 Upkeep and Assist Service “textual content” area kind’s “processed” computed property must be non-internal and carry cacheability metadata Finalize API for creating, overriding, and altering code-defined bundle fields Decouple area definitions from typed knowledge definitions Add a FieldStorageDefinition class Error when saving a denormalized entity with textual content fields with “processed” property Tagged jsonapi, REST, XML, JSON, hal_json, Normalizers Drupal 10 Improvement 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

Drupal 10 Assist: Drupal 10 Upkeep and Assist Service The way to create and expose computed properties to Drupal Development REST API 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.