1

I am using Field Collection Views module which allowed to create field widget as a view.

My requirement is to allow content author to choose widget for output this field (Field Collection). I added select field to article type to choose widget from two Field Collection Views views.

Field Collection is using views_embed_view for rendering this field, so I am trying to use hook_views_pre_view to switch to different view if author choose other than default view (display formatter)

/*
 * Implements hook_views_pre_view()
 */
function wzp_gallery_views_pre_view(&$view, &$display_id, &$args) {
    $node = menu_get_object();
    if (! $node->type == 'article') {
        return;
    }
    $field_name = FIELD_GALLERY_VIEW_FIELD_NAME;
    if (! $node->$field_name) {
        return;
    }
    $value = array_shift($node->$field_name);
    $view_info = explode('#', $value[0]['value']);
    if (! $view_info) {
        return;
    }
    $name = $view_info[0];
    $id = $view_info[1];
    if ($view->name == $name && $display_id == $id) {
        return;
    }
    $display_id = $id;
    if ($view->name == $name) {
        return;
    }
    drupal_set_message('<pre>' . print_r($name, 1) . '</pre>');
    drupal_set_message('<pre>' . print_r($id, 1) . '</pre>');
    $view = views_get_view($name);
    $view->set_display($id);
    $view->preview = TRUE;
}

but I am end up always with default view rendered.

Code form Field Collection Views module:

/**
 * Implements hook_field_formatter_view().
 */
function field_collection_views_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  switch ($display['type']) {
    case 'field_collection_views_view':
      //debug($items);
      $args = '';
      $i = 1;
      foreach ($items as $delta => $item) {
        if ($i == 1) {
          $args .= $item['value'];
        }
        else {
          $args .= '+' . $item['value'];
        }
        $i++;
      }
      $view_name = isset($settings['name']) ? $settings['name'] : 'field_collection_view';
      $display_id = isset($settings['display_id']) ? $settings['display_id'] : 'default';
      $content = views_embed_view($view_name, $display_id, $args);
      $element[0] = array(
        '#markup' => $content,
      );
      if (empty($items)) {
        field_collection_field_formatter_links($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
      }
      break;
  }
  return $element;
}

Could someone give me any advice?

2 Answers 2

1

Answer

You can't switch view during render process but current display id.

1

The answer is to use hook_views_pre_view, see the documentation here:

https://api.drupal.org/api/views/views.api.php/function/hook_views_pre_view/7

Hope this helps, I had a similar use case. Note: you should use

$view->set_display()

To actually change the view display.

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.