Quantcast
Channel: Question and Answer » hook-views-query-alter
Viewing all 26 articles
Browse latest View live

how do I add a table and where clause to query object using hook_views_query_alter()?

$
0
0

I currently have a unique situation whereby I need a to use a different term reference field in a view’s contextual filters based on a node’s content type value. I don’t want to have to manually add a display for each possible combination of content types so I have been tying to use hook_views_query_alter() to accomplish what is now seeming impossible to me.

In my .module file I have:

/**
 * Implementation of hook_views_api().
 */
function mymodule_views_api() { // your module name into hook_views_api
  return array(
    'api' => 3,     
    'path' => drupal_get_path('module', 'mymodule').'/includes',
  );
}

Then in the file mymodule/includes/mymodule.views.inc:

/**
 * Implements hook_views_query_alter()
 */
function mymodule_views_query_alter(&$view, &$query) {
   //this is the name of our View and Display
   if ($view->name == 'shop' && $view->current_display == 'listings') {

       $query->add_table('field_data_field_my_category');
       //then add where clause afterwards etc etc
   }
}

This throws the error:

Fatal error: Call to undefined method SelectQuery::add_table()

Which leads me to believe that the Views API is not loading properly, unless I’m completely on the wrong track here.

I’ve been trying to find the best practices for altering views queries but have just ended up being generally confused and frustrated. Could anyone lead me towards the light please?

Thanks!


Add a JOIN with a subquery into a views query

$
0
0

Here’s my SQL, which I am trying to include in hook_views_query_alter() to modify the uc_catalog view (from Ubercart 3):

JOIN (
   SELECT uc_products.nid, MIN(uc_products.sell_price) AS min_price
   FROM uc_products
   JOIN node as n ON n.nid = uc_products.nid
   WHERE uc_products.nid IN
   (
     # in an ideal world, this is the WHERE statement from my views query
     SELECT ti.nid AS nid
     FROM taxonomy_index ti
     WHERE ti.tid = :db_condition_placeholder_1
     AND n.status = :db_condition_placeholder_2
     AND n.type IN :db_condition_placeholder_3
     GROUP BY uc_products.model
   )
 ) AS s2
 ON node.nid = s2.nid AND uc_products.sell_price = s2.min_price

I am confused as to how to make this work with the tools in views_plugin_query_default since this SQL has a subquery, etc. that is more complex than a simple JOIN statement. I am having a hard time conceptualizing using add_relationship or add_table to make this work. Is there something about $join building I don’t understand.

how to use hook_views_query_alter() to modify where condition?

$
0
0

I’m trying to modify the where condition of a views query. till now I was successful with altering “order by”, but I have no idea how to alter the where condition. I want to check the search_term and if it was in uppercase, transform it to lowercase so the query can find it. also there are some special characters in my language (persian) that I need to replace them before the query runs. anyone can help me where to start or what hooks or views_handlers to use?

<?php
/**
 * Implementation of hook_views_query_alter
 * @param type $view
 * @param type $query
 */
function nashreneydev_views_query_alter(&$view, &$query) {
  //krumo($query);
  //krumo($view);
  if ($view->name == 'custom_search') {
    $search_term = $view->exposed_raw_input['combine'];

    **//$query->where[0]['conditions'][0]['field']= "?????";**
    $view->query->orderby[1]['field'] = "CASE node_type WHEN 'product_display' THEN 1 ELSE 2 END";
    $view->query->orderby[1]['direction'] = "ASC";
    $view->query->orderby[0]['field'] = "CASE node_title WHEN '".$search_term."' THEN 1 ELSE 2 END";
    $view->query->orderby[0]['direction'] = "ASC";
    //krumo($view->query->orderby);
  }
}
?>

the devel result for where condition is as follow right now. :views_combine is equal to %s%.

CONCAT_WS(‘ ‘, node.title, ‘ ‘, field_data_body.body_value, ‘ ‘,
field_data_field_author.field_author_target_id, ‘ ‘,
field_data_field_translator.field_translator_target_id, ‘ ‘,
field_data_field_book_tags.field_book_tags_tid) LIKE :views_combine

How to alter view query to display only 5 top records

$
0
0

