I'm working with Drupal 7.9 and Views 7.x-3.x-dev with timestamp 10 Nov 2011.
Is there a way to render the base field optional?
I've looked at Option to prevent selecting base field and Consider allowing configuration of the default options for field handlers and several other issues, but don't see what I'm looking for.
Essentially, I want to be able to list either all content types available, or all contents types being used or 'instantiated'. Which would, in its basic form, translate to SQL as:
SELECT DISTINCT type
FROM `node`
But instead what I get is:
SELECT DISTINCT node.type AS node_type, node.nid AS nid
FROM {node} node
Which results in displaying multiples of the content type, because nid is added as the default base field. Removing any optional field results in an error indicating that no field has been selected. So the base field only comes in to play when a field is selected.
I've tried selecting the advanced query setting to distinct. I've also attempting Grouping by the type, but to no avail.
User of filter and sort does not seem to help either.
Would I be able to do this by building a Views plugin? Or would this best be done through an alter hook?
As a test I modified the value of the default base field from 'nid' to 'type' in the init function for views_plugin_query_default.inc. But it seems the default base field is being set elsewhere as well.
Here is my view:
$view = new view;
$view->name = 'content_types';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Content Types';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Content Types';
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['distinct'] = TRUE;
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '0';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'list';
$handler->display->display_options['style_options']['grouping'] = 'type';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Type */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'node';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['type']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['type']['alter']['external'] = 0;
$handler->display->display_options['fields']['type']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['type']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['type']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['type']['alter']['trim'] = 0;
$handler->display->display_options['fields']['type']['alter']['html'] = 0;
$handler->display->display_options['fields']['type']['element_label_colon'] = 1;
$handler->display->display_options['fields']['type']['element_default_classes'] = 1;
$handler->display->display_options['fields']['type']['hide_empty'] = 0;
$handler->display->display_options['fields']['type']['empty_zero'] = 0;
$handler->display->display_options['fields']['type']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['type']['link_to_node'] = 0;
$handler->display->display_options['fields']['type']['machine_name'] = 0;
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'Content Types';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'grid';
$handler->display->display_options['style_options']['columns'] = '6';
$handler->display->display_options['style_options']['fill_single_line'] = 1;
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['inline'] = array(
'type' => 'type',
);
$handler->display->display_options['row_options']['hide_empty'] = 0;
$handler->display->display_options['row_options']['default_field_elements'] = 1;
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['sorts'] = FALSE;
/* Sort criterion: Content: Type */
$handler->display->display_options['sorts']['type']['id'] = 'type';
$handler->display->display_options['sorts']['type']['table'] = 'node';
$handler->display->display_options['sorts']['type']['field'] = 'type';
$handler->display->display_options['defaults']['filters'] = FALSE;
$handler->display->display_options['path'] = 'content-types';
Comments
Comment #1
tomsherlock commentedJust downloaded the Views version for today, 2011 Nov 11.
It appears that the base field does not show on a simple query. I now get:
when selecting the type field.
However, I still get multiples of content types. So I select 'distinct' under advanced query settings. This results in the following query:
So nid is back. The result set has not changed. I'm still getting multiple content types. If we could just remove the nid, not automatically associate it with DISTINCT, should result in a result set without repeating data.
Is this a feature or a bug or an architectural constraint?
Comment #2
merlinofchaos commentedDISTINCT does not work the way you think it does, at least not in Views. That is not the way to proceed.
Under the advanced options turn on aggregation. This adds a lot of new options to all your fields. Put in the absolute minimum fields necessary -- preferably just node type, and possibly a sort on node type. Set the aggregation options appropriately (it should, I hope, be fairly obvious which ones you need).
Comment #3
tomsherlock commentedThanks, Earl. Using aggregation seems to have work. The SQL from Views is:
The result set is exactly what I'm looking for (once I apply exclude from display).
I think your note here about the Views DISTINCT behavior differing from that of SQL (at least through the lens of phpMyAdmin) is very valuable.