3

In Drupal 8, whenever you create a view block, you have the option to specify the number of items displayed in that view block which is awesome.

Currently, you can choose between 1, 5, 10, and 20. Is there any way to change this to include 1, 2, 3, 4, 5" or just be able to manually enter a number?

I was really excited about this feature and need to use it, but I need to really choose between 1, 2 or 3 items per block.

Edit: I'm referring to the block management screen, not the views configuration screen.

3

Thanks to the answer @Berdir provided, I've been able to replace the restrictive select field with a more flexible number field.

In mymodule.module

The form_id is block_form (andpanels_edit_block_form and panels_add_block_form for Panelizer). Change the #type to number to make it so that you can input a number. Unset the #options to avoid validation errors (Invalid choice).

/**
 * Implements hook_form_alter.
 *
 * Changes the input type of the "Items per block" setting of
 * a view-block in Blocks/Panelizer from 'select' to 'number',
 * so that amounts other than 5, 10, 20 and 40 can be chosen.
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if (in_array($form_id, ['block_form', 'panels_edit_block_form', 'panels_add_block_form'])) {
    if (isset($form['settings']['override']['items_per_page'])) {
      $form['settings']['override']['items_per_page']['#type'] = 'number';
      unset($form['settings']['override']['items_per_page']['#options']);
    }
  }
}
2

The solution is actually on the screenshot from @codenext.

In the Block settings, you can see the third entry line "Allowed settings". The summary for it is actually broken (it's not checked yet, so it shouldn't show), but if you click on it, you get a checkbox on "Allowed items".

Tick it, then you can configure the allowed items per block. Problem solved ;)

Edit: Being able to read the question would help:

Yes, the options are hardcoded in \Drupal\views\Plugin\views\display\Block::blockForm() (located in web/core/modules/views/src/Plugin/views/display/Block.php). Should be relatively possible to customize them with a form alter though, other than in there, the options are nowhere expected to be those values I think.

8
  • Hi @Berdir, actually what is happening is Allowed setting: Items per page, provides the option of only enabling or disabling the option of Items per page, which ultimately shows up in the Block UI (Not views block UI) with selection list of default, 5, 10, 20, 40. Hence under views block UI, there is no such option which may allow us to change the list 5, 10, 20, 40. @discg0lfer want to keep the option of "Images per page" under block configuration with customization list like 1,2,3,4... Hence it looks like, it need to override the Core Block's default option of 5, 10, 20, 40.
    – CodeNext
    May 18 '16 at 19:09
  • Ah. Being able to read would help. I'll update my answer.
    – Berdir
    May 18 '16 at 19:11
  • Yup man, it can be changed from there, I just did it for try, it is working. Please can you let me know what is the "Being able to read" is? I guess you are finding the source of that particular hard code, but how?..:)
    – CodeNext
    May 18 '16 at 19:42
  • Sorry if I was unclear there. I just meant that I was too stupid to properly read the question :) Clarified now.
    – Berdir
    May 18 '16 at 21:36
  • omg, Since I am learning stage in codes, I thought that it is coders some non-formal way of saying, of finding out which file does have that hard-code. So I started wondering is there any method to find from which file it is coming, hence asked that..:)
    – CodeNext
    May 18 '16 at 21:46
1

If i understood correctly, you are talking about the "Items per block" when we create the views block. But I am getting the option of 0,1,2,3,4.....as shown in the first image. Moreover if this is the issue, you can even change this setting after entering to the views user interface under PAGER option(as shown in second image), items per page. Block view page

Pager

4
  • Actually, I was referring to the block management screen. If you go to Structure > Block Layout > Configure (on a view block).
    – discg0lfer
    May 18 '16 at 12:37
  • Hmmm...I did check that one. You also would have observed that, the number we describes while creating the views block, it comes as the default in this list. Hence, it seems like it has been provided to the Block User Interface for Broad selection, like in above case I selected 2, hence it comes as 2, 5, 10..... I do not know why you want to change that, may be you need that way. But it has been provided this way purposefully there. So, you can choose the finer selection at views UI, and coarse selection at Block UI(in case u need), which overrides the views UI selection.
    – CodeNext
    May 18 '16 at 12:52
  • I have an odd use case where the content managers will need to (somtimes) change the number of items that are output. They are not going to edit views.
    – discg0lfer
    May 18 '16 at 12:59
  • Hhmmm....I did check and try some solution but no luck...
    – CodeNext
    May 18 '16 at 13:40
0

I think I understand this issue and can help solve it without hard coding. Unfortunately if you management doesn't want to do the adjustment in views this wont work for you. Simply set the block configuration for items per block for default setting first. Then go to your view and under pager set at display a specified number of items and set the number of items to the actual number you want. Save the view and to check to see if it is set, return to your block configuration and you should see your number set in the block.

0

It's obviously a good idea to re-use a views block in several places without the need to create display copies only because the desired number of items is different.

There's a good chance this patch makes it into Drupal core 8.9: https://www.drupal.org/project/drupal/issues/3083379

(Allowing 1, 2, 3, 4, 5, 6, 10, 12, 20, 24, 40, 48 items per block).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.