I am programmatically generating views based on an entity with varying fields and saving them with $view->save(). Just to be clear, I have to do this because my entities have x-amount of fields, and the view needs to display all the fields.

I am thinking that creating all these views in the database might not be a good long term plan, especially when I go into the Views UI, and see a billion views there.

Can I create a one-off view (e.g. typical Views export code producing a block), and print its block to a page without saving it?

  • I am not sure what you mean by my entities have x-amount of fields, and the view needs to display all the fields. As far I recall, the Views module doesn't have a problem with displaying all the fields an entity has.
    – apaderno
    Commented Jan 20, 2016 at 17:16
  • AFAIK you need to specify which fields to show (e.g. if you have 3 fields, you need to add 3 columns to display each one). Because each entity has a different set of fields, each entity needs it's own seperate view. Regardless I can't create a single view, I just need to be able to create on on the fly. Commented Jan 20, 2016 at 17:19

1 Answer 1


I figured it out myself in the end - by looking at how Views does it itself in views_block_view().

function page_callback($my_entity) {
  //create view
  $view = new view();

   ... //Do all the views creation stuff e.g. code in export view

  $display_id = 'block_1';
  $view->new_display('block', 'Block', $display_id);

  if ($view->access($display_id)) {
    $output = $view->execute_display($display_id);
    views_add_block_contextual_links($output, $view, $display_id);
    return $output;

The above code renders a view that is created from scratch and never saved to a database. If you wanted to do a page you could look at the code in views_page().

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.