Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tripper54’s picture

Great idea, I'll look into this with a view to add it.

Patches welcome in the mean time!

ndf’s picture

Created a patch with 3 arguments.
I does its job, but it is not super beautifull.

I have tested it quite thoroughly but would like to know about improvement.

ndf’s picture

Status: Active » Needs review
tripper54’s picture

Great patch, @nielsdefeyter , thanks for contributing.

I wasn't comfortable with the fixed number of arguments in the form, so spent a bit of time trying to come up with a better solution,

After stuggling in vain with AJAX callbacks to determine the correct number of argument fields to display, I settled on this version, which offers one field for arguments and instructs users to split the arguments with a slash, similar to how the preview works in views UI.

What do you think of this approach?

tripper54’s picture

..and here's a version that doesn't break context processing.

ndf’s picture

I agree with you that those fixed arguments are not neat at all.

What I would prefer though (I have been playing around with it but couldn't do it yet) is really use ctools contexts.

This would be trivial if we would know what type of arguments our View needs.
Say the View needs an user-id and a node-id, we could then add:

  'required context' => array(
    new ctools_context_optional(t('Argument 1, User ID'), 'user'),
    new ctools_context_optional(t('Argument 2, Node ID'), 'node'),
  ),

to the plugin definition and in the settings form nice selects-boxes show up with the real available contexts.

My problem to accomplish that, is that we have to alter the plugin settings after the view has been selected.

2 possible methods:
1. Select a View, extract the View's arguments, add those arguments to the plugin definition.
2. Load the configuration form, load 'all available contexts', add those contexts to the plugin definition as optional. If there are 2 available contexts, you can select 2 arguments. If there are 4 available contexts, you can select 4 arguments.

How do you think about that?

ndf’s picture

Like this one from ctools/plugins/access/context_exists.inc

  'required context' => new ctools_context_required(t('Context'), 'any', TRUE),

And this variant with multiple optional contexts.

  'required context' => array(
    new ctools_context_optional(t('Context'), 'any', TRUE),
    new ctools_context_optional(t('Context'), 'any', TRUE),
  ),
ndf’s picture

Hi Phil,

Think we got a nice patch now!

What is changed since first patch:
- Arbitrary number of arguments.
--- Function 'views_get_view_result' is called with 'call_user_func_array' to make that possible.
- Select dropdowns for arguments selection instead of text fields.
--- Apparently available contexts are known in the settings form.
--- Its impossible now to pass arguments without a context (hardcoded argument). I think this is not a bad thing.
- Code cleanup
--- Moved views options list to '_ctools_view_access_get_views_options_list'.
--- Added arguments to plugin summary.
--- Code commenting, etc.

ndf’s picture

Hi Phil, what shall we do?

heivoll’s picture

Joining the issue here.

I've applied the patch from #8, and it works. However, I noticed that if you use it as a visibility rule on a Panels pane, there are no selectors for arguments.

Also, since the way of using the arguments now are by selecting contexts directly, it's not possible to pass e.g. a field value. That might limit the usability of it a bit. Or am I missing something here?

Sorry for joining in so late. But to be honest, I think the approach from #4 is better, as it gives you the option to use any context and any number of arguments.

ndf’s picture

Thanks for your review/feedback!

So the goal of this issue is the ability to pass arguments to a view.
Patch #2 had a fixed amount of arguments and an option for using a context or direct input. Context are recognized by a specific pattern (%node).
Patch #4, #5 has a flexible amount of arguments and both direct input and context. Context are recognized by a specific pattern (%node).
Patch #8 dropped the direct input and favors choosing a context with a select box.

My thought about dropping direct input is that it is more error-prone and user-friendly, because you don't have to remember/lookup available contexts but its just click and go. Feels more ctools/panels-alike.
I am not completely sure, but can't you add a context of type "String" and use its keyword as a substitute for direct input? I will check that within a couple of days.

As of 'visiblity rules'. That's not working, but it should, so could call that a bug.

tripper54’s picture

Status: Needs review » Needs work

Sorry for my lack of attention on this, hopefully I'll get some time soon to contribute a little more constructively :)

It seems to me that we either:

1. commit patch #5; or
2. work on #8 to address the issues heivoll raises.

TBH I'm OK with either approach. I agree with Niels that a 'click to to configure' option seems like a nicer way to go, as long as the functionality is all there.

I'll leave this with you, Niels, if you would like to keep polishing #8, otherwise let me know and I'll commit #5. I guess the UI could always be cleaned up later, it just adds a bit of fiddling with upgrades...

ndf’s picture

Assigned: Unassigned » ndf

I'll put some time on it this week and then I come back here about it.

heivoll’s picture

nielsdefeyter: Yeah, I guess you're right that it's more elegant and such, and if it can be made to work 100%, it's a very nice solution. It looked first as if it's not possible to use the String context approach, but it should probably be possible with a dev version or something of ctools, see this issue: https://www.drupal.org/node/1774434 . It does have the limit that it's not possible to use token replacements in the string, but that will probably be fixed with time (see the last comments in the issue).

ndf’s picture

Hereby the new patch.

@heivoll: the visibility rule setting is fixed with this patch now, but I think the reason it didn't work is a bug somewhere in panels. In the access-plugin definition "all contexts => TRUE" is used, but those contexts are not passed to the settings form. Those contexts are sideloaded now and therefore available.

The string-context is available but not configurable in ctools latest stable release (7.x-1.4). So therefore you need ctools 7.x-1.x-dev today.

I also killed watchdog notices/warnings by better checking for empty arrays/values.

@tripper, heivoll: would be great if you could give this patch another try!

ndf’s picture

Status: Needs work » Needs review
tripper54’s picture

Status: Needs review » Needs work
FileSize
2.64 KB

To test this, I created a simple view that takes a node ID as an argument, and lists all the nodes with that node id ;)