I couldn’t find a similar problem on the forum so I am asking. I created a view with exposed filters but when I visit the view page I am going display 5 top recent record for specific content type. When I click search button the result changes of course and result with a pager shows up. How can I display 5 top recent records before I click search button for the first time when I visit page with the view? I thought to use hook_views_query_alter to add limit to the query but I don’t know how. Any ideas how to achieve that?

Thank you!

modify query sent by views in chart API module

$
0
0

I am using Google Charts API module to generate a pie chart of a content type with the fields Blood-group and count representing the sectors . For this I need to completely alter the query ( select from a different table ) that the views integration provides. Can i use hook_views_query_alter?

hook_views_query_alter() order by taxonomy term name

$
0
0

I am using Drupal 7 with dataTables and everything works great except that the sort by taxonomy term is not working properly. The data is sorted by tid instead of term name so I was wondering if there is a way to achieve this programatically by using hook_views_query_alter and alter the $query.

function datatables_views_query_alter(&$view, &$query) {
    $field_to_avoid = array('edit_node', 'delete_node');
    if (($view->style_plugin->definition['name'] == 'datatables') && (in_array('sSearch', array_keys($_GET)))) {
        $string = $_GET['sSearch'];
        $group = count($query->where) + 1;

        if (!empty($string)) {
            foreach ($view->field as $field_name => $field) {
                if (!in_array($field_name, $field_to_avoid)) {
                    if (($field_name != 'title')) {
                        $join = new views_join();
                        $join->table = $field->table;
                        $join->field = 'entity_id';
                        $join->left_table = 'node';
                        $join->left_field = 'nid';
                        $join->type = 'LEFT';
                        $query->add_relationship($field->table, $join, 'node', null);
                        $query->add_where($group, $field->table . '.' . $field->real_field, '%' . db_like($string) . '%', 'LIKE');
                    }else {
                        $query->add_where($group, $field->table . '.' . $field->real_field, '%' . db_like($string) . '%', 'LIKE');
                    }
                }
            }
        }

        $query->where[$group]['type'] = "OR";
        $query->distinct = TRUE;


    }

    $order_by = (array_key_exists('order', $_GET)) ? $_GET['order'] : '';
    $order = (array_key_exists('sort', $_GET)) ? $_GET['sort'] : '';

    foreach ($view->field as $field_name => $field) {
        if (!in_array($field_name, $field_to_avoid)) {
            if ($field_name == $order_by) {
                $query->add_orderby($field->table, $field->real_field, strtoupper($order));
            }
        }
    }
}

How JOIN a subselect to main select 'node' fields

$
0
0

For posting purposes, I have removed all sorting, entity relationships, extra fields etc etc from Main View.

Main view returns 1200 rows
Subselect returns 176 rows
After aggregation the main view should display only 176 rows

I am trying to achieve this SQL using views API


SELECT 
    node.nid,
    c.field_parent_component_tid as comp_id, 
    env.field_mig_details_env_tid as env_id,     
    migdate.field_mig_details_mig_date_value AS d

FROM node node

LEFT JOIN field_data_field_parent_component c 
    ON node.nid = c.entity_id     
LEFT JOIN field_data_field_mig_details_env env 
    ON node.nid = env.entity_id 
LEFT JOIN field_data_field_mig_details_mig_date migdate 
    ON node.nid = migdate.entity_id 

JOIN (
      SELECT 
          c_sub.field_parent_component_tid as comp_id, 
          env_sub.field_mig_details_env_tid as env_id,         
          MAX(date_sub.field_mig_details_mig_date_value) AS maxdate

      FROM node subnode

      LEFT JOIN field_data_field_parent_component c_sub 
          ON subnode.nid = c_sub.entity_id           
      LEFT JOIN field_data_field_mig_details_env env_sub
          ON subnode.nid = env_sub.entity_id           
      LEFT JOIN field_data_field_mig_details_mig_date date_sub
          ON subnode.nid = date_sub.entity_id 
      WHERE 
        (subnode.status = '1') 
        AND (subnode.type IN  ('migration_details')) 
        AND (c_sub.field_parent_component_tid IS NOT NULL)

      GROUP BY 
        comp_id,
        env_id
) query2 ON c.field_parent_component_tid = comp_id 
        AND env.field_mig_details_env_tid = env_id
        AND migdate.field_mig_details_mig_date_value = maxdate

