Call a view with contextual filter programamtically on the basis of a form field value with ajax api,Shows how to import views programmatically

Public

1.Shows how to import views programmatically.
2. Shows how to call views programmatically with contextual filters inside a form using ajax property of form api
Directions to follow the result
1. Create a custom module and place the given code in .module file
2. Install the module(Before installing your custom module install views module).
3. Once you install this module you will see a new view is created with name as Test View.
4. Create nodes for article content type as admin and as other user with article add permission
5. Now visit the link as yoursitename/articles-listing
6. The above page will show all article listing for first and now user can opt My articles to see the articles,the current user is author of, so if you have used diffrent yours to create different article nodes then you can see the diff in nodes listing
7. Use textbox to filter articles by inputting article title.

</> CopyGet raw version
php
  1. <?php
  2. /**
  3.  * file to show how to import views programmatically,call views with contextual filters programamtically with ajax and form api
  4.  *
  5.  */
  6.  
  7.  
  8. /**
  9.  * Implements hook_views_api().
  10.  */
  11. function test_views_api() {
  12. list($module, $api) = func_get_args();
  13. if ($module == "views" && $api == "views_default") {
  14. return array("version" => "3.0");
  15. }
  16. }
  17.  
  18. /**
  19.  * Implementation of hook_views_default_views().
  20.  */
  21. function test_views_default_views() {
  22.  
  23. $export = array();
  24. // Replace the code down here till you see label flag down here with you view export
  25. $view = new view();
  26. $view->name = 'test_view';
  27. $view->description = '';
  28. $view->tag = 'default';
  29. $view->base_table = 'node';
  30. $view->human_name = 'Test View';
  31. $view->core = 7;
  32. $view->api_version = '3.0';
  33. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  34.  
  35. /* Display: Master */
  36. $handler = $view->new_display('default', 'Master', 'default');
  37. $handler->display->display_options['title'] = 'Test View';
  38. $handler->display->display_options['use_more_always'] = FALSE;
  39. $handler->display->display_options['access']['type'] = 'perm';
  40. $handler->display->display_options['cache']['type'] = 'none';
  41. $handler->display->display_options['query']['type'] = 'views_query';
  42. $handler->display->display_options['exposed_form']['type'] = 'basic';
  43. $handler->display->display_options['pager']['type'] = 'full';
  44. $handler->display->display_options['pager']['options']['items_per_page'] = '10';
  45. $handler->display->display_options['style_plugin'] = 'default';
  46. $handler->display->display_options['row_plugin'] = 'fields';
  47. /* Field: Content: Title */
  48. $handler->display->display_options['fields']['title']['id'] = 'title';
  49. $handler->display->display_options['fields']['title']['table'] = 'node';
  50. $handler->display->display_options['fields']['title']['field'] = 'title';
  51. $handler->display->display_options['fields']['title']['label'] = '';
  52. $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
  53. $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
  54. /* Field: Content: Body */
  55. $handler->display->display_options['fields']['body']['id'] = 'body';
  56. $handler->display->display_options['fields']['body']['table'] = 'field_data_body';
  57. $handler->display->display_options['fields']['body']['field'] = 'body';
  58. $handler->display->display_options['fields']['body']['label'] = '';
  59. $handler->display->display_options['fields']['body']['element_label_colon'] = FALSE;
  60. /* Sort criterion: Content: Post date */
  61. $handler->display->display_options['sorts']['created']['id'] = 'created';
  62. $handler->display->display_options['sorts']['created']['table'] = 'node';
  63. $handler->display->display_options['sorts']['created']['field'] = 'created';
  64. $handler->display->display_options['sorts']['created']['order'] = 'DESC';
  65. /* Contextual filter: Content: Author uid */
  66. $handler->display->display_options['arguments']['uid']['id'] = 'uid';
  67. $handler->display->display_options['arguments']['uid']['table'] = 'node';
  68. $handler->display->display_options['arguments']['uid']['field'] = 'uid';
  69. $handler->display->display_options['arguments']['uid']['default_action'] = 'default';
  70. $handler->display->display_options['arguments']['uid']['default_argument_type'] = 'raw';
  71. $handler->display->display_options['arguments']['uid']['default_argument_options']['index'] = '0';
  72. $handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
  73. $handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
  74. $handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
  75. /* Contextual filter: Content: Title */
  76. $handler->display->display_options['arguments']['title']['id'] = 'title';
  77. $handler->display->display_options['arguments']['title']['table'] = 'node';
  78. $handler->display->display_options['arguments']['title']['field'] = 'title';
  79. $handler->display->display_options['arguments']['title']['default_action'] = 'default';
  80. $handler->display->display_options['arguments']['title']['default_argument_type'] = 'raw';
  81. $handler->display->display_options['arguments']['title']['default_argument_options']['index'] = '1';
  82. $handler->display->display_options['arguments']['title']['summary']['number_of_records'] = '0';
  83. $handler->display->display_options['arguments']['title']['summary']['format'] = 'default_summary';
  84. $handler->display->display_options['arguments']['title']['summary_options']['items_per_page'] = '25';
  85. $handler->display->display_options['arguments']['title']['limit'] = '0';
  86. /* Filter criterion: Content: Published */
  87. $handler->display->display_options['filters']['status']['id'] = 'status';
  88. $handler->display->display_options['filters']['status']['table'] = 'node';
  89. $handler->display->display_options['filters']['status']['field'] = 'status';
  90. $handler->display->display_options['filters']['status']['value'] = 1;
  91. $handler->display->display_options['filters']['status']['group'] = 1;
  92. $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
  93. /* Filter criterion: Content: Type */
  94. $handler->display->display_options['filters']['type']['id'] = 'type';
  95. $handler->display->display_options['filters']['type']['table'] = 'node';
  96. $handler->display->display_options['filters']['type']['field'] = 'type';
  97. $handler->display->display_options['filters']['type']['value'] = array(
  98. 'article' => 'article',
  99. );
  100.  
  101. /* Display: Page */
  102. $handler = $view->new_display('page', 'Page', 'page');
  103. $handler->display->display_options['path'] = 'articles';
  104. //label Replace the above code with your views export
  105.  
  106. $export['test'] = $view;
  107. return $export;
  108. }
  109.  
  110. /**
  111.  *
  112.  * Implements hook_menu()
  113.  * Call a custom form as menu callback
  114.  * @return
  115.  * $items array of menu items created programmatically
  116.  */
  117. function test_menu() {
  118.  
  119. $items['articles-listing'] = array(
  120. 'title' => t('Article Listing'),
  121. 'page callback' => 'drupal_get_form',
  122. 'page arguments' => array('test_article_listing', 1),
  123. 'access arguments' => array('access content'),
  124. );
  125. return $items;
  126. }
  127.  
  128. /**
  129.  * fucntion to return custom form on menu callback
  130.  * @param
  131.  * type $form an array consists of form fields
  132.  * type $form_state form field values entered by user
  133.  * @return
  134.  * $form returns array of dorm fields.
  135.  */
  136. function test_article_listing($form, $form_state) {
  137. $form = array();
  138.  
  139. // check if values exists in form state on form building and assigning uid and article name accordingly.
  140. if (isset($form_state['values'])) {
  141. $uid = $form_state['values']['article_list'];
  142. $group_name = $form_state['values']['article_name'];
  143. if ($group_name == '') {
  144. $group_name = 'all';
  145. }
  146. }
  147. else {
  148. $uid = 'all';
  149. $group_name = 'all';
  150. }
  151.  
  152.  
  153. global $user;
  154. $user_id = $user->uid;
  155.  
  156. $form['article_list'] = array(
  157. '#type' => 'select',
  158. '#prefix' => '<div id="article-list-filter-options">',
  159. '#suffix' => '</div>',
  160. '#options' => array(
  161. 'all' => t('All Articles'),
  162. $user_id => t('My Articles'),
  163. ),
  164. // Defining Ajax property for form fields
  165. '#ajax' => array(
  166. 'callback' => 'ajax_article_list_callback',
  167. 'wrapper' => 'replace_article_list_div',
  168. ),
  169. );
  170. $form['article_name'] = array(
  171. '#type' => 'textfield',
  172. '#prefix' => '<div id="article-name-filter-options">',
  173. '#suffix' => '</div>',
  174. '#size' => 20,
  175. );
  176. $form['submit_form'] = array(
  177. '#type' => 'button',
  178. '#value' => t('Submit'),
  179. '#ajax' => array(
  180. 'callback' => 'ajax_article_list_callback',
  181. 'wrapper' => 'replace_article_list_div',
  182. ),
  183. '#prefix' => '<div id="group_list_submit">',
  184. '#suffix' => '</div>',
  185. );
  186. $form['replace_article_list'] = array(
  187. '#type' => 'markup',
  188. // calling view with context filter as argument
  189. '#markup' => views_embed_view("test_view", "page", $uid, $group_name),
  190. '#prefix' => '<div id="replace_article_list_div">',
  191. '#suffix' => '</div>',
  192. );
  193. return $form;
  194. }
  195.  
  196. function ajax_article_list_callback($form, $form_state) {
  197. // The form has already been submitted and updated. We can return the replaced
  198. // item as it is.
  199. return $form['replace_article_list'];
  200. }