We have a request from a client to create an interface where they select nodes in a Chosen box. We have the Chosen module on the site and have successfully implemented features with it.

In this case, they want to have an admin interface where they choose which nodes populate a javascript image slider. I thought to simply make them a custom view where they can check a checkbox field to indicate whether to include the node or not. However, because they want the Chosen functionality, I need to populate a select box with the values from a view.

How can I create a select box that's populated by values from a view I've already defined? I've done some searching and found the Entity Reference View Widget module, but that doesn't seem to do what I am looking for.

Edit I'm not so concerned about the Chosen functionality, I can handle that. What I really want to know is how to get the values from a view to populate a select box, either with a widget, or defining a select box in an form array.

  • Did u try it by creating a custom module? And where are u creating that select box?
    – DarkteK
    Jul 12, 2016 at 4:54
  • @GonzaloJarjury I haven't tried it by creating a custom module-- how would you do that? The select box is going in a regular form array.
    – user1359
    Jul 13, 2016 at 14:48
  • your field type in content type is entity reference, is it right?
    – Yuseferi
    Jul 13, 2016 at 21:13
  • @zhilevan Actually I want to use the node id and the title.
    – user1359
    Jul 14, 2016 at 2:16
  • 1
    @user1359 I don't understand what you want, I just concluded you want to have a view that one of view fields is list of (chosen) entities(node+[nid]) with specified order , is it right? I can't help you if you clear the subject for me.
    – Yuseferi
    Jul 14, 2016 at 6:24

1 Answer 1


You can programmatically invoke a view to retrieve data and process it as needed:

  // Example select form array
  $form['my_selectbox'] = array(
    '#title' => t('My Select Label'),
    '#type' => 'select',
    '#description' => t('My select label.'),
    '#options' => array(),

  $view = views_get_view("my_view_machine_name");

  //use "default" for default display

  // @TODO: Alter any other view properties 
  // (e.g. exposed_input, offset, etc.) as needed

  // Pre execute & execute view query

  // $view->result contains an array of row objects inside the view.
  // the properties will vary based on your view
  foreach ($view->result as $row) {
    $form['my_selectbox']['#options'][$row->nid] = $row->node_title;
  • How do you actually loop through the rows to get the values from them? I want the option values of the select box to be the nid, and the content in the option tag to be the title.
    – user1359
    Jul 14, 2016 at 13:36
  • 1
    There was no example code work with so it was intentionally vague. An example has given in the answer, but it will vary based on your view.
    – Shawn Conn
    Jul 14, 2016 at 17:34
  • While this might answer the question, the functionality seems to be incomplete. What happens when a user changes the value of this select box? It might be out of scope for this question. But, it would be really helpful if some light is shed on that part too.
    – AjitS
    Jul 15, 2016 at 0:46
  • @AjitS it is out of the scope of this question.
    – user1359
    Jul 15, 2016 at 2:33

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.