So, In hook_views_query_alter(&$view, &$query) I do this code to generate the subselect with all the aggregation


        $subquery = db_select('node', 'subnode');
    //Set up all the joins to get access to the fields    
    $subquery->leftjoin('field_data_field_parent_component', 'c', 'subnode.nid = c.entity_id');
    $subquery->leftjoin('field_data_field_mig_details_mig_date', 'migTbl', 'subnode.nid = migTbl.entity_id');    
    $subquery->leftjoin('field_data_field_mig_details_env', 'e', 'subnode.nid = e.entity_id');
    //now select the fields that will show up on the SELECT statement

    $subquery->fields('c', array('field_parent_component_tid',));
    $subquery->fields('e', array('field_mig_details_env_tid',));

    $subquery->addExpression('MAX(migTbl.field_mig_details_mig_date_value)', 'maxdate');
    //This will generate the WHERE clauses
    $subquery->condition('subnode.status', 1);    
    $subquery->condition('subnode.type', 'migration_details');
    //Lastly the grouping
    $subquery->groupBy('c.field_parent_component_tid, e.field_mig_details_env_tid');
    //Run the query, store in results so I can add it to main query
    $result = $subquery->execute();

Then I add the subselect to the main view with the following code


  $compAlias = $query->tables['node']['field_data_field_parent_component']['alias'];

  $join = new views_join('$subquery', 'node', 'field_parent_component_tid', $compAlias.'.field_parent_component_tid');
  //$join->definition = array('table' => $subquery, 'left_field' => 'field_parent_component_tid', 'field' => 'field_parent_component_tid', 'left_table' => 'node');
  $join->extra_type = 'AND';
  $join->table = $subquery;
  $join->left_field = $compAlias.'.field_parent_component_tid';
  $join->field = 'field_parent_component_tid';
  $join->type = 'INNER';
  //$join->adjusted = true;
  //$join->extra = $WHAT DO I USE HERE??? = field_mig_details_env_tid';
  // AND $WHAT DO I USE HERE??? = maxdate';

  //dpm($join);
  //Add the subquery as another "table" to the main query
  $query->add_relationship('query2', $join, $subquery);

So far so good, the SQL generated in my view looks good, but I need to add an $join->extra whit the equivilant of


AND env.field_mig_details_env_tid = env_id
AND migdate.field_mig_details_mig_date_value = maxdate

If I look at the generated SQL so far it looks like this:


SELECT 
  node.nid AS nid, 
  node_field_data_field_mig_parent_x_ref.nid AS node_field_data_field_mig_parent_x_ref_nid,
  'node' AS field_data_field_parent_component_node_entity_type, 
  'node' AS field_data_field_mig_details_env_node_entity_type, 
  'node' AS field_data_field_mig_details_mig_date_node_entity_type, 
  'node' AS field_data_title_field_node_entity_type
FROM 
 {node} node
LEFT JOIN {field_data_field_mig_parent_x_ref} field_data_field_mig_parent_x_ref 
  ON node.nid = field_data_field_mig_parent_x_ref.entity_id 
  AND (field_data_field_mig_parent_x_ref.entity_type = 'node' 
  AND field_data_field_mig_parent_x_ref.deleted = '0')
LEFT JOIN {node} node_field_data_field_mig_parent_x_ref 
  ON field_data_field_mig_parent_x_ref.field_mig_parent_x_ref_target_id = node_field_data_field_mig_parent_x_ref.nid
INNER JOIN {field_data_field_parent_component} field_data_field_parent_component 
  ON node.nid = field_data_field_parent_component.entity_id 
  AND (field_data_field_parent_component.entity_type = 'node' 
  AND field_data_field_parent_component.deleted = '0')
INNER JOIN (
  SELECT c.field_parent_component_tid AS field_parent_component_tid,    e.field_mig_details_env_tid AS field_mig_details_env_tid,   MAX(migTbl.field_mig_details_mig_date_value) AS maxdate
  FROM 
  {node} subnode
  LEFT OUTER JOIN {field_data_field_parent_component} c ON subnode.nid = c.entity_id
  LEFT OUTER JOIN {field_data_field_mig_details_mig_date} migTbl ON subnode.nid =   migTbl.entity_id
  LEFT OUTER JOIN {field_data_field_mig_details_env} e ON subnode.nid = e.entity_id
  WHERE  (subnode.status = '1') AND (subnode.type = 'migration_details') 
  GROUP BY c.field_parent_component_tid, e.field_mig_details_env_tid) 
