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..e2314fd 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' => '
', ); } + 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,129 @@ 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 $setting) { + switch ($setting) { + 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'], + ); + } + } + + 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']['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']); + } + } + + /** + * 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() { @@ -195,3 +345,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 2c0481e..eb61e6b 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Block/ViewsBlock.php @@ -66,6 +66,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; } @@ -89,6 +90,60 @@ public function build() { } /** + * {@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); + } + + /** + * {@inheritdoc} + */ + 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); + } + + + /** + * {@inheritdoc} + */ + 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()); + // Before returning the block output, convert it to a renderable array + // with contextual links. + $this->addContextualLinks($output); + + $this->view->destroy(); + return $output; + } + + /** * Converts Views block content to a renderable array with contextual links. * * @param string|array $output