ViewsBlock.php 3.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\views\Plugin\Derivative\ViewsBlock.
 */

namespace Drupal\views\Plugin\Derivative;

10
11
12
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
15
16
17
18

/**
 * Provides block plugin definitions for all Views block displays.
 *
 * @see \Drupal\views\Plugin\block\block\ViewsBlock
 */
19
class ViewsBlock implements ContainerDerivativeInterface {
20
21
22
23
24
25
26
27

  /**
   * List of derivative definitions.
   *
   * @var array
   */
  protected $derivatives = array();

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  /**
   * The base plugin ID.
   *
   * @var string
   */
  protected $base_plugin_id;

  /**
   * The view storage controller.
   *
   * @var \Drupal\Core\Entity\EntityStorageControllerInterface
   */
  protected $viewStorageController;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $base_plugin_id,
48
      $container->get('entity.manager')->getStorageController('view')
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    );
  }

  /**
   * Constructs a ViewsBlock object.
   *
   * @param string $base_plugin_id
   *   The base plugin ID.
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $view_storage_controller
   *   The entity storage controller to load views.
   */
  public function __construct($base_plugin_id, EntityStorageControllerInterface $view_storage_controller) {
    $this->basePluginId = $base_plugin_id;
    $this->viewStorageController = $view_storage_controller;
  }

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  /**
   * Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
   */
  public function getDerivativeDefinition($derivative_id, array $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];
  }

  /**
   * Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
   */
  public function getDerivativeDefinitions(array $base_plugin_definition) {
    // Check all Views for block displays.
81
    foreach ($this->viewStorageController->loadMultiple() as $view) {
82
      // Do not return results for disabled views.
83
      if (!$view->status()) {
84
85
        continue;
      }
86
      $executable = $view->getExecutable();
87
88
89
90
      $executable->initDisplay();
      foreach ($executable->displayHandlers as $display) {
        // Add a block plugin definition for each block display.
        if (isset($display) && !empty($display->definition['uses_hook_block'])) {
91
          $delta = $view->id() . '-' . $display->display['id'];
92
93
94
95
          $desc = $display->getOption('block_description');

          if (empty($desc)) {
            if ($display->display['display_title'] == $display->definition['title']) {
96
              $desc = t('View: !view', array('!view' => $view->label()));
97
98
            }
            else {
99
              $desc = t('View: !view: !display', array('!view' => $view->label(), '!display' => $display->display['display_title']));
100
101
102
            }
          }
          $this->derivatives[$delta] = array(
103
            'category' => $display->getOption('block_category'),
104
            'admin_label' => $desc,
105
106
107
108
109
110
111
112
113
114
            'cache' => $display->getCacheType()
          );
          $this->derivatives[$delta] += $base_plugin_definition;
        }
      }
    }
    return $this->derivatives;
  }

}