Sunday, March 4, 2012

Drupal How to create Views 2 filters for your new module




I will show you tips on how to write a see filter that joins several tables together to filter the results. Recently i wrote the Taxonomy Access Control filter for beats by dr dre studio each of really permissions (view/update/delete/list/create). We will use that as an example, however in our case we're going only make use of the "view" authorization.





Note: if the program code is incomplete or hard to read, it is possible to grab the whole TAC filter set up from here.





1st, decide on the particular query that you should operate, write it and test it (in phpMyAdmin designed for example).





Here is the query that I need to use for filter nodes that have explicit TAC see authorization:





SELECT tn. nid THROUGH term_node tn LEFT JOIN term_access ta ON tn. tid = ta. tid WHERE ta. rid IN (SELECT ur. free FROM users_roles ur WHERE ur. uid = %d) AND konstruera. grant_view = one



So we need our filter to do these:



JOIN `term_node` Monster Beats Studio to the bottom `node` table on `vid`.



JOIN `term_access` to `term_node` table on `tid`.



Include the above WHERE declaration to the main views question.





Keep that in mind, and let's do the preliminary do the job.





1st, we modify taxonomy_access. component, and add these functionality:





functionality taxonomy_access_views_api() return array( 'api' => 2, 'path' => drupal_get_path('module', 'taxonomy_access') . '/includes', );





That will let the component know that we're using views 2 and also to look for all of the necessary files in the "includes" folder.





Make the "includes" folder.





Include taxonomy_access. views. inc in order to "includes" folder using the following contents:





functionality taxonomy_access_views_data() $data = array(); if(module_exists('taxonomy_access')) $data gong0deng gong1deng = array( 'title' => t('TAC: beats by dr dre pro grant view'), 'help' => t('Current user has Taxonomy Access Control view permission.'), 'filter' => array( 'handler' => 'taxonomy_access_handler_filter_view', ), ); return $data;





functionality taxonomy_access_views_handlers() return array( 'info' => array( 'path' => drupal_get_path('module', 'taxonomy_access') . '/includes', ), 'handlers' => array( 'taxonomy_access_handler_filter_view' => array( 'parent' => 'views_handler_filter', ), ), );





The very Monster iBeats first part tells views about our filter. Provides some useful explanation, title, and specifies that handler to use and which group to tag it along in order to ("Node" in our case). The 2nd part is the handler, that basically tells views to look for "taxonomy_access_handler_filter_view. inc" in the "includes" listing.





Right now let's create that document:





course taxonomy_access_handler_filter_view extends views_handler_filter function admin_summary() function operator_form() function query() $table = $this->ensure_my_table(); $join = new views_join(); $join->construct('term_node', $this->table_alias, 'vid', 'vid'); $this->query->ensure_table('term_node', $this->relationship, $join); $join = new views_join(); $join->construct('term_access', 'term_node', 'tid', 'tid'); $this->query->ensure_table('term_access', $this->relationship, $join); $this->query->add_where($this->options gong2deng , "term_access.grant_view = 1 AND term_access.rid IN (SELECT rid FROM users_roles WHERE uid = ***CURRENT_USER***)");





This is how the magic happens. This does percisely what we should attempt to do:



JOIN `term_node` to the bottom `node` table on `vid`.



JOIN `term_access` to `term_node` table on `tid`.



Include the above WHERE declaration to the main views question.





Initially you will also want to clear the menus cache for the changes to take impact.





Note: if the program code is incomplete or hard to read, it is possible to grab the whole TAC filter set up from here.


No comments:

Post a Comment