I had a need for Views content caches to be able to be invalidated when content that views are built on are flagged. Flag provides a hook when items are flagged or unflagged.

I've attached a patch that adds a plugin to add defined flags as content cache settings.

Comments

hefox’s picture

Stopping in to say: Looks good from a quick look, and thought about doing the same plugin, but realized it wasn't as useful as I'd have liked. My use for clearing on flag is having flag by like an edit event on content: ie I'd want it to clear when the content of certain types is edited or flagged. I did that via using hook_flag in custom, but I wonder if there is a way for views_content to understand that logic via configuration.

hachreak’s picture

StatusFileSize
new2.45 KB

+1

Very useful integration!
I tryed this patch in version 2.2 of this module + views 3, but it seems that not work for me... :(

p.s. I add a default NULL value in options_form of class views_content_cache_key_flag. Otherwise, when I open configuration of cache in views, the views module return me this warning:

warning: Missing argument 2 for views_content_cache_key_flag::options_form()

steven jones’s picture

Status: Needs review » Needs work

We should be able to get this to trigger the cache segments for nodes and comments to record this timestamp additionally, just need to work out how.

jaydub’s picture

Version: 6.x-2.x-dev » 7.x-3.x-dev
Status: Needs work » Needs review
StatusFileSize
new3.38 KB

Trying again this time as a plugin for Views Content Cache for Drupal 7. Has Flag hooks for both the 7.2.x and the 7.3.x branch of Flag.

joemaine’s picture

Hey Jaydub,

Great work! The plugin works as expected. I hope your work gets added to the module!

rafaqz’s picture

Patch looks good, but it's no longer working against current dev.

Line 4 in flag.inc needs to be changed to:

function options_form($value, &$handler = NULL) {

But then I also get this error:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') OR c2 = '7') ORDER BY timestamp DESC' at line 1: SELECT timestamp FROM {views_content_cache} WHERE (() OR c2 = :c2_0) ORDER BY timestamp DESC; Array ( [:c2_0] => 7 ) in views_content_cache_update_get() (line 218 of /drupal/sites/all/modules/contrib/views_content_cache/views_content_cache.module).

Not sure if this is also new or an already existing bug.

MaWebDesigns’s picture

I am also very interested in this becoming its own module.

I tried the patch on 7.x-3.0-alpha3 and while it had no side effects, I could not get it to work.

Is there suppose to be an option in the cache settings to get this to work? I just have content caching turned on in my view, with the proper node type selected and the cache times set to 6 hour max.

As it stands right now, what ever I flag the first time always gets returned back to me, no matter if I flag other items (with caching turned on).

Am I using the proper version of the mod? Should I be testing against DEV?

garamani’s picture

Issue summary: View changes
StatusFileSize
new71.15 KB

The patch doesn't work for me. I got this error:
patch error

jaydub’s picture

@garamani fwiw your error mentions a PHP fatal error for drupal_error_levels() which has nothing to do with this module or patch so possible that your issue is unrelated.

garamani’s picture

Thanks jaydub
I don't know what was the source of error; flushing the caches didn't help but after restarting the server, Ajax error has gone.

But now there's a new one:

Strict warning: Declaration of views_content_cache_key_flag::options_form() should be compatible with that of views_content_cache_key::options_form() in require_once() (line 3 of C:\Users\Garamani\Downloads\Compressed\apolo\sites\all\modules\views_content_cache\plugins\views_content_cache\flag.inc).

The flag.inc Codes:

<?php

class views_content_cache_key_flag extends views_content_cache_key {
  function options_form($value, &$handler) {
    // Get list of flags
    $options = array();
    $flags = flag_get_flags();

    foreach ($flags as $fid => $flag) {
      $description = '';
      $description = 'flag type: <em>'. $flag->entity_type .'</em>';
      if (($flag->entity_type == 'node' || $flag->entity_type == 'comment') && count($flag->types)) {
        $description .= '<br />node types: <em>'. implode(', ', $flag->types) .'</em>';
      }
      $options[$flag->fid] = $flag->title . '<br />'. $description;
    }
    natcasesort($options);

    return array(
      '#title' => t('Flags'),
      '#description' => t('Checks for flag and unflag actions for the selected flags.'),
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $value,
    );
  }

  function content_key($object, $object_type) {
    if ($object_type === 'flag') {
      return $object->fid;
    }
  }

  function clause_mode() {
    // We can't be combined with other cache segments:
    return 'OR';
  }
}
jaydub’s picture

@garamani try the small change referenced in #6 above and let me know if that gets rid of the warning.

elaman’s picture

The code in the patch is working, but patch needs to be updated.
Regarding the issues in #6, I think the separate issue in the module, that causes SQL errors when you write custom plugins. See #1185268: Nodequeue support (plugin) having the same issue.

glynster’s picture

Applying the patch and updating the inc manually and this solves the problem completely!

joegraduate’s picture

Related issues: +#2407467: views_content_cache_update_get() builds incorrect query
StatusFileSize
new2.61 KB
new3.51 KB

Attached is a re-rolled version of #4 that includes the changes recommended in #6 as well as some minor coding standards adjustments.

A separate patch for the SQL errors described in #6 and #12 is available here: #2407467: views_content_cache_update_get() builds incorrect query.

trackleft2’s picture

THis works for me perfectly

trackleft2’s picture

Status: Needs review » Reviewed & tested by the community

RTBC in my opinion