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

12.15.5. Կախվածությունների ներարկում անհատական ​​դասում/ծառայությունում

19/04/2025, by Ivan

Menu

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

12.15. Սերվիսներ և Dependency Injection։
12.15.1. Dependency Injection Controller-ում
12.15.2. Dependency Injection Բլոկում
12.15.3. Dependency Injection BaseForm-ում
12.15.4. Dependency Injection ConfigFormBase-ում (կազմաձեւման ձևում)

Այս հոդվածում կդիտարկենք, թե ինչպես սերվիսներ ավելացնել DI-ի միջոցով custom դասում/սերվիսում։ Օրինակ վերցնենք Book մոդուլը․

/core/modules/book/book.services.yml:

services:
  book.manager:
    class: Drupal\book\BookManager
    arguments: ['@entity.manager', '@string_translation', '@config.factory', '@book.outline_storage', '@renderer']

arguments դաշտում նշվում են այն սերվիսները, որոնք անհրաժեշտ է ստանալ Service container-ից։ Հետևաբար, մեր դասում մեզ պետք չէ create() մեթոդ ավելացնել։

/core/modules/book/src/BookManager.php:

<?php

namespace Drupal\book;

use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
...

/**
 * Defines a book manager.
 */
class BookManager implements BookManagerInterface {

  /**
   * Entity manager Service Object.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Config Factory Service Object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Books Array.
   *
   * @var array
   */
  protected $books;

  /**
   * Book outline storage.
   *
   * @var \Drupal\book\BookOutlineStorageInterface
   */
  protected $bookOutlineStorage;

  /**
   * Stores flattened book trees.
   *
   * @var array
   */
  protected $bookTreeFlattened;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a BookManager object.
   */
  public function __construct(EntityManagerInterface $entity_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, BookOutlineStorageInterface $book_outline_storage, RendererInterface $renderer) {
    $this->entityManager = $entity_manager;
    $this->stringTranslation = $translation;
    $this->configFactory = $config_factory;
    $this->bookOutlineStorage = $book_outline_storage;
    $this->renderer = $renderer;
  }

  ...

}

Օրինակից հեռացվել է DI-ի հետ ոչ առնչվող կոդը՝ հեշտ դիտարկման համար։ Մնացած մասով սերվիսների ավելացումն իրականացվում է նույն կերպ ինչպես controller-ում։