class ViewsBlock

Same name in this branch

Provides a generic Views block.

Plugin annotation


@Plugin(
  id = "views_block",
  admin_label = @Translation("Views Block"),
  module = "views",
  derivative = "Drupal\views\Plugin\Derivative\ViewsBlock"
)

Hierarchy

Expanded class hierarchy of ViewsBlock

File

drupal/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php, line 24
Contains \Drupal\views\Plugin\Block\ViewsBlock.

Namespace

Drupal\views\Plugin\Block
View source
class ViewsBlock extends BlockBase {

  /**
   * The View executable object.
   *
   * @var \Drupal\views\ViewExecutable
   */
  protected $view;

  /**
   * The display ID being used for this View.
   *
   * @var string
   */
  protected $displayID;

  /**
   * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    list($plugin, $delta) = explode(':', $this
      ->getPluginId());
    list($name, $this->displayID) = explode('-', $delta, 2);

    // Load the view.
    $this->view = views_get_view($name);
  }

  /**
   * Overrides \Drupal\block\BlockBase::access().
   */
  public function access() {
    return $this->view
      ->access($this->displayID);
  }

  /**
   * Overrides \Drupal\block\BlockBase::form().
   */
  public function form($form, &$form_state) {
    $form = parent::form($form, $form_state);

    // Set the default label to '' so the views internal title is used.
    $form['label']['#default_value'] = '';
    $form['label']['#access'] = FALSE;
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    if ($output = $this->view
      ->executeDisplay($this->displayID)) {
      $output = $this->view
        ->executeDisplay($this->displayID);

      // Set the label to the title configured in the view.
      $this->configuration['label'] = filter_xss_admin($this->view
        ->getTitle());

      // Before returning the block output, convert it to a renderable array
      // with contextual links.
      $this
        ->addContextualLinks($output);
      $this->view
        ->destroy();
      return $output;
    }
    return array();
  }

  /**
   * Converts Views block content to a renderable array with contextual links.
   *
   * @param string|array $output
   *   An string|array representing the block. This will be modified to be a
   *   renderable array, containing the optional '#contextual_links' property (if
   *   there are any contextual links associated with the block).
   * @param string $block_type
   *   The type of the block. If it's 'block' it's a regular views display,
   *   but 'exposed_filter' exist as well.
   */
  protected function addContextualLinks(&$output, $block_type = 'block') {

    // Do not add contextual links to an empty block.
    if (!empty($output)) {

      // Contextual links only work on blocks whose content is a renderable
      // array, so if the block contains a string of already-rendered markup,
      // convert it to an array.
      if (is_string($output)) {
        $output = array(
          '#markup' => $output,
        );
      }

      // Add the contextual links.
      views_add_contextual_links($output, $block_type, $this->view, $this->displayID);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockBase::blockForm public function Returns the configuration form elements specific to this block plugin. 11
BlockBase::blockSubmit public function Adds block type-specific submission handling for the block form. 11
BlockBase::blockValidate public function Adds block type-specific validation for the block form.
BlockBase::getConfig public function Returns the configuration data for the block plugin.
BlockBase::setConfig public function Sets a particular value in the block settings.
BlockBase::settings public function Returns plugin-specific settings for the block. Overrides BlockPluginInterface::settings 13
BlockBase::submit public function Implements \Drupal\block\BlockPluginInterface::submit(). Overrides BlockPluginInterface::submit
BlockBase::validate public function Implements \Drupal\block\BlockPluginInterface::validate(). Overrides BlockPluginInterface::validate
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
ViewsBlock::$displayID protected property The display ID being used for this View.
ViewsBlock::$view protected property The View executable object.
ViewsBlock::access public function Overrides \Drupal\block\BlockBase::access(). Overrides BlockBase::access
ViewsBlock::addContextualLinks protected function Converts Views block content to a renderable array with contextual links.
ViewsBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build 1
ViewsBlock::form public function Overrides \Drupal\block\BlockBase::form(). Overrides BlockBase::form
ViewsBlock::__construct public function Overrides \Drupal\Component\Plugin\PluginBase::__construct(). Overrides BlockBase::__construct