3

So I've been trying to implement an anonymous "wish list" for an eCommerce site using webform_select_options and Flag. My implementation uses an anonymous flag set on my products and uses webform_hook_select to add a select list of the flagged products to a webform.

The issue I have is that the webform_select_options only show up in the webform when the user's logged in, thus failing as an anonymous implementation.

UPDATE (number 2 8/12/15)

I can confirm that the items are flagged when an anonymous user flags them. I tested this with a block displaying the flagged items when the site was viewed in incognito mode, the list is modifiable (I can add and remove flagged items), so it behaves more or less the way I expect it to.

The view shows commerce products that have been flagged by the current user. This shows me that the items are flagged. I have tested this with multiple browsers in incognito mode and can confirm that the lists are different for different anonymous users.

I tried the dpm($view->result); and it looks like the function doesn't get called with anonymous users. The function prints results with an authenticated user, but no result with anonymous users. I adjusted anonymous permissions to see devel information and can confirm that the function is called with anonymous viewers but returns an array with 0 objects.

Here is the code I'm using, the flag is defined through the flag module UI.

<?php
 function webform_hook_webform_select_options_info() {
   $items = array();
   if (function_exists('webform_hook_user_flag_sample_requests')) {
     $items['webform_hook_user_flag_sample_requests'] = array(
      'title' => t("Sample Requests (User Flag list)"),
      'options callback' => 'webform_hook_user_flag_sample_requests',
     );
   }
   return $items;
 }
 function webform_hook_user_flag_sample_requests() {
       $view = views_get_view('webform_flag_views', TRUE);
       $view->set_display('webform_options');
       $view->execute();
dpm($view->result);
       $options = array();
       if ($view->result) {
         foreach($view->result as $item) {
        $options[$item->product_id]= $item->commerce_product_title . render($item->field_field_image[0]['rendered']);
          }
           }
       else {
         $options[0] = t("Nothing");
       }
    return $options;
          } 

So, I am at a loss at to what is wrong here, or how to approach this problem. I followed the tutorial here http://drupalsteve.com/node/57, but there doesn't seem to be an obvious way to resolve this.

I now strongly suspect that this is related to the general issue of showing a view to an anonymous user. Normally, you'd mitigate this by disabling SQL rewriting. Now, I wonder if my flags aren't showing up because I haven't programmaticaly set the advanced options of the "view" being created in the script.

So, I can think of two options: 1) create the flag array without generating a view; or 2) set the advanced query setting to disable sql rewriting.

thanks in advance.

  • I know how it sounds but... Have you cleared your caches? – Mołot Aug 7 '15 at 20:51
  • Yeah, I habitually flush the caches. I double checked to humor your response, too. No dice. – nizz0k Aug 7 '15 at 22:55
  • Do you know if your view return a result when you view as an anonymous user or is that an assumption? (dpm($view->result) may help?) – Darvanen Aug 10 '15 at 6:11
  • I have a separate block view which was meant to test this. The view shows commerce products that have been flagged by the current user. this separate block view shows me that the items are flagged. I have tested this with multiple browsers in incognito mode and can confirm that the lists are different for different anonymous users. – nizz0k Aug 10 '15 at 19:56
2

So, after some frustration and headbanging, I was led to the answer by someone on #drupal-support IRC. The solution was in fact related to the SQL rewriting. The view was editable through the UI. There under the advanced section, I selected 'diasable sql rewriting.' This ultimately solved the problem.

0

Don't know what was the problem. But first time I used view block and saw only authenticated user's flags. Now I'm using view page and can see admin's flags under admin and anonymouse flags under anon. Just copypasted the code from your link and it works.

I can't figure out why did you name your functions in that way. Did you put your functions in webform_hook.module?

Anonymouse flags

Admin's flags Sorry, can't post comments.

  • Hi, I'm using a custom module calling the webform_select_options_info() function which just lets me add a select list created programmaticaly to my webform (my code in the post is the entire module, which yes I called webform_hook for semantic reasons). I did this to leverage webform's capabilities (I wanted basic client info, DB stored results, and auto-generated emails). I suspect that this has to do with some of the advanced views settings. Often I a view will not be visible to anonymous users unless you disable SQL rewriting. I'm not sure how to do this in code. – nizz0k Aug 9 '15 at 7:37
  • I tried to made it on clean distribution of Drupal 7 and it works as designed. Created view page and added flag relationship. If you want we can discuss it via skype, just use my current nickname – ar7max Aug 9 '15 at 7:59
  • And another question - why there are 2 implementations of hook in your module? – ar7max Aug 9 '15 at 16:08
  • hi ar7max, sorry I slipped and posted the wrong code from my module. I've updated the post. When are you available to chat? – nizz0k Aug 10 '15 at 20:17
  • My laptop is broken, so in a few days I can use only text future of Skype on android. My timezone is gmt +5. Available from 8am to 9pm. – ar7max Aug 10 '15 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.