On this page
Tutorial: Last visited content block
In this tutorial we will learn how to create a block that shows each user the last content (i.e. nodes) she has visited. For this we will use the Flag module and take advantage of it's integration with the Rules and Views modules.
- Add a new flag called
last_visited_content, enable it for any content type (e.g. story) and make sure to disable all theDisplay optionscheckboxes, as we don't want the user to control those flags - it should happen automatically for her. - Import the attached Rules configuration, which basically says If a content is viewed as a page then flag it, and trim the flag 'queue' to 5 (you can always change this number via the Rules configuration). Note: you should enable PHP filter module (Drupal core) for the configuration to work.
- Import the attached Views configuration, which adds a block view of the flag
- Enable the
Latest viewed contentblock. Once you will start viewing content it will be automatically flagged for you.
Caution Regarding SortOrder
When you visit a node, it will get flagged. However, the Flag module only flags items (nodes, in this case) that aren't already flagged. In other words, the timestamp field attached to each flagging operation won't be updated when you visit a node already flagged.
So re-visiting a node won't put it on the top of your sorted View. Another implication: since this node isn't "pushed" to the front of the queue (the timestamp field detemines the queue order), the next execution of the "Trim" action might remove its flagging.)
To solve this problem: first unflag the node (or any other item), then flag it.
array (
'rules' =>
array (
'rules_1' =>
array (
'#type' => 'rule',
'#set' => 'event_node_view',
'#label' => 'Last visited content',
'#active' => 1,
'#weight' => '0',
'#categories' =>
array (
0 => '',
),
'#status' => 'custom',
'#conditions' =>
array (
0 =>
array (
'#info' =>
array (
'label' => 'Check a truth value',
'arguments' =>
array (
'boolean' =>
array (
'type' => 'boolean',
'label' => 'Truth value',
),
),
'module' => 'Rules',
),
'#name' => 'rules_condition_check_boolean',
'#settings' =>
array (
'boolean' => '<?php return $page ?>',
'#eval input' =>
array (
'rules_input_evaluator_php' =>
array (
'boolean' =>
array (
0 => 'page',
),
),
),
),
'#type' => 'condition',
'#weight' => 0,
),
),
'#actions' =>
array (
2 =>
array (
'#weight' => -2,
'#info' =>
array (
'label' => 'Flag viewed content, under "Last visited content"',
'base' => 'flag_rules_action_flag',
'label callback' => 'flag_rules_action_flag_label',
'arguments' =>
array (
'flag' =>
array (
'type' => 'flag',
'label' => 'Flag',
'flag_type' => 'node',
),
'object' =>
array (
'type' => 'node',
'label' => 'Flagged content',
),
'flagging_user' =>
array (
'type' => 'user',
'label' => 'User on whose behalf to flag',
'description' => 'For non-global flags, this is the user on whose behalf to flag the object. In addition, if checked below, the access permissions to the flag are checked against this user.',
),
),
'module' => 'Flag',
),
'#name' => 'flag_rules_action_flag_node',
'#settings' =>
array (
'flag' => 'last_visited_content',
'permission_check' => 0,
'#argument map' =>
array (
'object' => 'node',
'flagging_user' => 'user',
),
),
'#type' => 'action',
),
1 =>
array (
'#type' => 'action',
'#settings' =>
array (
'flag' => 'last_visited_content',
'cutoff_size' => '5',
'#argument map' =>
array (
'flagging_user' => 'user',
),
),
'#name' => 'flag_rules_action_trim',
'#info' =>
array (
'label' => 'Trim "Last visited content" at 5',
'arguments' =>
array (
'flag' =>
array (
'type' => 'flag',
'label' => 'Flag',
),
'flagging_user' =>
array (
'type' => 'user',
'label' => 'User whose flag to trim',
'description' => 'For non-global flags, this is the user whose flag to trim. (For global flags, this argument is ignored.)',
),
'cutoff_size' =>
array (
'type' => 'number',
'label' => 'Flag queue size',
'description' => 'The maximum number of objects to keep in the queue. Newly flagged objects will be kept; older ones will be removed. Tip: by typing "1" here you implement a <a href="http://en.wikipedia.org/wiki/Singleton_pattern">singleton</a>.',
),
),
'module' => 'Flag',
),
'#weight' => 0,
),
),
),
),
)
$view = new view;
$view->name = 'flag_last_viewed_content';
$view->description = '';
$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(
'flag_content_rel' => array(
'label' => 'flag',
'required' => 1,
'flag' => 'last_visited_content',
'user_scope' => 'current',
'id' => 'flag_content_rel',
'table' => 'node',
'field' => 'flag_content_rel',
'relationship' => 'none',
),
));
$handler->override_option('fields', array(
'title' => array(
'label' => '',
'alter' => array(
'alter_text' => 0,
'text' => '',
'make_link' => 0,
'path' => '',
'alt' => '',
'prefix' => '',
'suffix' => '',
'help' => '',
'trim' => 0,
'max_length' => '',
'word_boundary' => 1,
'ellipsis' => 1,
'html' => 0,
),
'link_to_node' => 1,
'exclude' => 0,
'id' => 'title',
'table' => 'node',
'field' => 'title',
'relationship' => 'none',
),
));
$handler->override_option('sorts', array(
'timestamp' => array(
'order' => 'DESC',
'granularity' => 'second',
'id' => 'timestamp',
'table' => 'flag_content',
'field' => 'timestamp',
'relationship' => 'flag_content_rel',
'override' => array(
'button' => 'Override',
),
),
));
$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',
),
));
$handler->override_option('access', array(
'type' => 'none',
));
$handler->override_option('title', 'Last visited content');
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
'grouping' => '',
'type' => 'ul',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', 'Last content viewed');
$handler->override_option('block_caching', -1);
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion