1

Imagine a user has ten fields of type "List(text)" which each have for their widget "Check boxes/radio buttons". Each one is displayed to the user as a single value checkbox which asks the user to state whether they agree with something or not.

A distinct Computed Field of data type TinyInt uses custom PHP to examine the values of these List(text) fields checkboxes. If all are checked the Computed Field is set to have the value of 1. If any or all are unchecked the Computed Field is set to have the value of 0.

(Ideally I would like the Computed Field to be of Boolean type - so simple TRUE/FALSE - but this does not seem possible out of the box. Anyway, the real question is the following.)

In a View with exposed fields, how can you use the Computed Field as an exposed field that offers the user a single checkbox: "Find all users where the Computed Field value is 1: [•]".

If this isn't directly possible, I'm happy to have a workaround that involves creating a distinct (hidden) field in the user profile and then uses this for the Views exposed filter search. But I can't think of a way to do this right now... Thanks.

  • Can you tell what is happening when you expose the field now and filter all values equal to 1? – AltaGrade Mar 30 '18 at 22:37
  • Sorry AltaGrade I don't quite understand what you're asking... – penname Apr 3 '18 at 21:00
0
  1. Change exposed computed field's filter type to "Grouped filters",
  2. Widget type to "Radios",
  3. Check "Allow multiple selections" checkbox,
  4. Expose one option equal to 1 with your choice of label

See screenshot: computed field - exposed filter settings

computed field - exposed filter

The view export:

<?php

$view = new view();
$view->name = 'computed_field_test';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Computed Field Test';
$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'] = 'Computed Field Test';
$handler->display->display_options['use_more_always'] = FALSE;
$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['exposed_form']['type'] = 'basic';
$handler->display->display_options['exposed_form']['options']['autosubmit'] = TRUE;
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Content: List 1 */
$handler->display->display_options['fields']['field_list_1']['id'] = 'field_list_1';
$handler->display->display_options['fields']['field_list_1']['table'] = 'field_data_field_list_1';
$handler->display->display_options['fields']['field_list_1']['field'] = 'field_list_1';
/* Field: Content: List 2 */
$handler->display->display_options['fields']['field_list_2']['id'] = 'field_list_2';
$handler->display->display_options['fields']['field_list_2']['table'] = 'field_data_field_list_2';
$handler->display->display_options['fields']['field_list_2']['field'] = 'field_list_2';
/* Field: Content: Computed */
$handler->display->display_options['fields']['field_computed']['id'] = 'field_computed';
$handler->display->display_options['fields']['field_computed']['table'] = 'field_data_field_computed';
$handler->display->display_options['fields']['field_computed']['field'] = 'field_computed';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'page' => 'page',
);
/* Filter criterion: Content: Computed (field_computed) */
$handler->display->display_options['filters']['field_computed_value']['id'] = 'field_computed_value';
$handler->display->display_options['filters']['field_computed_value']['table'] = 'field_data_field_computed';
$handler->display->display_options['filters']['field_computed_value']['field'] = 'field_computed_value';
$handler->display->display_options['filters']['field_computed_value']['exposed'] = TRUE;
$handler->display->display_options['filters']['field_computed_value']['expose']['operator_id'] = 'field_computed_value_op';
$handler->display->display_options['filters']['field_computed_value']['expose']['label'] = 'Computed (field_computed)';
$handler->display->display_options['filters']['field_computed_value']['expose']['operator'] = 'field_computed_value_op';
$handler->display->display_options['filters']['field_computed_value']['expose']['identifier'] = 'field_computed_value';
$handler->display->display_options['filters']['field_computed_value']['expose']['multiple'] = TRUE;
$handler->display->display_options['filters']['field_computed_value']['is_grouped'] = TRUE;
$handler->display->display_options['filters']['field_computed_value']['group_info']['identifier'] = 'field_computed_value';
$handler->display->display_options['filters']['field_computed_value']['group_info']['optional'] = FALSE;
$handler->display->display_options['filters']['field_computed_value']['group_info']['widget'] = 'radios';
$handler->display->display_options['filters']['field_computed_value']['group_info']['multiple'] = TRUE;
$handler->display->display_options['filters']['field_computed_value']['group_info']['group_items'] = array(
  1 => array(
    'title' => 'Find all users where the Computed Field value is 1',
    'operator' => '=',
    'value' => array(
      'value' => '1',
      'min' => '',
      'max' => '',
    ),
  ),
  2 => array(
    'title' => '',
    'operator' => '=',
    'value' => array(
      'value' => '',
      'min' => '',
      'max' => '',
    ),
  ),
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'computed-field-test';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Computed Field Test';
$handler->display->display_options['menu']['name'] = 'devel';
  • Thank you. I have configured the view as per your answer, but it is showing a radio button, not a checkbox. Screenshots: Views config: prntscr.com/j0g83t Views config preview: prntscr.com/j0g919 Views final output prntscr.com/j0g8dk Any ideas why this might be? – penname Apr 3 '18 at 20:59
  • did you make sure the "Allow multiple selections" is checked and "Optional" is unchecked? – osman Apr 3 '18 at 21:06
  • @penname I added PHP export of the view I used for creating the screenshots. Please compare it with yours. If still have issues, please export your view and post it in here. – osman Apr 3 '18 at 21:16
  • Yes. The View is configured exactly as you said (and as I showed in the screenshots, which are on the LightShot prnt.sc site if you were hesitant to click on the links). May I ask, which version of Views and Drupal are you using? Do you have any other relevant modules installed? – penname Apr 3 '18 at 21:19
  • Ok, thank you for the export. I will create a fresh dev site and test. – penname Apr 3 '18 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.