I found out that it is impossible to add a custom query tag to a view. I needed that functionality in a project (filtering on the 'currently selected' website-wide filter). I found the solution below, which basically checks for the description of the view (searches for '(addTag:TAG_NAME)') and adds tags using hook_views_query_substitutions.

It would be nice if there is an option in the View UI interface in which you can add some tags without having to code. It doesn't necessarily have to be advanced: a simple textfield in which you can put comma-separated tags would be great.

It would make views more flexible in it's interaction with 3rd party modules and custom code.

A possibility?

/**
 * Implements hook_views_query_substitutions().
 * @param $view
 */
function filter_post_views_query_substitutions(&$view) {
	if ($view instanceof view && property_exists($view, 'human_name')) {
		//Parse the view for an addTag expression and adds the defined tags as tags
		$human_name = $view->human_name;
		$matches = array();
		if (preg_match("/\(addTag:(.+)\)$/", $human_name, $matches)) {
			$tags = explode(',', $matches[1]);
			foreach($tags as $tag) {
				$view->build_info['query']->addTag($tag);
			}
		}
	}
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

You know this seems to be a feature for me, which will probably be misunderstood by most people

Why not using the $view->query->add_tag function.

fubhy’s picture

FileSize
3.57 KB
fubhy’s picture

Status: Active » Needs review
dawehner’s picture

Status: Needs review » Needs work
+++ b/plugins/views_plugin_display.incundefined
@@ -2286,6 +2286,11 @@ class views_plugin_display extends views_plugin {
+      case 'display_tags':
+        $display_tags = explode(',', $form_state['values'][$section]);
+        $display_tags = array_walk($display_tags, 'trim');
+        $this->set_option($section, $display_tags);

I think this might be a leftover from another approach

+++ b/plugins/views_plugin_query_default.incundefined
@@ -1545,3 +1573,16 @@ function views_query_default_aggregation_method_distinct($group_type, $field) {
+function views_element_validate_query_tags($element, &$form_state) {
+  $values = array_map('trim', explode(',', $element['#value']));
+  foreach ($values as $value) {
+    if (preg_match("/[^a-z_]/", $value)) {
+      form_error($element, t('The query tags may only contain lower-case alphabetical characters and underscores.'));
+      return;
+    }

Can't you use the options_validate in the query plugin?

fubhy’s picture

Status: Needs work » Needs review
FileSize
2.75 KB

Yes, sorry... That one was a leftover from my first approach (before I placed it in "Query Settings").

Regarding #2: I always prefer element_validate because that is exactly what it is good for (for me, the form validate function is good if multiple form elements need to be checked against each other and compared with each other, element_validate is nice for single form elements). So I would leave it like it is, but if you prefer the other approach I can of course use the form validator too.

fubhy’s picture

FileSize
2.74 KB

Renamed the element validator to views_element_validate_tags()

dawehner’s picture

Status: Needs review » Fixed

Oh actually this is already committed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bvanmeurs’s picture

Great work guys. Thanks!