We already know there can be problems when you delete CCK fields that are used in Views. There is also a related problem cleaning up Views when the modules that created the fields are disabled or uninstalled. I have been thinking about possible ways to fix this, and I think it might be easier to fix if we added a 'module' field to the 'views_fields', 'views_arguments', and 'views_filters' tables that would store the name of the module that created that field.

Then we could create a few helper functions to make it easy to find and remove fields, like:

views_get_fields($module) - return a list of that module's fields which are currently in use.

views_remove_fields($module, $field_name = NULL) - delete all fields or a specific field for a module from all views tables and clear the views cache.

Either Views or individual modules could then use these functions when modules are disabled or uninstalled to clean views up, and CCK could also use them when individual fields are deleted.

I didn't make a patch, just checking to see how people feel about the concept.

CommentFileSizeAuthor
#3 views_9.patch12.42 KBkarens
#2 views_8.patch13.57 KBkarens

Comments

merlinofchaos’s picture

Big big +1 from me;

Stuff like this has been running through the back of my mind, but hasn't really done more than be thought about. I'd happily jump on work to do this.

karens’s picture

Status: Active » Needs review
StatusFileSize
new13.57 KB

Here's a patch then. This patch does the following:

1) adds a 'module' column to each of the views tables and adds an update to the install file

2) alters the code in _views_get_tables() and _views_get_arguments() to store the module name in the cached arrays

3) alters the code that saves the views fields so it saves the module name in the table

4) creates a new helper function called views_get_module_fields($module) to return an array of all of that modules fields that are currently used in views

5) creates a new helper function called views_delete_module_fields($module, $field_name = NULL) that deletes all the module fields from views tables

6) creates a new helper function called views_save_all() that pulls up and saves all views so their tables get updated.

karens’s picture

StatusFileSize
new12.42 KB

Oops, some debugging cruft was left in that last patch.

karens’s picture

I've been working a bit more with this patch and have been struggling with some views that kept trying to use deleted fields even after I cleared all the caches and removed the fields from all the views tables. After going down a dozen blind allies, I think I am figuring some of it out.

Part of the problem was of my own creation since I was caching some views info in my own caches instead of the views cache so views_invalidate_cache() wasn't clearing them and my bad cached values were getting re-inserted. I'm doing some patching of my modules now to move anything I cache that has any views info to the views cache so it will get cleared whenever views_invalidate_cache() is called.

Another thing that may be a problem is the static variables in _views_get_tables() and _views_get_arguments(). I think old values might be getting saved in the static values and then getting reinserted into the view when they shouldn't. I'm playing around with adding a $reset flag to clear those static values out when views_invalidate_cache() is called so no old values leak back in. I'm still trying to be sure whether this is really needed.

One last static value that might or might not be a problem is a static value in _views_load_view(). I'm wondering if that might be another place where invalid info is getting trapped and reinserted.

moshe weitzman’s picture

@karen - on a related note, Views need a hook when a View is renamed. Panels, OG, and other modules rely on View names and will break if they suddenly change. The hook is analogous to hook_node_type which reacts to content type changes. Actually, we might pass the whole View anytime it is saved? Not sure how content type does it but thats probably what we should do here.

i will open a new issue if this is too unrelated.

karens’s picture

It makes sense to go ahead and add a hook here. Actually I can maybe see two hooks -- one for anytime a view is edited and one for anytime views_invalidate_cache() is called (when presumably all other modules that rely on Views data need to flush their own values).

I can try to work that in, too.

merlinofchaos’s picture

This means I'm actually going to have to start applying Views 1.7 patches and get something out =)

sun’s picture

Version: 5.x-1.x-dev » 6.x-2.x-dev
Status: Needs review » Active

This sounds plain awesome and I wished I had seen this issue earlier. However, I don't think that adding this to 5.x now makes any sense? Hence, moving to 6.x. Please correct me if I'm wrong.

merlinofchaos’s picture

Status: Active » Fixed

Views 2 is much better about dealing with items that get removed unexpectedly. At least, it doesn't crash.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.