query2 ON   
         field_data_field_parent_component.field_parent_component_tid =   query2.field_parent_component_tid
  WHERE (( (node.status = '1') AND (node.type IN  ('migration_details')) AND (field_data_field_parent_component.field_parent_component_tid = '141') ))

If I look at the $query->tables, I don’t see that date table, I don’t see the environement table/ How would I add these??

Append more nodes to views result

$
0
0

What’s the best Views Hook and the best method to append more nodes to an already queried views result?

I wanted to build a node type (Type A) with node references to another type (Type B), and also a views view which will list node type B through a views relationship provided it gets node type A’s nid through a contextual filter. The node type A and B however also share a taxonomy term reference field for the same vocabulary.

So I wanted the person creating node type A to be able to reference a few nodes of type B, and they will get displayed at the top of the views list, however I wanted to also append the rest of the node type B with the same taxonomy terms as node type A to the views list. It is quite important that the result from querying nodes with same taxonomy reference gets appended to the result of the referenced nodes for display purposes. Because I’m using an Isotope/Packery display, they need to be in the same container with same structure.

I was thinking of also using hook_views_query_alter but it seems like it cannot be achieved through a single query, because the referenced nodes need to be ordered and appear at the top. I was thinking of using hook_views_post_execute and just simply run a db_query of my own, exclude the nodes already in the views result to grab the rest of the nodes of type B and append it to the result.

Is this possible? What are the pitfalls of such a method? I.E. paging, performance etc. Is it do-able?

Is there a better method? Is it do-able from the UI? Any pointers greatly appreciated.


How to use custom tables in Views

$
0
0

I have multisite DRUPAL6.26 website using VIEWS 3.0 module in it.

Both sites are install in single DB.
The first/parent site has no prefix for its tables.EX: node,block,role etc..
The second/childsite has prefix of child. EX: child_node,child_block,child_role etc..

I have a VIEW showing all pictures uploaded by user in there PARENT site account.
And the same VIEW for CHILD site showing pictures that they upload on CHILD site.

What I want is to show all pictures in PARENT site account of user whether they upload on PARENT site OR CHILD site.

[users are shared between the 2 sites.]

Any help of ‘views master’ required :)

Views hook query alter, change the DatabaseCondition Object

$
0
0

I am trying to alter views filters with hook_views_query_alter like

function custom_views_query_alter(&$view, &$query) {      
     foreach($query->where[1]['conditions'] as $idx => $condition) {
      dpr($condition); 
     } 
}

And below is the output I get from DPR function

