2

I have a theme setting that allows the administrator users to specify how many teasers should be shown on the home page by selecting a number from 1 to 4 (through a <select> field). Is it possible to reference this variable so I can use it in Views? In that way, I can let Views check that value in the theme settings, then display the number of entries accordingly.

2

This has been answered before over at Is it possible to dynamically set Views' pager settings?

That example is very well document. The only difference is that you will want to use theme_get_setting to get your count instead.

1

You could do this, but it will require that you know a bit about views exports.

You would have to create the view in code, by exporting it, this could be done with features or the views alone and use hook_default_views.

Once views read the view from code instead of the database you could insert a dynamic value for the number of items shown by the view, gotten from the theme setting. You would need to make sure that the value is formatted the way views expects it.

One important note. You can't make any changes to the way, as it will stop reading from the code and use the database instead. When this happens the dynamic value will be replace with a static one.

  • Boohoo, I don't know much about exports. Would it be too much to ask if you can give me a sample code I can customize? – enchance Jun 16 '11 at 14:53
  • 1
    @enchance exports are a wall of text. If you understand php array and objects you should be able to find what you need. – googletorp Jun 16 '11 at 15:08
0

Adapted from https://stackoverflow.com/questions/4739672/select-number-of-items-in-drupal-views/4742406#4742406.

You can accomplish this with hook_views_pre_build by altering the pager property. print_r it to see what is available. If your case, you would need to get the theme variable into the hook, but this does somewhat do things backwards (modules should be theme agnostic).

The code would look something like:

function MYMODULE_views_pre_build(&$view) {
  $perpage = some_way_to_get_the_variable();

  if (is_numeric($perpage) && (int) $perpage > 1) {
    $view->pager["use_pager"] = FALSE;
    $view->pager["items_per_page"] = (int) $perpage;
  }
}

The link above to SO has a more a more generic version.

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.