ViewsBlock.php 2.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

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

namespace Drupal\views\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DerivativeInterface;

/**
 * Provides block plugin definitions for all Views block displays.
 *
 * @see \Drupal\views\Plugin\block\block\ViewsBlock
 */
class ViewsBlock implements DerivativeInterface {

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

  /**
   * 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.
    foreach (views_get_all_views() as $view) {
      // Do not return results for disabled views.
44
      if (!$view->status()) {
45
46
47
48
49
50
51
        continue;
      }
      $executable = $view->get('executable');
      $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'])) {
52
          $delta = $view->id() . '-' . $display->display['id'];
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
          $desc = $display->getOption('block_description');

          if (empty($desc)) {
            if ($display->display['display_title'] == $display->definition['title']) {
              $desc = t('View: !view', array('!view' => $view->getHumanName()));
            }
            else {
              $desc = t('View: !view: !display', array('!view' => $view->getHumanName(), '!display' => $display->display['display_title']));
            }
          }
          $this->derivatives[$delta] = array(
            'subject' => $desc,
            'cache' => $display->getCacheType()
          );
          $this->derivatives[$delta] += $base_plugin_definition;
        }
      }
    }
    return $this->derivatives;
  }

}