Array
(
    [field] => node.status
    [value] => 1
    [operator] => =
)
Array
(
    [field] => node.type
    [value] => Array
        (
            [0] => pricing
        )

    [operator] => in
)
Array
(
    [field] => DatabaseCondition Object
        (
            [conditions:protected] => Array
                (
                    [#conjunction] => AND
                    [0] => Array
                        (
                            [field] => field_data_field_zip_range.field_zip_range_from
                            [value] => 65000
                            [operator] => <=
                        )

                    [1] => Array
                        (
                            [field] => field_data_field_zip_range.field_zip_range_to
                            [value] => 65000
                            [operator] => >=
                        )

                )

            [arguments:protected] => Array
                (
                )

            [changed:protected] => 1
            [queryPlaceholderIdentifier:protected] => 
        )

    [value] => 
    [operator] => 
)

So the first 2 filters are not a problem, you just edit the field and you set new values. Problem is the DatabaseCondition Object with this conditions:protected part. I just don’t have a clue how to change values there.

There was similar questions here views hook query alter but it was not solved. It only solves problems for first 2 filters and doesn’t do anything for DatabaseCondition Object.

Add group by to views join via hook_view_query_alter

$
0
0

I am trying to add a GROUP BY to a JOIN via hook_views_query_alter and I can’t seem to get it right.

This is the psuedo-ish query that I’m going for (:

SELECT * FROM node
LEFT JOIN (
  SELECT * from repeating_date
  ON node.nid = repeating_date.entity_id
    AND repeating_date.date_value >= '2014-05-13'
  GROUP BY repeating_date.entity_id
)

I’ve tried this (which I found on d.o, I believe):

$query->add_field('repeating_date', 'date_value', array('function' =>  'groupby'));
$query->add_groupby('repeating_date.date_value');

I’ve also tried to simply do:

$query->table_queue['repeating_date']->group_by = array('date_value')

All to no avail. The ultimate goal is to only return one result from repeating_date, which has a one-to-many relationship with node.

Thanks for any help!

View query alter joins view_joins construct() with condition

$
0
0

We are trying to alter view query, by joining table with extra condition related to other table in the query to avoid duplicate,

$join2 = new views_join;
$join2->construct('mls_state', 'data_field_mls', 'mls_value', 'mls_sid');
$join2->extra = array(array('table' => 'mls_state', 'field' => 'state_code', 'operator' => '=', 'value' => 'data_field_state.state_value'));
$view->query->add_relationship('mls_state',$join2,'data_field_mls');

But in view it default takes the value in quotes
Result

LEFT JOIN {mls_state} mls_state ON data_field_mls.mls_value = mls_state.mls_sid AND mls_state.state_code = 'data_field_state.state_value'

How would we check condition with other referenced table fields value?

How to modify drupal views query

$
0
0

So Drupal views is creating a query and I need to change the where clause conditions and move it to the on clause. This seems to give me the results that I am looking for. I can not figure out how to do this with views in its self so I have to alter the views query in order to do this.

This is the query that drupal is creating.

SELECT field_data_field_procedure_date_time.delta AS     field_data_field_procedure_date_time_delta, field_data_field_procedure_date_time.entity_id     AS date_id_field_procedure_date_time, field_data_field_procedure_date_time.delta AS     date_delta_field_procedure_date_time, node.title AS node_title, node.nid AS nid, field_data_field_procedure_date_time.language AS field_data_field_procedure_date_time_language, field_data_field_procedure_date_time.bundle AS field_data_field_procedure_date_time_bundle, field_data_field_procedure_date_time.field_procedure_date_time_value AS field_data_field_procedure_date_time_field_procedure_date_ti, field_data_field_procedure_date_time.field_procedure_date_time_rrule AS field_data_field_procedure_date_time_field_procedure_date_ti_1, 'node' AS field_data_field_procedure_date_time_node_entity_type
FROM 
{node} node
LEFT JOIN {field_data_field_procedures} field_data_field_procedures ON node.nid =     field_data_field_procedures.field_procedures_target_id AND     (field_data_field_procedures.entity_type = 'user' AND field_data_field_procedures.deleted = '0')
LEFT JOIN {users} field_procedures_node ON field_data_field_procedures.entity_id = field_procedures_node.uid
LEFT JOIN {field_data_field_pre_op_procedure} field_data_field_pre_op_procedure ON node.nid = field_data_field_pre_op_procedure.field_pre_op_procedure_target_id AND (field_data_field_pre_op_procedure.entity_type = 'user' AND field_data_field_pre_op_procedure.deleted = '0')
LEFT JOIN {users} field_pre_op_procedure_node ON field_data_field_pre_op_procedure.entity_id = field_pre_op_procedure_node.uid
LEFT JOIN {field_data_field_procedure_date_time} field_data_field_procedure_date_time ON node.nid = field_data_field_procedure_date_time.entity_id AND (field_data_field_procedure_date_time.entity_type = 'node' AND field_data_field_procedure_date_time.deleted = '0')
WHERE (( (DATE_FORMAT(CONVERT_TZ(field_data_field_procedure_date_time.field_procedure_date_time_value, 'UTC', 'America/Boise'), '%Y-%m') >= '2014-10' AND DATE_FORMAT(CONVERT_TZ(field_data_field_procedure_date_time.field_procedure_date_time_value, 'UTC', 'America/Boise'), '%Y-%m') <= '2014-10') )AND( (field_procedures_node.uid = '3' ) AND (field_pre_op_procedure_node.uid = '3' ) )AND(( (node.status = '1') AND (node.type IN  ('procedures')) )))

and I need to get it formated to look like this.

SELECT DISTINCT 
field_data_field_procedure_date_time.delta AS 
field_data_field_procedure_date_time_delta, field_data_field_procedure_date_time.entity_id AS 
date_id_field_procedure_date_time, field_data_field_procedure_date_time.delta AS 
date_delta_field_procedure_date_time, node.title AS 
node_title, node.nid AS 
nid, field_data_field_procedure_date_time.language AS 
field_data_field_procedure_date_time_language,   field_data_field_procedure_date_time.bundle AS 
field_data_field_procedure_date_time_bundle,    field_data_field_procedure_date_time.field_procedure_date_time_value AS 
field_data_field_procedure_date_time_field_procedure_date_ti,   field_data_field_procedure_date_time.field_procedure_date_time_rrule AS 
field_data_field_procedure_date_time_field_procedure_date_ti_1, 'node' AS 
field_data_field_procedure_date_time_node_entity_type
FROM 
node node left JOIN 
field_data_field_procedures field_data_field_procedures ON node.nid =   field_data_field_procedures.field_procedures_target_id AND   (field_data_field_procedures.entity_type = 'user' AND field_data_field_procedures.deleted  = '0')  LEFT JOIN 
users field_procedures_node ON field_data_field_procedures.entity_id =   field_procedures_node.uid AND ( field_procedures_node.uid = '53' ) LEFT JOIN 

field_data_field_pre_op_procedure field_data_field_pre_op_procedure ON node.nid =   field_data_field_pre_op_procedure.field_pre_op_procedure_target_id AND  (field_data_field_pre_op_procedure.entity_type = 'user' AND   field_data_field_pre_op_procedure.deleted = '0') LEFT JOIN 
users field_pre_op_procedure_node ON field_data_field_pre_op_procedure.entity_id =  field_pre_op_procedure_node.uid AND (field_pre_op_procedure_node.uid = '53' )  LEFT JOIN 


field_data_field_procedure_date_time field_data_field_procedure_date_time ON node.nid = field_data_field_procedure_date_time.entity_id AND  (field_data_field_procedure_date_time.entity_type = 'node' AND  field_data_field_procedure_date_time.deleted = '0')
WHERE 


(( (node.status = '1') AND (node.type IN  ('pre_op', 'procedures')) ))

Its basically the same query except I moved the where conditions of the uid=53 into the ON clause instead of the where clause. Anyone know how I can do this with either changing some settings in the views or by manually modifiying the query with a hook_views_query_alter or a hook_views_pre_exectute?

Views: how to make an OR condition between filters and contextual filters

$
0
0

I have been able to separate Views filters into groups and apply an OR condition, following this article: http://www.webomelette.com/drupal-views-filters-and-or-logic

I also understand how to use the hook_views_query_alter() to make a few contextual filters act with an OR statement between each contextual filter.

What I can’t figure out is this: how do I make the conditional between the REGULAR filters and the CONTEXTUAL filters? After altering the query, this is what I’ve able to create:

WHERE 
(
  ( (og_membership_node.gid = '8' ) 
    AND (node.status = '1') 
    AND (node.type IN  ('event')) 
    AND (field_data_field_event_type.field_event_type_tid = '12') 
  )
  AND(
  ( (node.status = '1') 
     AND (node.type IN  ('event')) 
     AND (field_data_field_event_type2.field_event_type_tid IN  ('11', '10')) )
  )
)

and I want it to be:

WHERE 
(
  ( (og_membership_node.gid = '8' ) 
    AND (node.status = '1') 
    AND (node.type IN  ('event')) 
    AND (field_data_field_event_type.field_event_type_tid = '12') 
  )
  OR(
  ( (node.status = '1') 
     AND (node.type IN  ('event')) 
     AND (field_data_field_event_type2.field_event_type_tid IN  ('11', '10')) )
  )
)

To give some background of what I am trying to do: I am using Organic groups, and I want my view to show events, Global, Regional and Chapter events. However, if the event is of type Chapter, I want to only show Chapter events which are in the same group as the page content.

I had manually edited the query to move the regular filters associated with the Chapter filter into the contextual filter group, with this:

foreach($query->where[1]['conditions'] as $condition) {
  $query->where[0]['conditions'][] = $condition;
}
unset($query->where[1]);

Change view query

$
0
0

I’ve a view and I would like to change it’s query (slightly). Below is an example of what I have.

SELECT * FROM {node} node ORDER BY my_field ASC

I want to inject ‘COLLATE utf8_danish_ci‘ into query. My expectations below

SELECT * FROM {node} node ORDER BY my_field COLLATE utf8_danish_ci ASC

How would I do that in most correct way?

I’m also wondering if there is a way to apply such ‘injection’ only for specific view(s).

enter image description here


How to alter drupal views filter by start date query

$
0
0

I need to display only future events with start and end date greater than or equal to today’s date. I am using drupal with oracle. When i am trying in views UI, its giving me an SQL error.

How to hook_views_query_alter arbitrary field and use it in view GUI

$
0
0

For the life of me I can’t figure this out.

I have vast and complicated drupal 7 site and among other things I’m using profile2 module. Not sure why, but a few years back I’ve added field directly to ‘profile’ table. It is used to store some number and adding it looked like quick fix back then.

Now I need to get some info using views and all is fine except this one field which I absolutely have to take into account, but I am not able to.

What I did so far is:

function modulename_views_api(){    
    return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'modulename'),
  ); 
}

