class TestContextAwareBlock
Same name in other branches
- 9 core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php \Drupal\block_test\Plugin\Block\TestContextAwareBlock
- 10 core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php \Drupal\block_test\Plugin\Block\TestContextAwareBlock
- 11.x core/modules/block/tests/modules/block_test/src/Plugin/Block/TestContextAwareBlock.php \Drupal\block_test\Plugin\Block\TestContextAwareBlock
Provides a context-aware block.
Plugin annotation
@Block(
id = "test_context_aware",
admin_label = @Translation("Test context-aware block"),
context_definitions = {
"user" = @ContextDefinition("entity:user", required = FALSE,
label = @Translation("User Context"), constraints = { "NotNull" = {} }
),
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Component\Plugin\ContextAwarePluginBase extends \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\ContextAwarePluginInterface
- class \Drupal\Core\Plugin\ContextAwarePluginBase extends \Drupal\Component\Plugin\ContextAwarePluginBase implements \Drupal\Core\Plugin\ContextAwarePluginInterface, \Drupal\Core\Cache\CacheableDependencyInterface uses \Drupal\Core\TypedData\TypedDataTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait
- class \Drupal\Core\Block\BlockBase extends \Drupal\Core\Plugin\ContextAwarePluginBase implements \Drupal\Core\Block\BlockPluginInterface, \Drupal\Core\Plugin\PluginWithFormsInterface, \Drupal\Core\Render\PreviewFallbackInterface uses \Drupal\Core\Block\BlockPluginTrait, \Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait
- class \Drupal\block_test\Plugin\Block\TestContextAwareBlock extends \Drupal\Core\Block\BlockBase
- class \Drupal\Core\Block\BlockBase extends \Drupal\Core\Plugin\ContextAwarePluginBase implements \Drupal\Core\Block\BlockPluginInterface, \Drupal\Core\Plugin\PluginWithFormsInterface, \Drupal\Core\Render\PreviewFallbackInterface uses \Drupal\Core\Block\BlockPluginTrait, \Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Plugin\ContextAwarePluginBase extends \Drupal\Component\Plugin\ContextAwarePluginBase implements \Drupal\Core\Plugin\ContextAwarePluginInterface, \Drupal\Core\Cache\CacheableDependencyInterface uses \Drupal\Core\TypedData\TypedDataTrait, \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait
- class \Drupal\Component\Plugin\ContextAwarePluginBase extends \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\ContextAwarePluginInterface
Expanded class hierarchy of TestContextAwareBlock
File
-
core/
modules/ block/ tests/ modules/ block_test/ src/ Plugin/ Block/ TestContextAwareBlock.php, line 22
Namespace
Drupal\block_test\Plugin\BlockView source
class TestContextAwareBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
/** @var $user \Drupal\user\UserInterface */
$user = $this->getContextValue('user');
return [
'#prefix' => '<div id="' . $this->getPluginId() . '--username">',
'#suffix' => '</div>',
'#markup' => $user ? $user->getAccountName() : 'No context mapping selected.',
];
}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
if ($this->getContextValue('user') instanceof UserInterface) {
$this->messenger()
->addStatus('User context found.');
}
return parent::blockAccess($account);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
TestContextAwareBlock::blockAccess | protected | function | |
TestContextAwareBlock::build | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.