class TestDisplayVariant in Drupal 10
Same name and namespace in other branches
- 8 core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php \Drupal\display_variant_test\Plugin\DisplayVariant\TestDisplayVariant
- 9 core/modules/system/tests/modules/display_variant_test/src/Plugin/DisplayVariant/TestDisplayVariant.php \Drupal\display_variant_test\Plugin\DisplayVariant\TestDisplayVariant
Provides a display variant that requires configuration.
Plugin annotation
@DisplayVariant(
id = "display_variant_test",
admin_label = @Translation("Test display variant")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Display\VariantBase implements VariantInterface uses RefinableCacheableDependencyTrait, PluginDependencyTrait
- class \Drupal\display_variant_test\Plugin\DisplayVariant\TestDisplayVariant implements ContextAwareVariantInterface, PageVariantInterface
- class \Drupal\Core\Display\VariantBase implements VariantInterface uses RefinableCacheableDependencyTrait, PluginDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TestDisplayVariant
File
- core/
modules/ system/ tests/ modules/ display_variant_test/ src/ Plugin/ DisplayVariant/ TestDisplayVariant.php, line 18
Namespace
Drupal\display_variant_test\Plugin\DisplayVariantView source
class TestDisplayVariant extends VariantBase implements PageVariantInterface, ContextAwareVariantInterface {
/**
* The render array representing the main page content.
*
* @var array
*/
protected $mainContent = [];
/**
* The page title: a string (plain title) or a render array (formatted title).
*
* @var string|array
*/
protected $title = '';
/**
* An array of collected contexts.
*
* This is only used on runtime, and is not stored.
*
* @var \Drupal\Component\Plugin\Context\ContextInterface[]
*/
protected $contexts = [];
/**
* Gets the contexts.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* An array of set contexts, keyed by context name.
*/
public function getContexts() {
return $this->contexts;
}
/**
* Sets the contexts.
*
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts
* An array of contexts, keyed by context name.
*
* @return $this
*/
public function setContexts(array $contexts) {
$this->contexts = $contexts;
return $this;
}
/**
* {@inheritdoc}
*/
public function setMainContent(array $main_content) {
$this->mainContent = $main_content;
return $this;
}
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* {@inheritdoc}
*/
public function build() {
$config = $this
->getConfiguration();
if (empty($config['required_configuration'])) {
throw new \Exception('Required configuration is missing!');
}
$contexts = $this
->getContexts();
if (!isset($contexts['context'])) {
throw new \Exception('Required context is missing!');
}
$build = [];
$build['content']['default'] = [
'#markup' => $config['required_configuration'] . ' ' . $contexts['context']
->getContextValue(),
];
CacheableMetadata::createFromObject($this)
->applyTo($build);
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | ||
CacheableDependencyTrait:: |
public | function | ||
CacheableDependencyTrait:: |
public | function | ||
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
MessengerTrait:: |
protected | property | The messenger. | 18 |
MessengerTrait:: |
public | function | Gets the messenger. | 18 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function | ||
PluginBase:: |
public | function | ||
PluginBase:: |
public | function | 2 | |
PluginBase:: |
public | function | ||
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TestDisplayVariant:: |
protected | property | An array of collected contexts. | |
TestDisplayVariant:: |
protected | property | The render array representing the main page content. | |
TestDisplayVariant:: |
protected | property | The page title: a string (plain title) or a render array (formatted title). | |
TestDisplayVariant:: |
public | function |
Builds and returns the renderable array for the display variant. Overrides VariantInterface:: |
|
TestDisplayVariant:: |
public | function |
Gets the contexts. Overrides ContextAwareVariantInterface:: |
|
TestDisplayVariant:: |
public | function |
Sets the contexts. Overrides ContextAwareVariantInterface:: |
|
TestDisplayVariant:: |
public | function |
Sets the main content for the page being rendered. Overrides PageVariantInterface:: |
|
TestDisplayVariant:: |
public | function |
Sets the title for the page being rendered. Overrides PageVariantInterface:: |
|
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function | ||
VariantBase:: |
public | function |