Cache views per enabled language

hctom - August 21, 2009 - 22:32
Project:Views
Version:6.x-2.x-dev
Component:Views Data
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Issue tags:cache, node translation
Description

Hi @all,

I just mentioned that there is a problem with cached views using the "Node translation: Language" filter. In my case I have a view that should only present nodes in the current user's language. If I cache the view, the first access of it (e.g. with English language is selected) is cached and switching the website's language to another language does not have an effect on the view at all anymore. So I thought about caching the views per language.

Do you think this would be applicable?

Thanx in advance & cheers

hctom

#1

dereine - August 22, 2009 - 09:45

The cool stuff about the current caching system thats its pluggable.

Just extend the views_plugin_cache_time class to what you need.

Perhaps there could be a default class for this.

#2

hctom - August 22, 2009 - 14:37

@dereine: Thanx for the hint... I'll give that a try :)

Cheers

hctom

#3

hctom - August 22, 2009 - 16:10

@dereine: This works perfectly :)

If anybody is interested in a cache plugin like this, you may use the file attached to this post.

Cheers

hctom

AttachmentSize
Cache plugin: time-based respecting website language 2.27 KB

#4

hctom - August 22, 2009 - 16:11
Status:active» closed

#5

Bensbury - November 26, 2009 - 12:09
Status:closed» active

Hi, that plugin is exactly what I need.

Sad thing is it doesn't work perfectly for me.

I've named it views_plugin_cache_time_locale.inc

Put it in the plugins folder...... but nothing.
No options, no effect.

Have flushed a bunch of caches, also changed to 6 -2 .dev

But can't get it to work.
so close!

How do I get the plugin rocking?

Thanks.

#6

hctom - November 26, 2009 - 12:39

Hi Bensburry,

did you register the plugin via hook_views_plugins()? Otherwise the views module won't be aware of it :-)

You need to define something like this in your MODULENAME.views.in:

<?php
/**
* Implementation of hook_views_plugins().
*/
function MODULENAME_views_plugins() {
 
// Set up path variables
 
$path_plugins = 'path/to/your/plugins';
 
$path_views = drupal_get_path('module', 'views');
 
// Register plugins
 
$plugins = array();
 
$plugins['module'] = 'MODULENAME';
 
// Cache plugin: time-based with locale
 
$plugins['cache']['MODULENAME_cache_time_locale'] = array(
 
'handler' => 'views_plugin_cache_time_locale', // Remember that the plugin filename has to be identical
 
'help' => t('Simple time-based caching of data respecting the website language.'),
 
'parent' => 'time',
 
'path' => $path_plugins,
 
'title' => t('Time-based respecting website language'),
 
'uses options' => TRUE,
  );
 
// ---
 
return $plugins;
}
?>

Hope this helps?

Cheers

hctom

#7

Bensbury - November 26, 2009 - 13:06

So I need to create my own custom module and then reference the plugin?

Sorry, I am power confused now....

Where should I put the plugin?

Currently it is in Views>plugins

Where is the file from where I register the plugin?

Looks to me like I need to make my own module right? (>_<)

I made a file in views>modules called 'steve.views.inc' and put in:

<?php
/**
* Implementation of hook_views_plugins().
*/
function steve_views_plugins() {
 
// Set up path variables
 
$path_plugins = drupal_get_path('module', 'views') . '/plugins/steve'
 
$path_views = drupal_get_path('module', 'views');
 
// Register plugins
 
$plugins = array();
 
$plugins['module'] = 'steve';
 
// Cache plugin: time-based with locale
 
$plugins['cache']['steve_cache_time_locale'] = array(
 
'handler' => 'views_plugin_cache_time_locale', // Remember that the plugin filename has to be identical
 
'help' => t('Simple time-based caching of data respecting the website language.'),
 
'parent' => 'time',
 
'path' => $path_plugins,
 
'title' => t('Time-based respecting website language'),
 
'uses options' => TRUE,
  );
 
// ---
 
return $plugins;
}
?>

then in views>plugins I put steve_plugin_cache_time_locale.inc

I then changed

class views_plugin_cache_time_locale extends views_plugin_cache_time
To -
class steve_plugin_cache_time_locale extends views_plugin_cache_time

...and this must be totally wrong because it didn't work :p

#8

hctom - November 26, 2009 - 16:43

I think it is time to consult the views documentation :)

Or did you install the advanced help module? The views help contained in there gives you a good start on how to use the steve.views.inc file and where to place files etc.

But to summarize in a few words:

  • You have to use an own custom module
  • This module has to implement hook_views_api() (this is where you specify the path to MODULENAME.views.inc)
  • The MODULENAME.views.inc file has to implement hook_views_plugins().
  • In hook_views_plugins() you specify the path where the view plugin is located in your module (path) and the plugin code goes into a file under that path with HANDLER_NAME.inc as a filename
  • NEVER EVER put own customizations or files into contributed modules directories in order to keep them updatable :)

... and don't forget to read the views help/API :) You can aslo find documentation links on the views project page.

Cheers

hctom

#9

Bensbury - November 27, 2009 - 02:04

Thanks,

I'll do my best.

Writing your own modules isn't exactly Drupal 101 and it looks like it's time for the steep Drupal learning curve that it has the reputation for.

The documentation for plugins isn't finished and to me it's high level stuff already assuming that you know how to write a module and then, once knowing how to write a module know about hooking the module up to views.

I searched.... views plugin how to / install , and so forth but there was nothing specific about how to do it, that I could find.

So it's going to be a consult Pro Drupal Development time, learn how to make my own module and then learn how to register it --- on the hope that it will work.

Thanks for your help, I really appreciate it, although I feel there is a massive gap in knowledge between where I am and what you know. Therefore I don't understand about 50% of what I am supposed to do.
I am at the overriding functions in the template document stage using the drupal api.
Before I can do anything I am going to need to learn about module development.

I am all for the teaching a man to fish approach, but also does he have to learn to make his own rod and build a boat too?

Having the language awareness in the views cache is a really import thing I reckon, and I have been able to implement patches when people make them.
Writing whole modules to get it to work seems like an entry barrier.

Don't worry, I'm not complaining, thanks for the pointers and the plugin, this is just a mini-vent to psyche up for learning how to build modules.
After all in the end it will be useful to know!

Okay, module learning time!
gambare.

#10

Bensbury - November 27, 2009 - 09:26

Well that was distinctly brutal and took 7 hours, but now I can write modules.

As I got it working I supposed I could ask a cheeky question....... can you get this to work with themes?
I use theme-switching so if views could cache by theme too, that would be grande.

For people who might not want to go through the same pain, I've attached my module called 'Steve'.

It is actually the module from Drupal Pro Development, then adapted to make the views plugin work.

So half of it you don't need.

To get the plugin to work your module (in this case 'steve') has to contain this function to wake up views.
(the return is probably totally unnecessary)

function steve_views_api() {

return array(api' => 2.0);
}

Then you put in a file in the same folder called steve.views.inc which contains the above information on this page.

This file registers the plugin with Views.
Then also put the plugin file in the same folder.

The hard bit is having to write your own module, and then knowing where you are supposed to put the files.

Hopefully it will all make sense from the zip file.

AttachmentSize
steve.zip 50.9 KB
 
 

Drupal is a registered trademark of Dries Buytaert.