Search
 
SCRIPT & CODE EXAMPLE
 

PHP

drupal 9 custom blocks dependency injection

/**
 * @file
 * Contains DrupalmymodPluginBlockMyModMyStoreProductsStatusBlock
 */

namespace DrupalmymodPluginBlock;

use DrupalCoreBlockBlockBase;
use DrupalCorePluginContainerFactoryPluginInterface;
use DrupalmymodStoreService;
use SymfonyComponentDependencyInjectionContainerInterface;


/**
 * Provides a product status block for the store portal.
 * 
 * @Block(
 *   id = "mymod_my_store_products_status_block",
 *   admin_label = @Translation("My Store Products Status Block")
 * )
 */

class MyModMyStoreProductsStatusBlock extends BlockBase implements ContainerFactoryPluginInterface {
  // store service
  protected $ss = NULL;
  
  /*
   * static create function provided by the ContainerFactoryPluginInterface.
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('mymod.store_mgr')
    );
  }
  
  /*
   * BlockBase plugin constructor that's expecting the StoreService object provided by create().
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StoreService $ss) {
    // instantiate the BlockBase parent first
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    
    // then save the store service passed to this constructor via dependency injection
    $this->ss = $ss;
  }
  
  /*
   * return the render array with the vals provided by the injected store service.
   */
  public function build() {
    return [
      '#theme' => 'mymod_mystore_products_status_block',
      '#n_total' => $this->ss->getTotalCount(),
      '#n_active' => $this->ss->getActiveCount(),
      '#sales_mtd' => $this->ss->getSalesMTD(),
      '#n_products_sold' => $this->ss->getTotalSoldCount()
    ];
  }
}
Comment

PREVIOUS NEXT
Code Example
Php :: Create Mysqli Table Using Php 
Php :: template string php 
Php :: create weekly calendar in php 
Php :: execute function php 
Php :: how to use custome functions in laravel 
Php :: ajax search request 
Php :: Remove .php extension & Remove trailing slash 
Php :: wordpress add button to admin bar 
Php :: pdo error message 
Php :: laravel where in 
Php :: casts laravel 
Php :: php interval day value 
Php :: acf looping through post types 
Php :: laravel if else condition in blade file 
Php :: login page in php 
Php :: php try json decode 
Php :: integer data type php 
Php :: remove invalid characters from a string laravel 
Php :: how to get array key in php 
Php :: comment split une chaine de caratere en php 
Php :: how to pass parameters to relationships laravel 
Php :: get array of last 3 dates with carbon 
Php :: php inline if condition date time 
Php :: php value in array 
Php :: check if custom post type exists 
Php :: the requested url was not found on this server. apache/2.4.46 (win64) openssl/1.1.1h php/8.0.1 server at localhost port 80 
Php :: php tutorial 
Php :: square php 
Php :: download file using jquery php 
Php :: naming convention for magento2 custom cms page index xml file 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =