Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Acm Promotion module D9 ready by removing deprecated code/dependency …
Browse files Browse the repository at this point in the history
…fix etc. (#146)

* Making acm_promotion module D9 ready by removing deprecated code.

* Using interface for the argument.
  • Loading branch information
joshirohit100 authored and Miro Michalicka committed May 22, 2019
1 parent e8a4f4a commit 71c9b89
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 19 deletions.
4 changes: 3 additions & 1 deletion modules/acm_promotion/acm_promotion.module
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function acm_promotion_page_attachments(array &$page) {

// Create a map of promo displays to fill in containers once the page loads.
foreach (array_keys($display_modes) as $display_mode) {
$render_array = node_view($node, $display_mode);
$render_array = \Drupal::entityTypeManager()
->getViewBuilder('node')
->view()($node, $display_mode);
$promo_display_map[$promo_code][$display_mode] = \Drupal::service('renderer')->renderRoot($render_array);
}

Expand Down
1 change: 1 addition & 0 deletions modules/acm_promotion/acm_promotion.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ services:
- '@config.factory'
- '@database'
- '@acm.i18n_helper'
- '@module_handler'
6 changes: 3 additions & 3 deletions modules/acm_promotion/src/AcmPromotionQueueBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class AcmPromotionQueueBase extends QueueWorkerBase implements Containe
/**
* Promotion manager.
*
* @var \Drupal\acm_promotion\AcqPromotionsManager
* @var \Drupal\acm_promotion\AcmPromotionsManager
*/
protected $promotionManager;

Expand Down Expand Up @@ -55,7 +55,7 @@ abstract class AcmPromotionQueueBase extends QueueWorkerBase implements Containe
* Plugin definition.
* @param \Drupal\acm\Connector\IngestAPIWrapper $ingestApiWrapper
* IngestAPIWrapper Service object.
* @param \Drupal\acm_promotion\AcqPromotionsManager $promotionManager
* @param \Drupal\acm_promotion\AcmPromotionsManager $promotionManager
* Promotion manager.
* @param \Drupal\Core\Logger\LoggerChannelFactory $loggerFactory
* Logger service.
Expand All @@ -66,7 +66,7 @@ public function __construct(array $configuration,
$plugin_id,
$plugin_definition,
IngestAPIWrapper $ingestApiWrapper,
AcqPromotionsManager $promotionManager,
AcmPromotionsManager $promotionManager,
LoggerChannelFactory $loggerFactory,
CacheTagsInvalidatorInterface $tag_invalidate) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
Expand Down
33 changes: 30 additions & 3 deletions modules/acm_promotion/src/AcmPromotionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\node\NodeInterface;

/**
Expand Down Expand Up @@ -80,6 +81,27 @@ class AcmPromotionsManager {
*/
protected $connection;

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

/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;

/**
* I18n Helper.
*
Expand Down Expand Up @@ -108,6 +130,8 @@ class AcmPromotionsManager {
* Database connection service.
* @param \Drupal\acm\I18nHelper $i18n_helper
* I18nHelper object.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* Module handler.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager,
APIWrapperInterface $api_wrapper,
Expand All @@ -117,7 +141,9 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager,
QueueFactory $queue,
ConfigFactoryInterface $configFactory,
Connection $connection,
I18nHelper $i18n_helper) {
I18nHelper $i18n_helper,
ModuleHandlerInterface $module_handler) {
$this->entityTypeManager = $entity_type_manager;
$this->nodeStorage = $entity_type_manager->getStorage('node');
$this->skuStorage = $entity_type_manager->getStorage('acm_sku');
$this->apiWrapper = $api_wrapper;
Expand All @@ -128,6 +154,7 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager,
$this->configFactory = $configFactory;
$this->connection = $connection;
$this->i18nHelper = $i18n_helper;
$this->moduleHandler = $module_handler;
}

/**
Expand Down Expand Up @@ -244,7 +271,7 @@ public function getPromotionByRuleId($rule_id, $rule_type) {
*/
public function getSkusForPromotion(NodeInterface $promotion) {
$query = $this->connection->select('acm_sku__field_acm_sku_promotions', 'fasp');
if (\Drupal::entityTypeManager()->getDefinition('acm_sku')->isTranslatable()) {
if ($this->entityTypeManager->getDefinition('acm_sku')->isTranslatable()) {
$query->join('acm_sku_field_data', 'asfd', 'asfd.id = fasp.entity_id');
}
else {
Expand Down Expand Up @@ -347,7 +374,7 @@ public function syncPromotionWithConnectorResponse(array $promotion, NodeInterfa
}

// Invoke the alter hook to allow modules to update the node from API data.
\Drupal::moduleHandler()->alter('acm_promotion_promotion_node', $promotion_node, $promotion);
$this->moduleHandler->alter('acm_promotion_promotion_node', $promotion_node, $promotion);

$status = $promotion_node->save();
// Create promotion translations based on the language codes available in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AcmPromotionCommands extends DrushCommands {
*/
public function __construct(AcmPromotionsManager $acmPromotionsManager,
LoggerChannelFactoryInterface $loggerChannelFactory) {
parent::__construct();
$this->acmPromotionsManager = $acmPromotionsManager;
$this->logger = $loggerChannelFactory->get('acm_promotion');
}
Expand Down
2 changes: 1 addition & 1 deletion modules/acm_promotion/src/Form/PromotionSyncForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
switch ($action) {
case 'Synchronize Promotions':
$this->promotionManager->syncPromotions();
drupal_set_message('Promotions Synchronization Complete.', 'status');
$this->messenger()->addStatus($this->t('Promotions Synchronization Complete'));
break;
}
}
Expand Down
62 changes: 54 additions & 8 deletions modules/acm_promotion/src/Plugin/Block/AcmPromotionBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\NodeInterface;
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -22,12 +25,33 @@
class AcmPromotionBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The entity type manager.
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;

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

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

/**
* Route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* Constructs a NodeEmbedBlock instance.
*
Expand All @@ -39,10 +63,26 @@ class AcmPromotionBlock extends BlockBase implements ContainerFactoryPluginInter
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* Renderer service.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* Route match service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityDisplayRepositoryInterface $entity_display_repository) {
public function __construct(array $configuration,
$plugin_id,
$plugin_definition,
EntityDisplayRepositoryInterface $entity_display_repository,
EntityTypeManagerInterface $entity_type_manager,
RendererInterface $renderer,
RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityDisplayRepository = $entity_display_repository;
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->routeMatch = $route_match;

}

/**
Expand All @@ -53,7 +93,10 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_display.repository')
$container->get('entity_display.repository'),
$container->get('entity_type.manager'),
$container->get('renderer'),
$container->get('current_route_match')
);
}

Expand Down Expand Up @@ -123,8 +166,10 @@ public function build() {
$json = [];
foreach ($promos as $promo_node) {
$promo_code = $promo_node->getTitle();
$teaser = node_view($promo_node, $display_mode);
$json[$promo_code] = \Drupal::service('renderer')->render($teaser);
$teaser = $this->entityTypeManager
->getViewBuilder('node')
->view($promo_node, $display_mode);
$json[$promo_code] = $this->renderer->render($teaser);
}

// TODO We may need to remove the id and add better (more specific) classes.
Expand Down Expand Up @@ -159,19 +204,20 @@ public function build() {
*
* This method loads the active promotion nodes.
*
* @return Drupal\node\Entity\Node[]
* @return \Drupal\node\NodeInterface[]
* The promotion node for the user's session.
*/
protected function getPromotionNodes($view_mode) {
$nodes = [];

// Default to loading all enabled (published) nodes.
$query = \Drupal::entityQuery('node')
$query = $this->entityTypeManager->getStorage('node')
->getQuery()
->condition('type', 'acm_promotion')
->condition('status', NodeInterface::PUBLISHED);

if ($view_mode === 'sku_limit') {
$viewing_node = \Drupal::routeMatch()->getParameter('node');
$viewing_node = $this->routeMatch->getParameter('node');

if (
!is_null($viewing_node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Drupal\acm_promotion\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Utility\Token;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a block to display promotion information to users.
Expand All @@ -17,7 +20,48 @@
* }
* )
*/
class AcmPromotionContainerBlock extends BlockBase {
class AcmPromotionContainerBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* Token manager.
*
* @var \Drupal\Core\Utility\Token
*/
protected $tokenManager;

/**
* Constructs a new AcmPromotionContainerBlock.
*
* @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\Core\Utility\Token $token_manager
* Token manager.
*/
public function __construct(array $configuration,
$plugin_id,
$plugin_definition,
Token $token_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->tokenManager = $token_manager;
}

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

/**
* {@inheritdoc}
Expand Down Expand Up @@ -132,8 +176,7 @@ public function build() {
return !empty($value);
});

$token = \Drupal::token();
$argument = $token->replace($argument, $contexts);
$argument = $this->tokenManager->replace($argument, $contexts);

// TODO May need to update this in the future to make this more flexible,
// perhaps with a theme function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function processItem($data) {

$sku_texts = implode(',', $skus);

$invalidate_tags = array_merge($skus, ['node:' . $promotion_nid]);
// Invalidate cache tags for updated skus & promotions.
$this->tagInvalidate->invalidateTags($invalidate_tags);

Expand Down

0 comments on commit 71c9b89

Please sign in to comment.