Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / hook / views_query_alter.twig
diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/hook/views_query_alter.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/hook/views_query_alter.twig
new file mode 100644 (file)
index 0000000..d09a7e4
--- /dev/null
@@ -0,0 +1,24 @@
+/**
+ * Implements hook_views_query_alter().
+ */
+function {{ machine_name }}_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
+  // (Example assuming a view with an exposed filter on node title.)
+  // If the input for the title filter is a positive integer, filter against
+  // node ID instead of node title.
+  if ($view->id() == 'my_view' && is_numeric($view->exposed_raw_input['title']) && $view->exposed_raw_input['title'] > 0) {
+    // Traverse through the 'where' part of the query.
+    foreach ($query->where as &$condition_group) {
+      foreach ($condition_group['conditions'] as &$condition) {
+        // If this is the part of the query filtering on title, chang the
+        // condition to filter on node ID.
+        if ($condition['field'] == 'node.title') {
+          $condition = [
+            'field' => 'node.nid',
+            'value' => $view->exposed_raw_input['title'],
+            'operator' => '=',
+          ];
+        }
+      }
+    }
+  }
+}