Skip to content
Snippets Groups Projects
Commit 58d8e35c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2003058 by ashwinikumar, ebeyrent, DmitryDrozdik, benjy: Replace...

Issue #2003058 by ashwinikumar, ebeyrent, DmitryDrozdik, benjy: Replace drupal_container() with Drupal::service() in the block module.
parent f325f63c
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
use Drupal\block\BlockBase; use Drupal\block\BlockBase;
use Drupal\Component\Annotation\Plugin; use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation; use Drupal\Core\Annotation\Translation;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\block\Plugin\Type\BlockManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Defines a generic custom block type. * Defines a generic custom block type.
...@@ -21,7 +25,55 @@ ...@@ -21,7 +25,55 @@
* derivative = "Drupal\custom_block\Plugin\Derivative\CustomBlock" * derivative = "Drupal\custom_block\Plugin\Derivative\CustomBlock"
* ) * )
*/ */
class CustomBlockBlock extends BlockBase { class CustomBlockBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The Plugin Block Manager.
*
* @var \Drupal\block\Plugin\Type\BlockManager.
*/
protected $blockManager;
/**
* The Module Handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface.
*/
protected $moduleHandler;
/**
* Constructs a new CustomBlockBlock.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\block\Plugin\Type\BlockManager
* The Plugin Block Manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface
* The Module Handler.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, BlockManager $block_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->blockManager = $block_manager;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.block'),
$container->get('module_handler')
);
}
/** /**
* Overrides \Drupal\block\BlockBase::settings(). * Overrides \Drupal\block\BlockBase::settings().
...@@ -61,8 +113,8 @@ public function blockForm($form, &$form_state) { ...@@ -61,8 +113,8 @@ public function blockForm($form, &$form_state) {
*/ */
public function blockSubmit($form, &$form_state) { public function blockSubmit($form, &$form_state) {
// Invalidate the block cache to update custom block-based derivatives. // Invalidate the block cache to update custom block-based derivatives.
if (module_exists('block')) { if ($this->moduleHandler->moduleExists('block')) {
drupal_container()->get('plugin.manager.block')->clearCachedDefinitions(); $this->blockManager->clearCachedDefinitions();
} }
} }
......
...@@ -8,13 +8,47 @@ ...@@ -8,13 +8,47 @@
namespace Drupal\block; namespace Drupal\block;
use Drupal\Core\Entity\EntityAccessController; use Drupal\Core\Entity\EntityAccessController;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Provides a Block access controller. * Provides a Block access controller.
*/ */
class BlockAccessController extends EntityAccessController { class BlockAccessController extends EntityAccessController implements EntityControllerInterface {
/**
* The node grant storage.
*
* @var \Drupal\Core\Path\AliasManagerInterface
*/
protected $aliasManager;
/**
* Constructs a BlockAccessController object.
*
* @param string $entity_type
* The entity type of the access controller instance.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The alias manager.
*/
public function __construct($entity_type, AliasManagerInterface $alias_manager) {
parent::__construct($entity_type);
$this->aliasManager = $alias_manager;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static(
$entity_type,
$container->get('path.alias_manager')
);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -64,7 +98,7 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A ...@@ -64,7 +98,7 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A
if ($visibility['path']['visibility'] < BLOCK_VISIBILITY_PHP) { if ($visibility['path']['visibility'] < BLOCK_VISIBILITY_PHP) {
// Compare the lowercase path alias (if any) and internal path. // Compare the lowercase path alias (if any) and internal path.
$path = current_path(); $path = current_path();
$path_alias = drupal_strtolower(drupal_container()->get('path.alias_manager')->getPathAlias($path)); $path_alias = Unicode::strtolower($this->aliasManager->getPathAlias($path));
$page_match = drupal_match_path($path_alias, $pages) || (($path != $path_alias) && drupal_match_path($path, $pages)); $page_match = drupal_match_path($path_alias, $pages) || (($path != $path_alias) && drupal_match_path($path, $pages));
// When $block->visibility has a value of 0 // When $block->visibility has a value of 0
// (BLOCK_VISIBILITY_NOTLISTED), the block is displayed on all pages // (BLOCK_VISIBILITY_NOTLISTED), the block is displayed on all pages
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment