Skip to main content
Drupal 10 : Lazy Builder

To demonstrate Lazy buillder, we're going to build a button that displays the wihslist icon on entities.

To do this, we'll use the integer field formatter (provided that the integer to be formatted represents the identifier of an entity).

We'll start with an implementation of the field formatter:

<?php

namespace Drupal\habeuk_wishlist\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'string' formatter.
 *
 * @FieldFormatter(
 *   id = "habeuk_wishlist_entity",
 *   label = @Translation("Wishlist entity"),
 *   field_types = {
 *     "integer",
 *   }
 * )
 */
class EntityWishlist extends FormatterBase {
  
  /**
   *
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements[] = [
      '#theme' => 'habeuk_wishlist_entity',
      "#value" => Null,
      "#entity_type" => NULL,
      "#status" => NULL
    ];
    return $elements;
  }
  
}

We then modify the viewElements method so that it can be built using lazybuilder:
 

  /**
   *
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements[] = [
      '#lazy_builder' => [
        'habeuk_wishlist.manager:buildWishlist',
        [
          'value' => 'myvalue',
          'entity_type' => "myEntity",
          'status' => 'is not create'
        ]
      ],
      '#create_placeholder' => TRUE
    ];
    return $elements;
  }

This involves creating the service: habeuk_wishlist.manager and adding the buildWishlist method.
 

<?php
declare(strict_types = 1);

namespace Drupal\habeuk_wishlist\Services;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;

/**
 *
 * @todo Add class description.
 */
final class Manager implements ManagerInterface, TrustedCallbackInterface {
  
  /**
   * Constructs a Manager object.
   *
   * @param EntityTypeManagerInterface $entityTypeManager
   */
  public function __construct(private readonly EntityTypeManagerInterface $entityTypeManager) {
    //
  }
  
  /**
   *
   * {@inheritdoc}
   */
  public function buildWishlist($value, $entity_type, $status): array {
    return [
      '#theme' => 'habeuk_wishlist_entity',
      "#value" => Null,
      "#entity_type" => NULL,
      "#status" => NULL,
      '#cache' => [
        'contexts' => [
          'user'
        ]
      ]
    ];
  }
  
  public static function trustedCallbacks() {
    return [
      'buildWishlist'
    ];
  }
  
}

and that's how our lazy-builder works.

To better understand the process, read the following articles:

Ressources : 

 

Useful for the following tutorial:

  • https://www.drupal.org/docs/drupal-apis/render-api/cacheability-of-render-arrays
Profile picture for user admin Stephane K

Écrit le

Il y'a 1 year
Modifié
Il y'a 2 weeks
Loading ...
WhatsApp
Support Habeuk : +237 694 900 622
WhatsApp Send