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 drupal.stackexchange.com/questions/1561/… That example is very well document. The only difference is that you will want to use theme_get_setting to get your count instead.
    – ericduran
    Commented Jun 17, 2011 at 1:41
  • 1
    Should this question be closed? If you look at my answer you will notice that the question is a duplicate and the answer on the other question applies to this question.
    – ericduran
    Commented Jun 17, 2011 at 1:49

2 Answers 2

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.

2
  • 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
    Commented Jun 16, 2011 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
    Commented Jun 16, 2011 at 15:08
1

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.

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