0

I have a drop down list of different views for the users to select on a content type page in Drupal 7. I need to get the value of selected view from the drop down and display it in the form of selected view.

$value = $_GET['select id'];
$view = views_get_view('value');
print $view->preview('default');

I am not sure from where I should get the ID of the drop down field.

1

Not sure I fully understand the issue it would help if you asked a direct question. Here are some thoughts though.

To get a list of applicalbe view checkout this funciton form the node reference module:

function references_get_views_options($entity_type) {
  // Filter views that contain a 'references' display. This actually returns a
  // list of displays (the same view appears several times).
  $displays = views_get_applicable_views('references display');

  // Filter views that list the entity type we want, and group the separate
  // displays by view.
  $entity_info = entity_get_info($entity_type);
  $options = array();
  foreach ($displays as $data) {
    list($view, $display_id) = $data;
    if ($view->base_table == $entity_info['base table']) {
      $options[$view->name . ':' . $display_id] = $view->name .' - ' . $view->display[$display_id]->display_title;
    }
  }

  return $options;
}

The key function here is the "views_get_applicable_views(&display_type)". The rest of the function just filters the list for an entity type (node, taxonomy term, comment, or user are the common defaults). Then formats the data as an options list.

You can embed this list in a form as so (I wrote this code to do a similar task to what it sounds like you are doing:

$views_options = references_get_views_options('node');
if ($views_options) {
  $views_options = array('' => '<' . t('none') . '>') + $views_options;
  $element['view_and_display'] = array(
    '#type' => 'select',
    '#title' => t('View used to select the nodes'),
    '#options' => $views_options,
    '#default_value' => $default_view,
    '#description' => 'description',
  );

In the submit handler you save the view option. Something like:

$view = $form_state['values']['views_and_display'];

Then in the view hook of the content type you run an embed.

$view_info = explode(':', $display['settings']['view_and_display']);
$result[0]['#markup'] = views_embed_view($view_info[0], $view_info[1], $args);

Hope that helps. I wrote a module that did something similar for a client not too long ago so this is all working code form that module. It actually registers a display handler to output a node reference field as a view so it would give me a list with exactly the correct fields desired.

  • Hey Daniel, thanks a lot for the response..I would dive deep into to figure out options. You surely did gave me a better and new direction to try more options. Thanks again! :) – drupal_dev Aug 28 '12 at 22:01

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.