【问题标题】:Using custom, dynamic query with drupal views使用带有 drupal 视图的自定义动态查询
【发布时间】:2010-07-06 14:54:29
【问题描述】:

动态是指我希望能够根据用户输入更改 sql 查询。

如果这是我的自定义查询,我该如何更改它以便在用户单击列时在 ORDER BY .. Descending 和 Ascending 之间切换?当您覆盖 Views 生成的查询时,这甚至可能吗?

<?php
function views_views_pre_execute(&$view) {
   if($view->name=="hud_downloads") {
     $view->build_info['query']="SELECT node.nid AS nid, 
         node.title AS node_title, 
         SUM(pubdlcnt.count) AS pubdlcnt_count 
         FROM node node 
         LEFT JOIN pubdlcnt pubdlcnt ON node.nid = pubdlcnt.nid  
         WHERE (node.type in ('huds')) AND (node.status <> 0) 
         GROUP BY node.nid ORDER BY pubdlcnt_count DESC";
     }
}
?>

【问题讨论】:

    标签: drupal drupal-views sql


    【解决方案1】:

    小心,这里有龙。 这就是我如何做到这一点的。首先,您创建显示感兴趣内容的视图。在您感兴趣的所有字段(计数、价格、标题等)上添加排序不要担心它们不能很好地协同工作,您将在代码中动态删除/更改它们。

    我创建了一个模块来包含一个处理排序的函数。创建类型为“Global:Null”的参数。它可以出现在您列表中的任何位置,但该 url 位置中必须存在一个参数,否则代码将不会执行。我通常添加标题或搜索类型。设置参数以显示所有值(如果不存在)并添加验证器。选择一个 PHP 验证器并添加以下内容。

    return _mymodule_handle_sortables($view);
    

    这将允许您快速/轻松地编辑函数的内容,而无需在每次迭代时编辑视图/保存视图/预览视图。请注意,我传递了 $_GET 变量。这不是绝对必要的,因为无论如何它都应该在函数中可用。我这样做只是为了更容易阅读。

    第一步,使用开发模块获取可排序字段的名称

    function _mymodule_handle_sortables(&$view) {
        dpm($view->sort);
    }
    

    预览视图并记下字段的名称。获得它们后,您可以执行以下操作来更改视图的输出。

    function _mymodule_handle_sortables(&$view) {
        switch ($_GET['sort']) {
            case 'sell_price_asc':
                unset($view->sort['title']);
                $view->sort['sell_price']->options['order'] = 'ASC';
                break;
            case 'sell_price_desc':
                unset($view->sort['title']);
                $view->sort['sell_price']->options['order'] = 'DESC';
                break;
            case 'alpha_asc':
                unset($view->sort['sell_price']);
                $view->sort['title']->options['order'] = 'ASC';
                break;
            case 'alpha_desc':
                unset($view->sort['sell_price']);
                $view->sort['title']->options['order'] = 'DESC';
                break;
        }
        return true;
    }
    

    将 PHP 标头添加到您的视图中并添加以下内容

    <?php echo _mymodule_sortables($_GET); ?>
    

    现在您可以动态显示排序标题。这是一个公认的过度功能。

    function _emunications_sortables($g) {
        // Collect all the relevant GET parameters
        $gopts = array();
        foreach ($g as $k=>$v) {
            if ($k == 'q') continue;
            $gopts[$k] = $v;
        }
    
        $opts = http_build_query($gopts);
    
        // Preserve the sort choice for selection
        $s1 = $s2 = $s3 = $s4 = '';
    
        switch ($gopts['sort']) {
          case 'alpha_asc'    : $s1 = 'selected="selected"';break;
          case 'alpha_desc'   : $s2 = 'selected="selected"';break;
          case 'sell_price_asc' : $s3 = 'selected="selected"';break;
          case 'sell_price_desc'  : $s4 = 'selected="selected"';break;
        }
    
        // Unset the sort option so that it can be set in the url manually below
        unset($gopts['sort']);
        $opts_sort = http_build_query($gopts);
    
        $output = "
        <div class='product_index_header'>
          <div class='view-selection'>
            <span class='descript'>View: </span>
            <a class='list' href='/products?$opts'>&nbsp;</a>
            <span class='bar'>|</span>
            <a class='grid' href='/products/grid/list?$opts'>&nbsp;</a>
          </div>
          <div class='sortable'>
            <select name='droppy' class='droppy kitteh' onchange=\"window.location.href=$('select.droppy').val()\">
              <option value='#'>Sort</option>
              <option $s1 value='?sort=alpha_asc&amp;$opts_sort'>a-z</option>
              <option $s2 value='?sort=alpha_desc&amp;$opts_sort'>z-a</option>
              <option $s3 value='?sort=sell_price_asc&amp;$opts_sort'>$ - $$</option>
              <option $s4 value='?sort=sell_price_desc&amp;$opts_sort'>$$ - $</option>
            </select>
          </div>
        </div>
        ";
    
        return $output;
    }
    

    【讨论】:

    • 哇,不开玩笑!太棒了,这应该可以帮助我定制很多东西(不仅仅是排序)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多