Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save idiazroncero/061bd3b6756cf8bee46b34cfc5f7c3e2 to your computer and use it in GitHub Desktop.
Save idiazroncero/061bd3b6756cf8bee46b34cfc5f7c3e2 to your computer and use it in GitHub Desktop.
diff --git a/core/modules/views/config/schema/views.schema.yml b/core/modules/views/config/schema/views.schema.yml
index 21f606a329..5d2e57f31a 100644
--- a/core/modules/views/config/schema/views.schema.yml
+++ b/core/modules/views/config/schema/views.schema.yml
@@ -139,6 +139,9 @@ views_block:
views_label:
type: label
label: 'Title'
+ offset:
+ type: integer
+ label: 'Offset'
items_per_page:
type: string
label: 'Items per block'
diff --git a/core/modules/views/src/Plugin/views/display/Block.php b/core/modules/views/src/Plugin/views/display/Block.php
index bcdab29ecd..24ff49d92c 100644
--- a/core/modules/views/src/Plugin/views/display/Block.php
+++ b/core/modules/views/src/Plugin/views/display/Block.php
@@ -106,6 +106,7 @@ protected function defineOptions() {
$options['allow'] = [
'contains' => [
'items_per_page' => ['default' => 'items_per_page'],
+ 'offset' => ['default' => 'offset'],
],
];
@@ -126,6 +127,7 @@ protected function defineOptions() {
*/
public function blockSettings(array $settings) {
$settings['items_per_page'] = 'none';
+ $settings['offset'] = 'none';
return $settings;
}
@@ -240,6 +242,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$options = [
'items_per_page' => $this->t('Items per page'),
+ 'offset' => $this->t('Offset'),
];
$allow = array_filter($this->getOption('allow'));
@@ -302,14 +305,45 @@ public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $f
'#title' => $this->t('Items per block'),
'#options' => [
'none' => $this->t('@count (default setting)', ['@count' => $this->getPlugin('pager')->getItemsPerPage()]),
+ 1 => 1,
+ 2 => 2,
+ 3 => 3,
+ 4 => 4,
5 => 5,
+ 6 => 6,
+ 7 => 7,
+ 8 => 8,
+ 9 => 9,
10 => 10,
+ 12 => 12,
20 => 20,
+ 24 => 24,
40 => 40,
+ 48 => 48,
],
'#default_value' => $block_configuration['items_per_page'],
];
break;
+ case 'offset':
+ $form['override']['offset'] = array(
+ '#type' => 'select',
+ '#title' => $this->t('Offset'),
+ '#options' => [
+ 'none' => $this->t('@count (default setting)', array('@count' => $this->getPlugin('pager')->getOffset())),
+ 1 => 1,
+ 2 => 2,
+ 3 => 3,
+ 4 => 4,
+ 5 => 5,
+ 6 => 6,
+ 7 => 7,
+ 8 => 8,
+ 9 => 9,
+ 10 => 10,
+ ],
+ '#default_value' => !empty($block_configuration['offset']) ? $block_configuration['offset'] : 'none',
+ );
+ break;
}
}
@@ -348,6 +382,10 @@ public function blockSubmit(ViewsBlock $block, $form, FormStateInterface $form_s
$block->setConfigurationValue('items_per_page', $items_per_page);
}
$form_state->unsetValue(['override', 'items_per_page']);
+ if ($offset = $form_state->getValue(array('override', 'offset'))) {
+ $block->setConfigurationValue('offset', $offset);
+ }
+ $form_state->unsetValue(array('override', 'offset'));
}
/**
@@ -361,6 +399,9 @@ public function preBlockBuild(ViewsBlock $block) {
if ($config['items_per_page'] !== 'none') {
$this->view->setItemsPerPage($config['items_per_page']);
}
+ if (!empty($config['offset']) && $config['offset'] !== 'none') {
+ $this->view->setOffset($config['offset']);
+ }
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment