0

I know I am able to create a view programatically here: "How do I programatically create a view?" - only this requires knowing the structure of the view (e.g. which fields / filters / relationships to use) already.

How can I dynamically create a view at runtime? Either the view would get stored in the same place as typical views (in the views database) - or it could exist 'In code' except the code is dynamically generated. E.g. is it possible just to create views ad hoc in hook_views_default_views()?

4
  • Yeah absolutely, the Views module isn't bothered about how you build up the objects/arrays in hook_views_default_views(), you can build it dynamically or statically, totally your choice
    – Clive
    Commented Oct 21, 2015 at 14:52
  • ok - but do you know if this gets cached? Commented Oct 21, 2015 at 14:58
  • I jsut checked and it does - so if I were to go down this route, I would need to be able to reset the cache somehow if my views structure changed Commented Oct 21, 2015 at 15:01
  • Exactly right, yes
    – Clive
    Commented Oct 21, 2015 at 15:15

1 Answer 1

1

Ok - no answers and I've figured out two ways to do this.

1. hook_views_default_views()

One way is to define the view in hook_views_default_views() and then to rest the cache for instance with views_invalidate_cache(). These views end up being 'in code' but not actually set. This didn't seem like the most desirable method for my use - but might be better for some applications.

2. save the $view

Create the view e.g. $view = new view(); and fill all the fields, and then $view->save() saves it to the views database. Keeping a record of the name of the view allows you to delete it later if necessary etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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