There is a set of products in Drupal Commerce. How do I programmatically create a view that would select products for a particular field?

  • 1
    I don't think they are duplicates. The referenced question is asking about hook_views_default_views() for altering an existing view whereas this Q (seems to be) about creating a View programatically from scratch. – Chapabu Jun 20 '13 at 8:28
  • @Chapabu I trust if I'm mistaken people will vote for this one to stay. – Mołot Jun 20 '13 at 8:41
  • Not sure OP will return 8 months later and edit this post for better clarification. But it sounds like she was looking for some contextual filter stuff. Not to create a view object. – AyeshK Jun 21 '13 at 4:02
  1. Create your view the regular way (views export creates the code).
  2. Define your view API version and path for files that include view hooks by defining hook_views_api().
  3. paste in hook_views_default_views() the code you obtained in the first point.

See Creating views programmatically in Drupal 7 for more details.


The classic way is to create it manually, via UI, then click "export" (path like http://example.org/admin/structure/views/view/default_demo/export), and wrap resulting code into a module using hook_views_default_views(), maybe fine-tuning details.

  • 2
    Why did you just repeat my answer? – ram4nd Jan 28 '16 at 8:48
  • 2
    @ram4nd not quite repeat. Your answer lacked (and still lack) detailed info where to find export, and was poorly written (edited months after I posted mine). – Mołot Jan 28 '16 at 9:00
  • That's usually where adding a comment comes in. Usually you only post another answer if there's something fundamentally different about what you're saying. – Bratchley Jun 1 '17 at 16:35

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.