in my custom module,

function modulename_views_query_alter(&$view, &$query){
    if ($view->name == 'viewname') {
        $query->add_field(NULL, 'some_field', array());
    }
}

in modulename.views.inc

What I’m trying to do is either add this field to display directly, or add it to the list of fields, or replacement patterns, or anything I could use.

modulename_views_data() isn’t useful for me, because I would have to recreate whole table structure created (and frequently changed) in GUI.

I tried _views_pre_view, _views_pre_render, relationships, query rewrites, all to no avail. I’ve run out of ideas…

Clean url for custom query parameters

$
0
0

I have a custom filter form in my site and the form submitted by the filters adds 2 Get variables to the url. However, the variables appear in the form of:

http://example.com/newpage?param1=a%2Cb&param2=c

instead of:

http://example.com/newpage/param1/a%2Cb/param2/c

I have Global redirect clean url enabled and I have pathauto module in my installation and clean url test to checl mysite.com/user/login works fine.

I have tried a couple of plugins eg. query_parameters_to_url but they are not working for that page and I tried adding hook_url_outbound_alter and hook_url_inbound_alter in my custom module but they overwrite all the urls.

Can anyone suggest a way to implement clean urls for somehting like this with affecting the urls for other pages in the website?

Is there a way to search for instances of hook_views_query_alter() in my database?

