diff --git a/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php b/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php index 4a3bf10..a9cacbc 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php +++ b/core/modules/block/lib/Drupal/block/Plugin/views/display/Block.php @@ -10,6 +10,7 @@ use Drupal\Component\Annotation\Plugin; use Drupal\Core\Annotation\Translation; +use Drupal\views\Plugin\block\block\ViewsBlock; use Drupal\views\Plugin\views\display\DisplayPluginBase; /** @@ -27,6 +28,9 @@ * contextual_links_locations = {"block"}, * admin = @Translation("Block") * ) + * + * @see \Drupal\views\Plugin\block\block\ViewsBlock + * @see \Drupal\views\Plugin\Derivative\ViewsBlock */ class Block extends DisplayPluginBase { @@ -43,10 +47,35 @@ protected function defineOptions() { $options['block_description'] = array('default' => '', 'translatable' => TRUE); $options['block_caching'] = array('default' => DRUPAL_NO_CACHE); + $options['allow'] = array( + 'contains' => array( + 'items_per_page' => array('default' => TRUE), + 'more_link' => array('default' => TRUE), + ), + ); + return $options; } /** + * Returns plugin-specific settings for the block. + * + * @param array $settings + * The settings of the block. + * + * @return array + * An array of block-specific settings to override the defaults provided in + * \Drupal\views\Plugin\Block\ViewsBlock::settings(). + * + * @see \Drupal\views\Plugin\Block\ViewsBlock::settings(). + */ + public function blockSettings(array $settings) { + $settings['items_per_page'] = FALSE; + $settings['more_link'] = FALSE; + return $settings; + } + + /** * The display block handler returns the structure necessary for a block. */ public function execute() { @@ -85,6 +114,15 @@ public function optionsSummary(&$categories, &$options) { 'value' => views_ui_truncate($block_description, 24), ); + $allow = $this->getOption('allow'); + $filtered_allow = array_filter($allow); + + $options['allow'] = array( + 'category' => 'block', + 'title' => t('Allow settings'), + 'value' => empty($filtered_allow) ? t('None') : ($allow === $filtered_allow ? t('All') : t('Some')), + ); + $types = $this->blockCachingModes(); $options['block_caching'] = array( 'category' => 'other', @@ -153,6 +191,22 @@ public function buildOptionsForm(&$form, &$form_state) { '#markup' => '
' . t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '
', ); } + break; + case 'allow': + $form['#title'] .= t('Allow settings in the block configuration'); + + $options = array( + 'items_per_page' => t('Items per page'), + 'more_link' => t('More link'), + ); + + $allow = array_filter($this->getOption('allow')); + $form['allow'] = array( + '#type' => 'checkboxes', + '#default_value' => $allow, + '#options' => $options, + ); + break; } } @@ -164,15 +218,133 @@ public function submitOptionsForm(&$form, &$form_state) { parent::submitOptionsForm($form, $form_state); switch ($form_state['section']) { case 'block_description': - $this->setOption('block_description', $form_state['values']['block_description']); - break; case 'block_caching': - $this->setOption('block_caching', $form_state['values']['block_caching']); + case 'allow': + $this->setOption($form_state['section'], $form_state['values'][$form_state['section']]); break; } } /** + * Adds the configuration form elements specific to this views block plugin. + * + * This method allows block instances to override the views items_per_page and + * more_link settings. + * + * @param \Drupal\views\Plugin\Block\ViewsBlock $block + * The ViewsBlock plugin. + * @param array $form + * The form definition array for the block configuration form. + * @param array $form_state + * An array containing the current state of the configuration form. + * + * @return array $form + * The renderable form array representing the entire configuration form. + * + * @see \Drupal\views\Plugin\Block\ViewsBlock::blockForm() + */ + public function blockForm(ViewsBlock $block, array &$form, array &$form_state) { + $allow_settings = array_filter($this->getOption('allow')); + + if (!empty($allow_settings)) { + $form['override'] = array( + '#type' => 'details', + '#title' => t('Override settings'), + '#open' => TRUE, + '#weight' => 8, + ); + } + + $block_configuration = $block->getConfig(); + + foreach ($allow_settings as $type => $enabled) { + if (!$enabled) { + continue; + } + switch ($type) { + case 'items_per_page': + $form['override']['items_per_page'] = array( + '#type' => 'select', + '#title' => t('Items per page'), + '#options' => array( + '' => t('Override items per page'), + 5 => 5, + 10 => 10, + 20 => 20, + 40 => 40, + ), + '#default_value' => $block_configuration['items_per_page'], + ); + break; + case 'more_link': + $form['override']['more_link'] = array( + '#type' => 'select', + '#title' => t('More link'), + '#options' => array( + '' => t('Override more link'), + 0 => t('No more link'), + 1 => t('Display more link'), + ), + '#default_value' => $block_configuration['more_link'], + ); + break; + } + } + + return $form; + } + + /** + * Handles form validation for the views block configuration form. + * + * @param \Drupal\views\Plugin\Block\ViewsBlock $block + * The ViewsBlock plugin. + * @param array $form + * The form definition array for the block configuration form. + * @param array $form_state + * An array containing the current state of the configuration form. + * + * @see \Drupal\views\Plugin\Block\ViewsBlock::blockValidate() + */ + public function blockValidate(ViewsBlock $block, array $form, array &$form_state) { + } + + /** + * Handles form submission for the views block configuration form. + * + * @param \Drupal\views\Plugin\Block\ViewsBlock $block + * The ViewsBlock plugin. + * @param array $form + * The form definition array for the full block configuration form. + * @param array $form_state + * An array containing the current state of the configuration form. + * + * * @see \Drupal\views\Plugin\Block\ViewsBlock::blockSubmit() + */ + public function blockSubmit(ViewsBlock $block, $form, &$form_state) { + if (isset($form_state['values']['override']['items_per_page']) && $form_state['values']['override']['items_per_page'] !== '') { + $block->setConfig('items_per_page', $form_state['values']['override']['items_per_page']); + } + if (isset($form_state['values']['override']['more_link']) && $form_state['values']['override']['more_link'] !== '') { + $block->setConfig('more_link', $form_state['values']['override']['more_link']); + } + } + + /** + * Allows to change the display settings right before executing the block. + */ + public function preBlockBuild(ViewsBlock $block) { + $block_configuration = $block->getConfig(); + if (isset($block_configuration['items_per_page']) && $block_configuration['items_per_page'] !== '') { + $this->view->setItemsPerPage($block_configuration['items_per_page']); + } + if (isset($block_configuration['more_link']) && $block_configuration['more_link'] !== '') { + $this->setOption('use_more_always', 1); + } + } + + + /** * Block views use exposed widgets only if AJAX is set. */ public function usesExposed() { diff --git a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php index 2c0481e..277fac1 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php @@ -9,8 +9,14 @@ use Drupal\block\BlockBase; use Drupal\Component\Annotation\Plugin; +use Drupal\Component\Utility\Xss; use Drupal\Core\Annotation\Translation; +use Drupal\Core\Entity\EntityManager; use Drupal\Core\Entity\EntityStorageControllerInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\views\ViewExecutable; +use Drupal\views\ViewExecutableFactory; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a generic Views block. @@ -22,7 +28,7 @@ * derivative = "Drupal\views\Plugin\Derivative\ViewsBlock" * ) */ -class ViewsBlock extends BlockBase { +class ViewsBlock extends BlockBase implements ContainerFactoryPluginInterface { /** * The View executable object. @@ -39,15 +45,40 @@ class ViewsBlock extends BlockBase { protected $displayID; /** - * Overrides \Drupal\Component\Plugin\PluginBase::__construct(). + * Constructs a Drupal\Component\Plugin\PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\views\ViewExecutableFactory $executable_factory + * The view executable factory. + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller + * The views storage controller. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - + public function __construct(array $configuration, $plugin_id, array $plugin_definition, ViewExecutableFactory $executable_factory, EntityStorageControllerInterface $storage_controller) { + $this->pluginId = $plugin_id; list($plugin, $delta) = explode(':', $this->getPluginId()); list($name, $this->displayID) = explode('-', $delta, 2); // Load the view. - $this->view = views_get_view($name); + $views = $storage_controller->load(array($name)); + $view = reset($views); + $this->view = $executable_factory->get($view); + + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, $plugin_id, $plugin_definition, + $container->get('views.executable'), + $container->get('plugin.manager.entity')->getStorageController('view') + ); } /** @@ -66,6 +97,7 @@ public function 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; } @@ -73,10 +105,21 @@ public function form($form, &$form_state) { * {@inheritdoc} */ public function build() { + $config = $this->getConfig(); + if ($config['items_per_page'] !== '') { + $this->view->setItemsPerPage($config['items_per_page']); + } + + if ($config['more_link'] === 0) { + $this->view->display_handler->setOption('use_more', TRUE); + } + elseif ($config['more_link'] === 1) { + $this->view->display_handler->setOption('use_more', FALSE); + } + 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()); + $this->configuration['label'] = Xss::filterAdmin($this->view->getTitle()); // Before returning the block output, convert it to a renderable array // with contextual links. $this->addContextualLinks($output); @@ -89,6 +132,46 @@ public function build() { } /** + * {@inheritdoc} + */ + public function settings() { + $settings = parent::settings(); + + $this->view->setDisplay($this->displayID); + return $this->view->display_handler->blockSettings($settings); + } + + /** + * {@inheritdoc} + */ + public function blockForm($form, &$form_state) { + parent::blockForm($form, $form_state); + + $this->view->setDisplay($this->displayID); + return $this->view->display_handler->blockForm($this, $form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function blockValidate($form, &$form_state) { + parent::blockValidate($form, $form_state); + + $this->view->setDisplay($this->displayID); + $this->view->display_handler->blockValidate($this, $form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function blockSubmit($form, &$form_state) { + parent::blockSubmit($form, $form_state); + + $this->view->setDisplay($this->displayID); + $this->view->display_handler->blockSubmit($this, $form, $form_state); + } + + /** * Converts Views block content to a renderable array with contextual links. * * @param string|array $output @@ -109,7 +192,9 @@ protected function addContextualLinks(&$output, $block_type = 'block') { $output = array('#markup' => $output); } // Add the contextual links. - views_add_contextual_links($output, $block_type, $this->view, $this->displayID); + if (function_exists('views_add_contextual_links')) { + views_add_contextual_links($output, $block_type, $this->view, $this->displayID); + } } } diff --git a/core/modules/views/tests/Drupal/views/Tests/Plugin/Block/ViewsBlockTest.php b/core/modules/views/tests/Drupal/views/Tests/Plugin/Block/ViewsBlockTest.php new file mode 100644 index 0000000..65d4171 --- /dev/null +++ b/core/modules/views/tests/Drupal/views/Tests/Plugin/Block/ViewsBlockTest.php @@ -0,0 +1,209 @@ + ' Block: Views block', + 'description' => 'Tests the views block plugin.', + 'group' => 'Views module integration', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); // TODO: Change the autogenerated stub + + $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable') + ->disableOriginalConstructor() + ->setMethods(array('executeDisplay', 'setDisplay', 'destroy')) + ->getMock(); + + $this->executable->display_handler = $this->getMockBuilder('Drupal\block\Plugin\views\display\Block') + ->disableOriginalConstructor() + ->getMock(); + $this->executable->display_handler->expects($this->any()) + ->method('blockSettings') + ->will($this->returnValue(array('items_per_page' => '', 'more_link' => ''))); + + $this->view = $this->getMockBuilder('Drupal\views\Plugin\Core\Entity\View') + ->disableOriginalConstructor() + ->getMock(); + + $this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory') + ->getMock(); + $this->executableFactory->staticExpects($this->any()) + ->method('get') + ->with($this->equalTo($this->view)) + ->will($this->returnValue($this->executable)); + + $this->storageController = $this->getMockBuilder('Drupal\views\ViewStorageController') + ->disableOriginalConstructor() + ->getMock(); + + $this->storageController->expects($this->any()) + ->method('load') + ->with($this->equalTo(array('test_view'))) + ->will($this->returnValue(array($this->view))); + } + + /** + * Test the build method. + * + * @see \Drupal\views\Plugin\block\ViewsBlock::build() + */ + public function testBuild() { + $output = $this->randomName(100); + $build = array('#markup' => $output); + $this->executable->expects($this->once()) + ->method('executeDisplay') + ->with($this->equalTo('block_1')) + ->will($this->returnValue($build)); + + $this->executable->expects($this->once()) + ->method('destroy') + ->with() + ->will($this->returnValue(TRUE)); + + $block_id = 'views_block:test_view-block_1'; + $config = array(); + $definition = array(); + $definition['module'] = 'views'; + $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storageController); + + $this->assertEquals($build, $plugin->build()); + } + + /** + * Test the build method with a failed execution. + * + * @see \Drupal\views\Plugin\block\ViewsBlock::build() + */ + public function testBuildFailed() { + $output = FALSE; + $this->executable->expects($this->once()) + ->method('executeDisplay') + ->with($this->equalTo('block_1')) + ->will($this->returnValue($output)); + + $this->executable->expects($this->never()) + ->method('destroy') + ->with() + ->will($this->returnValue(TRUE)); + + $block_id = 'views_block:test_view-block_1'; + $config = array(); + $definition = array(); + $definition['module'] = 'views'; + $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storageController); + + $this->assertEquals(array(), $plugin->build()); + } + + /** + * Test the build method with no overriding. + */ + public function testBuildNoOverride() { + $this->view->expects($this->never()) + ->method('setItemsPerPage'); + $this->view->display_handler->expects($this->never()) + ->method('setOption'); + + $block_id = 'views_block:test_view-block_1'; + $config = array('items_per_page' => '', 'more_link' => ''); + $definition = array(); + $definition['module'] = 'views'; + $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storageController); + + $plugin->build(); + } + + /** + * Tests the build method with overriding items per page and the more link. + */ + public function testBuildOverride() { + $this->view->expects($this->once()) + ->method('setItemsPerPage') + ->with($this->equalTo(5)); + $this->view->display_handler->expects($this->once()) + ->method('setOption') + ->with($this->equalTo(array('use_more', TRUE))); + + $block_id = 'views_block:test_view-block_1'; + $config = array('items_per_page' => 5, 'more_link' => 1); + $definition = array(); + $definition['module'] = 'views'; + $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storageController); + + $plugin->build(); + } + +} + +} + +// @todo Remove this once t() is converted to a service. +namespace { + if (!function_exists('t')) { + function t($string) { + return $string; + } + } +}