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..c427a15 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,6 +47,13 @@ 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; } @@ -85,6 +96,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 +173,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 +200,97 @@ 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; } } /** + * Provides the block settings form. + */ + public function blockForm(ViewsBlock $block, &$form, &$form_state) { + $allow_settings = array_filter($this->getOption('allow')); + + if (!empty($allow_settings)) { + $settings['override'] = array( + '#type' => 'details', + '#title' => t('Override settings'), + '#collapsed' => FALSE, + '#collapsible' => FALSE, + '#weight' => 8, + ); + } + + $block_configuration = $block->getConfig(); + + foreach ($allow_settings as $setting) { + switch ($setting) { + case 'items_per_page': + $settings['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': + $settings['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'], + ); + } + } + + return $settings; + } + + /** + * @todo + */ + public function blockValidate(ViewsBlock $block, $form, &$form_state) { + } + + /** + * @todo + */ + public function blockSubmit(ViewsBlock $block, $form, &$form_state) { + if (isset($form_state['values']['items_per_page']) && $form_state['values']['items_per_page'] !== '') { + $block->setConfig('items_per_page', $form_state['values']['items_per_page']); + } + if (isset($form_state['values']['more_link']) && $form_state['values']['more_link'] !== '') { + $block->setConfig('more_link', $form_state['values']['more_link']); + } + } + + /** + * Allow to change the display settings right before execute 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() { @@ -195,3 +313,4 @@ public function remove() { } } + 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 1e2bfd5..f7837db 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php @@ -66,13 +66,52 @@ 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; } /** + * {@inheritdoc} + */ + public function settings() { + $settings = parent::settings(); + + return $this->view->display_handler->blockSettings($settings); + } + + /** + * {@inheritdoc} + */ + public function blockForm($form, &$form_state) { + parent::blockForm($form, $form_state); + + return $this->view->display_handler->blockForm($this, $form, $form_state); + } + + + + public function blockValidate($form, &$form_state) { + parent::blockValidate($form, $form_state); + + $this->view->display_handler->blockValidate($this, $form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function blockSubmit($form, &$form_state) { + parent::blockSubmit($form, $form_state); + + $this->view->display_handler->blockSubmit($this, $form, $form_state); + } + + + /** * Implements \Drupal\block\BlockBase::blockBuild(). */ protected function blockBuild() { + $this->view->setDisplay($this->displayID); + $this->view->display_handler->preBlockBuild($this); $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());