I created a simple Selection Rule that allows you to compare CCK Fields to a configured value.
This allows you to have different Variants based on a Field value, or for Visibility Rules for individual panes.
The project I built it for uses it to let Authors decide (Using a CCK Field) what to show on that Node's Teaser (using panels_teasers.module). Could be used to let Author's choose different layouts, show additional panes based on field selections, etc.
Due to the flexible nature of the actual SQL fields CCK used (*.nid, *.value, *.fid/*.list for files), I decided to add every "column" to the field selector list. This way, you can even compare the *.format of fields.
I had to change the hook_ctools_plugin function to include more than just the "content_types" folder. It now detects if the "panels/$plugin" folder exists, if it does, return the dir.
CVS didn't include the new plugin file in the patch, so I zipped up the patch to content.module and the plugin, which must go in the ./includes/panels/access folder.
Comment | File | Size | Author |
---|---|---|---|
#35 | Capture.PNG | 181.11 KB | Vote_Sizing_Steve |
#30 | cck-798180-30.patch | 6.13 KB | loganfsmyth |
#8 | cck-798180.patch | 808 bytes | Jon Pugh |
#8 | cck-798180-plugin.patch | 5.28 KB | Jon Pugh |
#2 | fieldvalue.inc_.zip | 3.14 KB | stevector |
Comments
Comment #1
davidwhthomas CreditAttribution: davidwhthomas commentedIf you'd rather not use a module for this, you can use a PHP code in the Selection Rule for the Variant.
This for a node_view panel with multiple variants, to select based on that node's cck field value.
e.g:
Note: don't include the php
tags in the snippet.
hth,
DT
P.S It would be nice if Panels supported this natively, but I don't like the idea of patching other modules ( e.g CCK ) to get it to work.
Comment #2
stevectorI also started to tackle this functionality about a week ago and went about it in a slightly different way. I didn't realize someone was already working on it. I've posted the code I have so far. This should also be dropped into CCK's /includes/panels/access after applying careernerd's change to the hook_ctools_plugin function.
As I conceptualized such a plugin, I wanted a two-step configuration process. At the first step a user would select which field is being used. At the second step the user would get the CCK widget appropriate to the field as a way of setting the value.
This approach was modeled after the way ctools does the Views content_type. On the first screen, the admin sets which Views display to use. On the second screen, the display is configured.
However, I was not able to replicate this two-step process. I think I'd need assistance from a CTools expert. In the attached file, you'll see some commented out code that attempts to do this.
Instead, this plugin is currently hard-coded to work with a field called field_public. It's a CCK text field that has the radio button options of
0|Private
1|Public
This plugin right now is tested with single-value text fields. Getting support for node refs and any other CCK type would be a small code improvement. Also commented out in this file is code starts to support multivalue fields.
davidwhthomas, I agree that I'd like ctools to support this natively. However, the precedent has been set with CCK supplying it's own content_type plugin. I think it's then appropriate for CCK to supply an access plugin.
careernerd, anyway we can combine our efforts? What if we expanded upon your plugin so that users selected which field to use on one form and then on the next, had the option of using the CCK widget for exact values or an open field for regex?
Comment #3
Jon Pugh@davidwhthomas: I'm aware of the PHP selection rule, it's just that this is the whole point of CTools, so you can make simple plugins to make the logic common to many projects easy to configure. I'm not adding this patch for users, I'm hoping to get this into CCK itself.
Also, CTools in fact, should not contain this functionality natively because it is only used with CCK. I discussed this with merlinofchaos at DrupalConSF, since CCK already has the ctools_plugins hook, it is very easy to drop this into the next version of the module.
@stevector: The two step process sounds ideal. I built this plugin to have one step for simplicity, I haven't yet tried to make it two step. Also, I've tried to pull out individual CCK widgets as form items into other modules before and it was a real pain, so I'm not sure its worth the extra work. There are also additional challenges due to the flexible nature of the data column names.
My plugin already scans all values for multiple value fields, if one of them returns true, the selection rule passes.
I agree it's worth a shot, especially thinking about D7, I just won't have the time to work on this until another project pops up that needs this functionality.
Lets take my patch and build from there. I wrote it to be ready for committing, following coding standards and keeping it generic. I don't yet know how to make multipage forms with CTools, but I want to find out.
Comment #4
stevectorI think you're right careernerd. Your patch should go in because it works and is ready to go as is.
If in the future we want this to be a two-step plugin, I tried to dissect ctools/views_content/plugins/content_types/views.inc to replicate its process but was unsuccessful.
Comment #5
lord_of_freaks CreditAttribution: lord_of_freaks commentedsubscribing
Comment #6
that0n3guy CreditAttribution: that0n3guy commentedsub...
Comment #7
markus_petrux CreditAttribution: markus_petrux commentedLooking at the code, it seems that there are several snippets commented. These need to be removed. Also, the code does not follow coding standards, and we would need a patch. It's hard to review the code further.
Comment #8
Jon PughNo problem re: coding standards... I hadn't cleaned it up yet, I wanted to get it up so people can test it out.
Here's a patch for the hook_ctools_plugins_directory and a separate one for adding the new plugin file. I'm not fully versed in CVS, so it was a little challenging to add a new directory and file, but I think the patch is proper. instructions for this aren't perfectly clear... http://drupal.org/node/550576
Comment #9
Summit CreditAttribution: Summit commentedSubscribing, greetings, Martijn
Comment #10
Remon CreditAttribution: Remon commented+1
Comment #11
stevector@careernerd, In http://drupal.org/node/798180#comment-2987442 you mention scanning multiple fields but this code is only considering delta 0.
Did you just mean that in configuration of this plugin, multivalue fields could be selected?
$node_field = $node->$field_name;
$field_value = strval($node_field[0][$field_key]);
if (empty($conf['case'])) {
$value = drupal_strtolower($value);
$field_value = drupal_strtolower($field_value);
}
switch ($conf['operator']) {
case '=':
return $value === $field_value;
case '!=':
return $value !== $field_value;
case 'regex':
return preg_match($value, $field_value);
case '!regex':
return !preg_match($value, $field_value);
}
Comment #12
merlinofchaos CreditAttribution: merlinofchaos commentedI never do this because I hate to, but this one is important enough, so this is me subscribing to the issue.
Comment #13
merlinofchaos CreditAttribution: merlinofchaos commentedIf there are ways I can enhance CTools to make this easier I'm very interested in doing so.
One thing I can think of that this might benefit from are wizard forms for access plugins (i.e, select a field in step 1 and in step 2, configure the test against that field).
However I'm not sure that's necessary, just useful. Still, implementing wizard forms for access plugins should be doable.
Comment #14
Lloyd CreditAttribution: Lloyd commentedI applied the patches to display a variant if a CCK select list was of a specific value.
So, Content: Field Comparison, Node Being Viewed, and I selected the correct CCK field to equal "Info Both" (without quotes).
I get the following error:
warning: array_filter() [function.array-filter]: The first argument should be an array in /var/www/vhosts/provada.com/httpdocs/sites/all/modules/cck/includes/panels/access/content_field_compare.inc on line 132.
Comment #15
KarenS CreditAttribution: KarenS commentedI'm trying to understand this patch and its purpose. I'm not really clear on what this will accomplish, but when it comes to panels integration I'm happy to defer to merlinofchaos about what would be useful.
One thing that may not work as intended is if you have shared fields on different content types where you won't discover all the fields for all the types.
You use '$fields = content_fields()' to grab the available fields, but that function will not give you all the instances of shared fields. Instead you should do something like:
That would give you all fields on all types. If you know what type you want to limit it to, you can do:
Comment #16
stevector@merlinofchaos, adding wizard support to CTools to get the functionality you describe in #13 would be very helpful. Should a separate feature request be opened in CTools for that?
@KarenS, I'm using this patch to switch show/hide panes based on the value of a CCK field.
Comment #17
merlinofchaos CreditAttribution: merlinofchaos commentedYes, an issue for CTools for that is a good idea. Panels would probably need an update to its implementation of the access plugins as well.
Comment #18
merlinofchaos CreditAttribution: merlinofchaos commentedThis patch, when complete and ready to go, would allow users to control the visibility of panel panes, variants and pages based upon the values of CCK fields.
One incredible use case that I really want to be able to put as a standard recipe: Let's say I have a node type 'article'. Articles can be laid out in various ways, and I do not want my content managers worrying about creating layouts or tweaking them. Instead, I want to provide a dozen or so layouts.
In the Panels node view, I can provide all of those layouts and set up the nodes so that when the data is filled in, each one of them will work. To select which layout a node is, I would put a CCK field, 'Layout' as a textfield with a select box. It would be prepopulated with the layouts I want. Then the selection criteria on the variant for that layout would ask "Is the layout field equal to the name of this layout?" If yes, use it.
Comment #19
stevectorI have created a feature request in CTools for wizards on access plugins. http://drupal.org/node/876722
The use case described in #18 is exactly the kind of functionality I want to provide. Thanks.
Comment #20
Jon Pugh@karens: In this case I don't believe detecting the field instance is necessary. Panels handles the node context, and this plugin just checks to see if the field exists and matches the value entered. If the field doesn't exist in that type, the compare will just return false.
However...
@merlinofchaos: Wizards in access control makes sense to me. As far as I can see, "Selection rules" are the future of display logic. There's way to much room for error when you use custom PHP code to determine if something should show or not.
In the case of this plugin, the wizard might not be necessary if we can detect what node types are allowed, kindof like how in the panel content editor, the Field panes only show up for the nodetypes that may be available based on the selection rules... not sure how that would be accomplished, When I get a chance I'll dig through the code.
Comment #21
merlinofchaos CreditAttribution: merlinofchaos commentedYes you can detect what node types are allowed, but there will be instances when you have no idea what type it will be so you have to account for that situation.
That said I believe you want to look at $context->restrictions -- and these restrictions can be applied by previous access rules. Sadly this is not documented anywhere, so the only thing you can do is search for the word 'restrictions' and see how it gets applied.
Comment #22
Lloyd CreditAttribution: Lloyd commentedAny clue how the warning message below can be fixed?
warning: array_filter() [function.array-filter]: The first argument should be an array in /var/www/vhosts/provada.com/httpdocs/sites/all/modules/cck/includes/panels/access/content_field_compare.inc on line 132.
Comment #23
xtfer CreditAttribution: xtfer commentedsubscribing
Comment #24
Lloyd CreditAttribution: Lloyd commentedHas anybody been able to confirm if the patch in #8 works? I still haven't been able to get it running successfully.
Comment #25
abulte CreditAttribution: abulte commentedI'm very interested in this feature too, and I think it's a pretty common use case.
I also strongly agree with that, this is where we should be headed :
Comment #26
Lloyd CreditAttribution: Lloyd commentedAny update with this. The current patch is still unusable for me as it results in errors. Would love to see this happen.
Comment #27
datawench CreditAttribution: datawench commentedsnippet at #1 is extremely helpful. subscribing to keep track of the plugin.
Comment #28
drupalok CreditAttribution: drupalok commentedsubscribing
Comment #29
rickvug CreditAttribution: rickvug commentedThis is a very common use case. Subscribing.
Comment #30
loganfsmyth CreditAttribution: loganfsmyth commentedHello people,
This is definitely a feature that'll be useful, so we should try to get it done with. Having it be multi-step would be awesome, but for now we need to get a solution in there, and add more once access plugins can have a wizard options too.
I have reworked the patches from #8 a bit and fixed the array_filter error message that people mentioned. I also removed the negation of each operation because there is already an automatic negation options added by ctools. Finally, I also added a 'delta' option so that you can compare against a particular delta in a CCK field if you want to.
Thoughts?
Comment #31
Lloyd CreditAttribution: Lloyd commentedShould the patch in #30 be in addition to the plugin patch in #8? Or is the #30 patch alone sufficient?
Comment #32
loganfsmyth CreditAttribution: loganfsmyth commentedJust #30 is needed. I took the code from #8 and reworked it a bit and fixed a few issues.
Comment #33
pedrospI can't figure out how to deal with this patch, I've tried patching 6.x-2.9 and today's dev with no fun so far.
As far I understand the code I expect to see some additional options on the Panels Visibility Rules, and/or Selection Rules named : Content: Field Comparison
But nothing new :(
Any tips please ?
Comment #34
loganfsmyth CreditAttribution: loganfsmyth commentedI haven't tested it since I posted it, but maybe make sure you've cleared your caches? Ctools will been to process all of it's plugin hooks again, since it caches all of that.
Comment #35
Vote_Sizing_Steve CreditAttribution: Vote_Sizing_Steve commentedI'm having the same problem as #34 - after running update, and clearing caches.
See attachment for screenshot.
Comment #36
Lloyd CreditAttribution: Lloyd commentedAny progress on perhaps getting this committed? And the patch is no longer working for me.
Comment #37
stevectorI have an issue started for this is Drupal 7: #1299838: Field Comparison Access Plugin
Comment #38
stevectorI have an issue started for this is Drupal 7: #1299838: Field Comparison Access Plugin
Comment #39
stevectorThe Drupal 7 version of this functionality has been committed. #1300476: Add Access Plugin for Entity Field Value