Print views result

$view=views_get_view($view_name);
if(is_object($view)) {
  $view->set_arguments(array(3));
  $view->preview(); // print HTML
  $results=$views->result; // object results
}
 
$results = views_get_view_result($view_name, $display_id, array(3)); // object results
 
// 多值参数
$view->set_arguments(array(implode(',', $ids)));

注意set_arguments是接受一个array,而array的元素是代表每个views中设置的contextual filters。如果contextual filter被设置为Allow multiple values,则允许多值参数,但输入的仍是string,不是array。多值可以用,或者+来分隔。

filters change

$view = views_get_view($view_name);
$view->display_handler->default_display->options['filters']['xxx']['value'] = 'xxx';
print $view->preview();

pager change

$view = views_get_view($view_name);
$view->display_handler->default_display->options['pager']['type'] = 'some';
$view->display_handler->default_display->options['pager']['options']['items_per_page'] = 30;
print $view->preview();

基于Term ID的node list

Contextual filters:
    Content: Has taxonomy term ID
        Provide default value
            Raw value from URL
                Path component: 2   
        When the filter value IS in the URL or a default is provided
            Override title: %1
 
Path:
    /xxx/%
 
Filter criteria:
    Content: Published (Yes)
    Content: Type (= Xxxx)

How to use fivestar as sort criteria?

Relationship: Vote results
Add sort criterion: Vote results: Value, Relationship: Voting results

change default option by sort exposed form

$view = views_get_view($view_name);
if(is_object($view)) {
    $view->set_display($display_id);
    $view->set_exposed_input(array('sort_by' => 'publish_date'));
    return $view->preview();
}

how to use hook change row?

/**
 * Implements hook_views_pre_render
 */
function hook_views_pre_render(&$view) {
  if($view->name == 'view_name' && $view->current_display == 'display_id') {
    foreach($view->result as $key => $row) {
      unset($view->result[$key]->field_name);
    }
  }
}

drupal7 views 过滤条件(filter criteria),跳转后没法显示第一个默认值(default value)

解决方法:
1、在views编辑里面找到filter criteria,点击add 找下  內容: Type
2、用Grouped filters 不要用Single filter,选择后再填完相关信息后方可以显示。
注意:下面一定要选择any, 不能选择其它,否则就不能全部显示。

hook去掉某个filter

function hook_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'view_name') {
    if(!empty($_GET['field_xxxxx'])) {
      if($_GET['field_xxxxx'] == 1) {
        if(!empty($view->display['default']->handler->options['filters']['field_xxxxx']))
          unset($view->display['default']->handler->options['filters']['field_xxxxx']);
      }
    }
  }
}

猫之良品
2.5k 声望139 粉丝

资深Drupal, magento与Joomla


引用和评论

0 条评论