Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll
19/04/2025, by Ivan

Menu

Նախորդ հոդվածներում մենք ուսումնասիրեցինք՝ ինչ են Services-ը, Dependency Injection-ը (DI) և ինչպես դրանք օգտագործել մեր Controller-ում․

12.15. Սերվիսներ և Dependency Injection։
12.15.1. Dependency Injection Controller-ում

Այս հոդվածում ներկայացված է օրինակ, թե ինչպես ավելացնել սերվիսներ DI-ի միջոցով բլոկի դասում․

/modules/custom/drupalbook/src/Plugin/Block/CartBlock.php:

<?php

namespace Drupal\drupalbook\Plugin\Block;

use Drupal\commerce_cart\CartProviderInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


/**
 * Provides a cart block.
 *
 * @Block(
 *   id = "commerce_cart",
 *   admin_label = @Translation("Cart"),
 *   category = @Translation("Commerce")
 * )
 */
class CartBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The cart provider.
   *
   * @var \Drupal\commerce_cart\CartProviderInterface
   */
  protected $cartProvider;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new CartBlock.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
   *   The cart provider.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CartProviderInterface $cart_provider, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->cartProvider = $cart_provider;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('commerce_cart.cart_provider'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * Builds the cart block.
   *
   * @return array
   *   A render array.
   */
  public function build() {
     ...
     
         /** @var \Drupal\commerce_order\Entity\OrderInterface[] $carts */
    $carts = $this->cartProvider->getCarts();
    ...
  }
}

Սկզբում մենք ավելացնում ենք ContainerFactoryPluginInterface ինտերֆեյսը մեր բլոկի դասին․

class CartBlock extends BlockBase implements ContainerFactoryPluginInterface {

Այնուհետև սահմանում ենք հատկություններ, որտեղ պահելու ենք սերվիսների օբյեկտները․

  protected $cartProvider;
  protected $entityTypeManager;

Հաջորդ քայլում կոնստրուկտորում սահմանում ենք պլագինի ստանդարտ պարամետրերը․

  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

Պլագինի ստանդարտ պարամետրերից հետո փոխանցում ենք անհրաժեշտ սերվիսների օբյեկտները՝

  public function __construct(array $configuration, $plugin_id, $plugin_definition, CartProviderInterface $cart_provider, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->cartProvider = $cart_provider;
    $this->entityTypeManager = $entity_type_manager;
  }

create() մեթոդում նույնպես նշում ենք սկզբում ստանդարտ պարամետրերը, ապա՝ սերվիսների օբյեկտները․

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('commerce_cart.cart_provider'),
      $container->get('entity_type.manager')
    );
  }

Այս ամենից հետո մենք կարող ենք օգտագործել սերվիսների օբյեկտները $this-ից՝

  public function build() {
     ...
     
         /** @var \Drupal\commerce_order\Entity\OrderInterface[] $carts */
    $carts = $this->cartProvider->getCarts();
    ...
  }