class ViewsBlock in Drupal 9
Same name in this branch
- 9 core/modules/views/src/Plugin/Derivative/ViewsBlock.php \Drupal\views\Plugin\Derivative\ViewsBlock
- 9 core/modules/views/src/Plugin/Block/ViewsBlock.php \Drupal\views\Plugin\Block\ViewsBlock
Same name and namespace in other branches
- 8 core/modules/views/src/Plugin/Derivative/ViewsBlock.php \Drupal\views\Plugin\Derivative\ViewsBlock
- 10 core/modules/views/src/Plugin/Derivative/ViewsBlock.php \Drupal\views\Plugin\Derivative\ViewsBlock
Provides block plugin definitions for all Views block displays.
Hierarchy
- class \Drupal\views\Plugin\Derivative\ViewsBlock implements ContainerDeriverInterface
Expanded class hierarchy of ViewsBlock
See also
\Drupal\views\Plugin\Block\ViewsBlock
File
- core/
modules/ views/ src/ Plugin/ Derivative/ ViewsBlock.php, line 15
Namespace
Drupal\views\Plugin\DerivativeView source
class ViewsBlock implements ContainerDeriverInterface {
/**
* List of derivative definitions.
*
* @var array
*/
protected $derivatives = [];
/**
* The base plugin ID.
*
* @var string
*/
protected $basePluginId;
/**
* The view storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $viewStorage;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($base_plugin_id, $container
->get('entity_type.manager')
->getStorage('view'));
}
/**
* Constructs a ViewsBlock object.
*
* @param string $base_plugin_id
* The base plugin ID.
* @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
* The entity storage to load views.
*/
public function __construct($base_plugin_id, EntityStorageInterface $view_storage) {
$this->basePluginId = $base_plugin_id;
$this->viewStorage = $view_storage;
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this
->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Check all Views for block displays.
foreach ($this->viewStorage
->loadMultiple() as $view) {
// Do not return results for disabled views.
if (!$view
->status()) {
continue;
}
$executable = $view
->getExecutable();
$executable
->initDisplay();
foreach ($executable->displayHandlers as $display) {
/** @var \Drupal\views\Plugin\views\display\DisplayPluginInterface $display */
// Add a block plugin definition for each block display.
if (isset($display) && !empty($display->definition['uses_hook_block'])) {
$delta = $view
->id() . '-' . $display->display['id'];
$admin_label = $display
->getOption('block_description');
if (empty($admin_label)) {
if ($display->display['display_title'] == $display->definition['title']) {
$admin_label = $view
->label();
}
else {
// Allow translators to control the punctuation. Plugin
// definitions get cached, so use TranslatableMarkup() instead of
// t() to avoid double escaping when $admin_label is rendered
// during requests that use the cached definition.
$admin_label = new TranslatableMarkup('@view: @display', [
'@view' => $view
->label(),
'@display' => $display->display['display_title'],
]);
}
}
$this->derivatives[$delta] = [
'category' => $display
->getOption('block_category'),
'admin_label' => $admin_label,
'config_dependencies' => [
'config' => [
$view
->getConfigDependencyName(),
],
],
];
// Look for arguments and expose them as context.
foreach ($display
->getHandlers('argument') as $argument_name => $argument) {
/** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
if ($context_definition = $argument
->getContextDefinition()) {
$this->derivatives[$delta]['context_definitions'][$argument_name] = $context_definition;
}
}
$this->derivatives[$delta] += $base_plugin_definition;
}
}
}
return $this->derivatives;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ViewsBlock:: |
protected | property | The base plugin ID. | |
ViewsBlock:: |
protected | property | List of derivative definitions. | |
ViewsBlock:: |
protected | property | The view storage. | |
ViewsBlock:: |
public static | function |
Creates a new class instance. Overrides ContainerDeriverInterface:: |
|
ViewsBlock:: |
public | function |
Gets the definition of a derivative plugin. Overrides DeriverInterface:: |
|
ViewsBlock:: |
public | function |
Gets the definition of all derivatives of a base plugin. Overrides DeriverInterface:: |
|
ViewsBlock:: |
public | function | Constructs a ViewsBlock object. |