Aller au contenu principal

Drupal 10 : Lazy Builder

Pour mettre en evidence Lazy buillder, nous allons construire un bouton permettant d'afficher l'icone de la wihslist sur les entitées.

Pour faire cela, nous allons passer par le formatteur de champs integer, ( à condition que l'integer à formatter represente l'identifiant d'une entité ).

Nous commencons par une implementation du formatteur de champs :

<?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;
  }
  
}

Par la suite nous modifier la methode viewElements afin qu'elle se construise via 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;
  }

Ce ci implique de creer le service : habeuk_wishlist.manager et d'y ajouter la methode buildWishlist.

 

<?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'
    ];
  }
  
}

et voila notre lazy-builder fonctionne.

Pour mieux comprendre le processus; vous pouvez lire les articles suivant :

Ressources : 

 

Utile pour le tuto suivant : 
  • https://www.drupal.org/docs/drupal-apis/render-api/cacheability-of-render-arrays
Stephane K
Il y'a 5 months
Modifié
Il y'a 5 months
Loading ...