heartbeat.views.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @file
  4. * Contains heartbeat\heartbeat.views.inc..
  5. * Provide a custom views field data that isn't tied to any other module. */
  6. use Drupal\Component\Utility\NestedArray;
  7. use Drupal\Core\Entity\EntityStorageInterface;
  8. use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
  9. use Drupal\Core\Render\Markup;
  10. use Drupal\field\FieldConfigInterface;
  11. use Drupal\field\FieldStorageConfigInterface;
  12. use Drupal\system\ActionConfigEntityInterface;
  13. use Drupal\views\Plugin\views\query\QueryPluginBase;
  14. use Drupal\views\ViewExecutable;
  15. use Drupal\views\Views;
  16. /**
  17. * Implements hook_views_data().
  18. */
  19. function heartbeat_views_data() {
  20. $data['views']['table']['group'] = t('Custom Global');
  21. $data['views']['table']['join'] = array(
  22. // #global is a special flag which allows a table to appear all the time.
  23. '#global' => array(),
  24. );
  25. $data['views']['heartbeat_message_field'] = array(
  26. 'title' => t('Heartbeat message field'),
  27. 'help' => t('Heartbeat field formatter which allows for rendering of HTML'),
  28. 'field' => array(
  29. 'id' => 'heartbeat_message_field',
  30. ),
  31. );
  32. // Base data.
  33. $data = [];
  34. $data['heartbeat_friendship'] = [];
  35. $data['heartbeat_friendship']['table'] = [];
  36. $data['heartbeat_friendship']['table']['group'] = t('Heartbeat Friendship');
  37. $data['heartbeat_friendship']['table']['base'] = [
  38. 'field' => 'id',
  39. 'title' => t('Friendships'),
  40. 'help' => t('Maintains friendship status between Users'),
  41. // 'query_id' => 'teacher',
  42. ];
  43. // Fields.
  44. $data['heartbeat_friendship']['id'] = [
  45. 'title' => t('id'),
  46. 'help' => t('Friendship ID'),
  47. 'field' => [
  48. 'id' => 'numeric',
  49. ],
  50. 'sort' => array(
  51. // ID of sort handler plugin to use.
  52. 'id' => 'standard',
  53. ),
  54. 'filter' => array(
  55. // ID of filter handler plugin to use.
  56. 'id' => 'numeric',
  57. ),
  58. 'argument' => array(
  59. // ID of argument handler plugin to use.
  60. 'id' => 'numeric',
  61. ),
  62. ];
  63. $data['heartbeat_friendship']['uid'] = [
  64. 'title' => t('UID'),
  65. 'help' => t('User\'s Unique ID.'),
  66. 'field' => [
  67. 'id' => 'numeric',
  68. ],
  69. 'sort' => array(
  70. // ID of sort handler plugin to use.
  71. 'id' => 'standard',
  72. ),
  73. 'filter' => array(
  74. // ID of filter handler plugin to use.
  75. 'id' => 'numeric',
  76. ),
  77. 'argument' => array(
  78. // ID of argument handler plugin to use.
  79. 'id' => 'numeric',
  80. ),
  81. 'relationship' => array(
  82. 'title' => t('User'),
  83. 'help' => t(''),
  84. 'base' => 'users_field_data',
  85. 'base field' => 'uid',
  86. 'id' => 'standard',
  87. ),
  88. ];
  89. $data['heartbeat_friendship']['uid_target'] = [
  90. 'title' => t('UID Target'),
  91. 'help' => t('Unique ID of the User who is the target of the relationship'),
  92. 'field' => [
  93. 'id' => 'numeric',
  94. ],
  95. 'sort' => array(
  96. // ID of sort handler plugin to use.
  97. 'id' => 'standard',
  98. ),
  99. 'filter' => array(
  100. // ID of filter handler plugin to use.
  101. 'id' => 'numeric',
  102. ),
  103. 'argument' => array(
  104. // ID of argument handler plugin to use.
  105. 'id' => 'numeric',
  106. ),
  107. 'relationship' => array(
  108. 'title' => t('User Target'),
  109. 'help' => t(''),
  110. 'base' => 'users_field_data',
  111. 'base field' => 'uid',
  112. 'id' => 'standard',
  113. ),
  114. ];
  115. $data['heartbeat_friendship']['status'] = [
  116. 'title' => t('Status'),
  117. 'help' => t('The status of the friendship'),
  118. 'field' => [
  119. 'id' => 'numeric',
  120. ],
  121. 'sort' => array(
  122. // ID of sort handler plugin to use.
  123. 'id' => 'standard',
  124. ),
  125. 'filter' => array(
  126. // ID of filter handler plugin to use.
  127. 'id' => 'numeric',
  128. ),
  129. 'argument' => array(
  130. // ID of argument handler plugin to use.
  131. 'id' => 'numeric',
  132. ),
  133. ];
  134. $data['heartbeat_friendship']['table']['join'] = [
  135. 'users_field_data' => [
  136. 'left_field' => 'uid',
  137. 'field' => 'uid',
  138. ],
  139. ];
  140. return $data;
  141. }
  142. /**
  143. * Implements hook_views_query_alter().
  144. * @param ViewExecutable $view
  145. * @param QueryPluginBase $query
  146. */
  147. function heartbeat_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  148. switch ($view->id()) {
  149. case 'heartbeat_friendship':
  150. $configuration = array(
  151. 'table' => 'users_field_data',
  152. 'field' => 'uid',
  153. 'left_table' => 'heartbeat_friendship',
  154. 'left_field' => 'uid_target',
  155. 'operator' => '=',
  156. );
  157. $join = Views::pluginManager('join')->createInstance('standard', $configuration);
  158. $query->addRelationship('users_target', $join);
  159. break;
  160. case 'user_friends':
  161. $friendData = \json_decode(\Drupal::config('heartbeat_friendship.settings')->get('data'));
  162. $friendUids = [];
  163. foreach ($friendData as $data) {
  164. $friendUids[] = $data->uid;
  165. $friendUids[] = $data->uid_target;
  166. }
  167. $query->addWhere('AND', 'users_field_data.uid', \Drupal::currentUser()->id(), '!=');
  168. $query->addWhere('AND', 'users_field_data.uid', array_unique($friendUids), 'IN');
  169. break;
  170. }
  171. }
  172. /**
  173. * Implements hook_views_pre_render().
  174. */
  175. //function heartbeat_views_pre_render(ViewExecutable $view) {
  176. // $friendStatuses = [-1 => 'No Friendship', 0 => 'Pending Friendship', 1 => 'Minimum Bromance'];
  177. // if ($view->id() === 'heartbeat_friendship') {
  178. // foreach ($view->result as $row) {
  179. //// $row->heartbeat_friendship_status = $friendStatuses[$row->heartbeat_friendship_status];
  180. // }
  181. // }
  182. //}