$
0
0

I am running a D7 site that someone else built and I’m having trouble seeing how an SQL command in a particular view has been altered. In particular, the attributes in the SELECT statement appear to have been altered.

Is there a way to search my database to see if the person building the site added an errant hook_views_query_alter command that is the reason the SQL filter looks different than one would expect just looking at the fields in the View?

Or is there another, better way to debug this issue? Thanks for any help!

Ajax pager failure in view after modifying view on hook_views_query_alter

$
0
0

The requirement is for different pages on the website to show different Twitter feeds according to the current page. There is a context (triggered according to the path) that inserts a view block into the footer. A custom module kicks in when the query for the view block is about to be executed. The module does some simple processing to determine what Twitter feed(s) are required to be displayed on the current page.

So, the custom module contains:

/**
 * Implementation of hook_views_query_alter
 */
function twitter_feed_selector_views_query_alter(&$view, &$query) {

  if ($view->name == 'tweets') {

    // Some processing...

    $feed = _twitter_feed_lookup($matching_path);
    $view->query->where[1]['conditions'][0]['value'] = $feed;

  }
}

However, the view block has a lite pager and Ajax enabled. And when the user clicks on the pager links to see the next/previous page of tweets, no tweets are coming back from the view.

I can’t find how to get around this. If I turn off Ajax on the view, all is well, but the whole page is reloaded each time the pager is clicked — which is way too clunky.

Can anyone suggest a means to make my amendments to the view query work nicely also with the Ajaxed results from the pager?

Viewing all 26 articles
Browse latest View live