Hi all,
I'm trying to create a frontpage on my site so that each individual user sees a personalized homepage based on user role. Because I'm using attachments, I want to randomize the nodes before I sort them. When I remove the 'Sort criteria: Random' the view functions perfectly, except for they are not in the order that I want them in. However, when I 'Sort criteria: Random' is turned on, I get duplicate nodes. This view does use arguments, and the number of duplicates equal the number of times that node would be loaded because of the argument. (If the user is assigned multiple roles, and the page is promoted to the frontpage for those multiple roles, I get duplicate views.)
Quick note, when I try to turn Distinct on, all nodes disappear.
This is the view:
$view = new view;
$view->name = 'home_page_new';
$view->description = 'Displays node content selected for particular home pages';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
'field_homepage_role_rid' => array(
'label' => 'Home Page for',
'required' => 1,
'delta' => '-1',
'id' => 'field_homepage_role_rid',
'table' => 'node_data_field_homepage_role',
'field' => 'field_homepage_role_rid',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
),
));
$handler->override_option('sorts', array(
'weight' => array(
'order' => 'ASC',
'id' => 'weight',
'table' => 'node_weight',
'field' => 'weight',
'relationship' => 'none',
'override' => array(
'button' => 'Override',
),
),
'type' => array(
'order' => 'DESC',
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'rid' => array(
'default_action' => 'ignore',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'fixed',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'add_table' => 0,
'require_value' => 0,
'reduce_duplicates' => 1,
'id' => 'rid',
'table' => 'users_roles',
'field' => 'rid',
'validate_user_argument_type' => 'uid',
'validate_user_roles' => array(
'2' => 0,
'16' => 0,
'11' => 0,
'10' => 0,
'12' => 0,
'14' => 0,
'13' => 0,
'15' => 0,
),
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
'default_options_div_prefix' => '',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(
'poll' => 0,
'faq' => 0,
'book' => 0,
'contact' => 0,
'event' => 0,
'gearlist' => 0,
'guest' => 0,
'health' => 0,
'page' => 0,
'patrol' => 0,
'position' => 0,
'profile' => 0,
'project' => 0,
'story' => 0,
'task' => 0,
'wiki' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'4' => 0,
'5' => 0,
'6' => 0,
'7' => 0,
'8' => 0,
'14' => 0,
'15' => 0,
'16' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_transform' => 0,
'validate_user_restrict_roles' => 0,
'validate_argument_signup_status' => 'any',
'validate_argument_signup_node_access' => 0,
'validate_argument_php' => '',
),
));
$handler->override_option('filters', array(
'status' => array(
'operator' => '=',
'value' => '1',
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'status',
'table' => 'node',
'field' => 'status',
'relationship' => 'none',
),
'type' => array(
'operator' => 'not in',
'value' => array(
'poll' => 'poll',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('distinct', 0);
$handler->override_option('row_plugin', 'node');
$handler->override_option('row_options', array(
'relationship' => 'none',
'build_mode' => 'teaser',
'links' => 0,
'comments' => 0,
));
$handler = $view->new_display('attachment', 'Upcoming Events', 'attachment_1');
$handler->override_option('relationships', array());
$handler->override_option('sorts', array(
'field_event_date_time_value' => array(
'order' => 'ASC',
'delta' => -1,
'id' => 'field_event_date_time_value',
'table' => 'node_data_field_event_date_time',
'field' => 'field_event_date_time_value',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
),
));
$handler->override_option('arguments', array());
$handler->override_option('filters', array(
'status' => array(
'operator' => '=',
'value' => '1',
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'status',
'table' => 'node',
'field' => 'status',
'relationship' => 'none',
),
'type' => array(
'operator' => 'in',
'value' => array(
'event' => 'event',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
'relationship' => 'none',
'override' => array(
'button' => 'Use default',
),
),
'date_filter' => array(
'operator' => 'between',
'value' => array(
'value' => NULL,
'min' => NULL,
'max' => NULL,
'default_date' => 'now',
'default_to_date' => 'now +21 day',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'date_fields' => array(
'node_data_field_event_date_time.field_event_date_time_value' => 'node_data_field_event_date_time.field_event_date_time_value',
),
'date_method' => 'OR',
'granularity' => 'day',
'form_type' => 'date_select',
'default_date' => 'now',
'default_to_date' => 'now +21 day',
'year_range' => '-1:+1',
'id' => 'date_filter',
'table' => 'node',
'field' => 'date_filter',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
),
'rid' => array(
'operator' => 'or',
'value' => array(
'10' => '10',
'12' => '12',
'14' => '14',
'13' => '13',
'15' => '15',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'rid',
'table' => 'users_roles',
'field' => 'rid',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
'reduce_duplicates' => 0,
),
'field_homepage_role_rid' => array(
'operator' => 'not',
'value' => array(
'16' => '16',
'11' => '11',
'1' => '1',
'2' => '2',
'10' => '10',
'12' => '12',
'14' => '14',
'13' => '13',
'15' => '15',
),
'group' => '0',
'exposed' => FALSE,
'expose' => array(
'operator' => FALSE,
'label' => '',
),
'id' => 'field_homepage_role_rid',
'table' => 'node_data_field_homepage_role',
'field' => 'field_homepage_role_rid',
'override' => array(
'button' => 'Use default',
),
'relationship' => 'none',
'reduce_duplicates' => 1,
),
));
$handler->override_option('items_per_page', 3);
$handler->override_option('attachment_position', 'after');
$handler->override_option('inherit_arguments', 0);
$handler->override_option('inherit_exposed_filters', FALSE);
$handler->override_option('displays', array(
'default' => 'default',
'page_5' => 'page_5',
));
$handler = $view->new_display('page', 'Home', 'page_5');
$handler->override_option('path', 'home');
$handler->override_option('menu', array(
'type' => 'none',
'title' => '',
'description' => '',
'weight' => 0,
'name' => 'navigation',
));
$handler->override_option('tab_options', array(
'type' => 'none',
'title' => '',
'description' => '',
'weight' => 0,
));
And here this query:
SELECT node.nid AS nid,
node_weight.sticky AS node_weight_sticky,
node.type AS node_type,
RAND() AS _random
FROM node node
LEFT JOIN content_field_homepage_role node_data_field_homepage_role ON node.vid = node_data_field_homepage_role.vid
INNER JOIN role role_node_data_field_homepage_role ON node_data_field_homepage_role.field_homepage_role_rid = role_node_data_field_homepage_role.rid
LEFT JOIN node node_weight ON node.nid = node_weight.nid
WHERE (node.status <> 0) AND (node.type not in ('poll'))
ORDER BY node_weight_sticky DESC, node_type DESC, _random ASC| Comment | File | Size | Author |
|---|---|---|---|
| #8 | views_handler_sort_random_patch.patch | 1.09 KB | andriy |
Comments
Comment #1
schultetwin commentedOops, sorry. I forgot to adjust one thing before I copied the sql query. It should be the following. (Exact same as above except for the order in the last line is different.
Comment #2
schultetwin commentedI'm pretty sure this is related too, if not a duplicate of, #384684: Duplicate records when selecting Random Sort Criteria. However, this occurs for a different version, and so I can't use the workaround listed there.
Comment #3
dawehnerIs this still the case with a views 2.11 and a current version of drupal core?
Comment #4
esmerel commentedNo updates for more than 30 days.
Comment #5
jordanmagnuson commentedI'm getting this same behavior with the latest 3.x dev. Other sort criteria -> no duplicates. Random sort criteria -> duplicates emerge. Turning distinct results on does remove the duplicates, but they shouldn't be there to begin with, as far as I can tell.
Here's my view:
Comment #6
andriy commentedhttp://drupal.org/node/384684#comment-3796282
the problem is in views_handler_sort_random.inc, changing it to
will fix the problem
Comment #7
dawehnerThis might cause some notices.
In general: It would be cool if you create a patch!
Comment #8
andriy commentedHere is a patch, also what do ypu mean by "This might cause some notices"? I need to pass the same argument between all the pages of the same view (with and without ajax), so I used $_SESSION, of you have another idea or improvement, please advise. Thanks
Comment #9
dawehnerSo
Comment #10
dawehnerCan you make this behavior configurable?
So add a option via option_definition and create a form via options_form.
It's important that noone get's unexpected changeds
Comment #11
dawehnerSomeone already wrote a module for it, see http://drupal.org/project/views_random_seed
Please use this module. Thanks
Comment #12
andriy commentedthanks for pointing to this module, I almost contributed very similar module. Is it normal behavior for random sort criteria to have duplicates on paged view queries? I am asking, because I posted my patch also to views.module development area. Thanks again for your replies. That was my first post to drupal.org :)