Problem/Motivation

Currently the status messages block is loaded using block id. Block id may vary as per the installation.

Proposed resolution

Use entity field query

$theme_manager = \Drupal::service('theme.manager');
$active_theme = $theme_manager->getActiveTheme()->getName();

$ids = \Drupal::entityQuery('block')
    ->condition('plugin', 'system_messages_block')
    ->condition('theme', $active_theme)
    ->execute();

to obtain the block id.

Remaining tasks

Update kernel tests.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

subhojit777 created an issue. See original summary.

subhojit777’s picture

subhojit777’s picture

Found out that one shortcoming of using plugin.manager.block service is that it does not gives the region where the block is placed. The region can only be obtained from the block entity.

Something like this:

$theme_manager = \Drupal::service('theme.manager');
$active_theme = $theme_manager->getActiveTheme()->getName();

$ids = \Drupal::entityQuery('block')
    ->condition('plugin', 'system_messages_block')
    ->condition('theme', $active_theme)
    ->execute();

can be done to check if status messages is placed in the current theme. The result $ids would give the block id, and using the block id the block entity can be loaded, which would give the region where block is placed.

subhojit777’s picture

If I am going with the query as mentioned in #3 then it would create an inconsistency in the code.

I am using block plugin manager to load the commerce_cart block. And I am happy with that approach. I can't find any reason to change the approach, and use a query like this #3.

Considering that the approach for refreshing cart content will not be changed, so, in future I will have to split RefreshPageElementsHelper class into two. One will refresh the status message, and the other will refresh the cart.

subhojit777’s picture

Title: [Code Improvement] Load status messages block using plugin manager » [Code Improvement] Unhardcode status message block id inside RefreshPageElementsHelper
subhojit777’s picture

Issue summary: View changes

  • subhojit777 committed ad35b1e on 8.x-1.x
    Issue #2920694 by subhojit777: [Code Improvement] Unhardcode status...
subhojit777’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.