See attached view.

I then created a panel variant with a string context, name 'string' , value '41'.

I created a custom panel pane on this variant, some arbitary text "If you can see this, the view has returned true".

Added a visibility rule, "a view returns results".

Selected "ctools access test - default" as my view / display.

Now I'm presented with 3 argument dropdowns, even though the view only accepts one argument.

So which should I select? The first? OK, I select "string" as the first argument. Save and test. Panel is not visible.

Go back and try setting all three to 'string' . Nope, still doesn't work.

So two problems here, one I could not get this to work, and two it showed 3 dropdowns for argument although the view only needs one.

heivoll’s picture

I am getting the same problems as tripper54, except that when using it as a Visibility rule for a pane, i get two argument selectors, not three. Perhaps that could be because the View has two different arguments in total, but spread over different displays?

ndf’s picture

Hmm, I missed something obvious.

1. String context
The 'string' context is different that the other real objects like user, node, etc.

In the function _ctools_view_access_get_argument_from_contexts() that is checked now. Silly I did not test that properly.
This is totally separated from the input method via a panel-visibility- or selection-rule. It would happen to with direct input (i.e. %string). So I am a bit worried that there could be other unknown exceptions too.

2. Argument selectors
About the input method with the multiple selectors.
The thing what happens is that all available contexts will be presented here independent if the View can handle them. The reason is that we don't have a proper ajax-callback that loads the view when you selected it. But it sounds like a better, less confusing UI.

So at least you will see one context (that is "Logged in user") and if your are on the node-panel you will see 2 ("Node being viewed" & "Logged in user") that you can pass into the view. If you add a string-context, you'll see 3 argument selectors.

In addition to the patch file also added 'test_node_panel" that makes use of the "test_view".

So test setup:
panels 3.4 stable
ctools 7.x-1.x-dev
patched ctools_view_access
test_view.txt
test_node_panel.txt

Maybe this is getting a bit to difficult all the way, but I still hope we could manage to fix this feature.

tripper54’s picture

@niels,

The string argument works now, thanks.

The problem with the number of argument selectors remains.

So if I understand correctly, the number of selectors I'm presented with will depend on the number of contexts available eg, if I have 'node' , 'user' and 'string' , I'll get three selectors, regardless of the number of arguments the view requires.

This is no good, I'm afraid. The number of selectors needs to correspond with the number of arguments from required by the view. Otherwise it's too confusing.

As I mentioned above, I did try to get this working via ajax callbacks, but these Ctools forms aren't rendered by the core form API, so core-style AJAX doesn't work.

Another option that seems like a common pattern is to make the form multi-step: in step one we select the view, then in step 2 we choose the arguments (if any).

Have you explored that?

ndf’s picture

Yes, clear. That means work to do. I'll dive into it: both ajax and multistep.
Nice change to learn something new!

ndf’s picture

Status: Needs work » Needs review
FileSize
6.54 KB
6.67 KB

Tried a long shot on the ajax, but at gave up. Got to many issues with ctools+ajax.

So what I did was using patch #5 and then I added functionality from the later patches:

  • Use 'call_user_func_array' to pass Views arguments
  • Changed the description of 'Arguments': available contexts (their keywords) are visible now. So no dropdowns, but still some feedback to the user about what can be put in
  • Stub-context for string (although you can also fill in absolute values)

I tested:
Both 'selection rules' as 'visibility rules'.
Log notices

If we want to give feedback to the user what arguments a view can handle, we might do that by extending the view-instance label with arguments. The view object contains this information. But this functionality is not in this patch though.

tripper54’s picture

Status: Needs review » Fixed

Committed! Thanks @nielsdefeyter for your work on this.

ndf’s picture

Thanks!

Status: Fixed » Closed (fixed)

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