3 namespace Drupal\views\Plugin\Derivative;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
7 use Drupal\Core\StringTranslation\TranslatableMarkup;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Provides block plugin definitions for all Views block displays.
13 * @see \Drupal\views\Plugin\Block\ViewsBlock
15 class ViewsBlock implements ContainerDeriverInterface {
18 * List of derivative definitions.
22 protected $derivatives = [];
29 protected $basePluginId;
34 * @var \Drupal\Core\Entity\EntityStorageInterface
36 protected $viewStorage;
41 public static function create(ContainerInterface $container, $base_plugin_id) {
44 $container->get('entity.manager')->getStorage('view')
49 * Constructs a ViewsBlock object.
51 * @param string $base_plugin_id
53 * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
54 * The entity storage to load views.
56 public function __construct($base_plugin_id, EntityStorageInterface $view_storage) {
57 $this->basePluginId = $base_plugin_id;
58 $this->viewStorage = $view_storage;
64 public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
65 if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
66 return $this->derivatives[$derivative_id];
68 $this->getDerivativeDefinitions($base_plugin_definition);
69 return $this->derivatives[$derivative_id];
75 public function getDerivativeDefinitions($base_plugin_definition) {
76 // Check all Views for block displays.
77 foreach ($this->viewStorage->loadMultiple() as $view) {
78 // Do not return results for disabled views.
79 if (!$view->status()) {
82 $executable = $view->getExecutable();
83 $executable->initDisplay();
84 foreach ($executable->displayHandlers as $display) {
85 /** @var \Drupal\views\Plugin\views\display\DisplayPluginInterface $display */
86 // Add a block plugin definition for each block display.
87 if (isset($display) && !empty($display->definition['uses_hook_block'])) {
88 $delta = $view->id() . '-' . $display->display['id'];
90 $admin_label = $display->getOption('block_description');
91 if (empty($admin_label)) {
92 if ($display->display['display_title'] == $display->definition['title']) {
93 $admin_label = $view->label();
96 // Allow translators to control the punctuation. Plugin
97 // definitions get cached, so use TranslatableMarkup() instead of
98 // t() to avoid double escaping when $admin_label is rendered
99 // during requests that use the cached definition.
100 $admin_label = new TranslatableMarkup('@view: @display', ['@view' => $view->label(), '@display' => $display->display['display_title']]);
104 $this->derivatives[$delta] = [
105 'category' => $display->getOption('block_category'),
106 'admin_label' => $admin_label,
107 'config_dependencies' => [
109 $view->getConfigDependencyName(),
114 // Look for arguments and expose them as context.
115 foreach ($display->getHandlers('argument') as $argument_name => $argument) {
116 /** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
117 if ($context_definition = $argument->getContextDefinition()) {
118 $this->derivatives[$delta]['context'][$argument_name] = $context_definition;
122 $this->derivatives[$delta] += $base_plugin_definition;
126 return $this->derivatives;