I want to place a flag link on a page, as suggested on that page: https://www.drupal.org/node/295383

The flag will subscribe the user to the most recent content page, which is done in Views.

I have the Flags: subscribe (by current user) Relationship.

I have installed Views PHP and I want to add to the header of the view a Global:PHP with the code <?php print flag_create_link('subscribe', $node->nid); ?>

Unfortunately I receive an error:

Notice: Undefined variable: node in __lambda_func() (line 1 of .../sites/all/modules/views_php/plugins/views/views_php_handler_area.inc(39) : runtime-created function). Notice: Trying to get property of non-object in __lambda_func() (line 1 of .../sites/all/modules/views_php/plugins/views/views_php_handler_area.inc(39) : runtime-created function).

What could be the reason for that?

Any help appreciated.

2 Answers 2


As I understand it, OOB it won't work because Flags require an entity to function, but Views pages are simple menu callbacks, not actual entities (such as nodes, users, comments, etc). So, in order to do this, you would need to trick it into think it is an entity:

  • create a content type, Dummy Page for this example

  • remove the body and restrict editing, deleting, etc to admin only

  • Create a node with this content, using the title you want for the View, Recent Content for this example, give it a path alias /recent

  • create the view as a block, and in the block UI, set it to show only on the recent page

  • Create a flag for Nodes, Favourites

  • create a page--recent.tpl.php file and override the $action_link for the page (or modify it drastically however you want), but include:

<?php print flag_create_link('favourites', $node->nid); ?>

Or use the built in flag link that comes with the Flag

  • then do whatever you were going to do originally with the flag. You can include it in other Views pages by creating new nodes of that content type and include the View as a block, etc.

This will trick the Flag into thinking the page is an entity and track it properly, as well as display it on the page wherever you decide to put it

  • Hi Geoff thank for your answer. Actually I want to put this in the Header of the View, not per item in the view.
    – user48533
    Commented Jun 30, 2015 at 14:50
  • @peer see my edit...
    – Geoff
    Commented Jul 2, 2015 at 1:21
  • Hi Geoff thank you for your update. Your suggestion prints the flag as supposed. The problem is that the flag is not working on the page /recent - I've tried both with my custom flag "Subscribe" and with the default flag "Bookmark". When I put both flags on another node page it works as expected...
    – user48533
    Commented Jul 2, 2015 at 10:59
  • I just ran a test and it works fine for me - you say it's not working, what isn't working?
    – Geoff
    Commented Jul 2, 2015 at 11:13
  • Hi Geoff. The idea is: The page "Recent content" has a flag "Subscribe" which sends an email to the user each time there is something new (new comments for example, or new content on the site). When user flags "Subscribe" emails are being send. I can flag the page al right. Bit there are no emails. Now, to crosscheck the flag, I've put it on a different content type, flag from another node, and in that case receive emails. I've tried with my "Subscribe" flag and with the default "Bookmark this" flag.
    – user48533
    Commented Jul 2, 2015 at 11:16

The error arises because there is no $node variable defined in the scope. Try looking inside the $results variable; something like this should work:

<?php print flag_create_link('subscribe', $results[0]->nid); ?>

This will print a subscribe link for the first row of the view. Also, you have to ensure that the nid is present in your result object. It should be already there, but if not, add the "Content: Nid" field and hide it by checking the "Exclude from display" option.

  • Hi, peterpoe, thanks for your reply. Your code is not printing anything. In the Global/PHP i have the following available variables $view: The view object. $handler: The handler object. $results: Array containing the view's result.
    – user48533
    Commented Jun 30, 2015 at 12:26
  • Hi, peterpoe, this did the trick, but it was a problem on my side, I have previously limited the flag to Panels only. Now its working ok. Thanks.
    – user48533
    Commented Jun 30, 2015 at 14:51
  • Now I encounter the problem that the content of the view changes because it is a Recent content - view of new or updated content. And when the first raw changes the flag is not activated.
    – user48533
    Commented Jul 1, 2015 at 13:07
  • Now I get it: I think you might have misunderstood the use of Flags. You can't flag a view, you can only flag entities, which may or may not be listed as results of a view. What is the intended effect of subscribing "the user to the most recent content page"?
    – peterpoe
    Commented Jul 2, 2015 at 13:39
  • Hi, peterpoe, check the discussion under Geoff's reply.
    – user48533
    Commented Jul 2, 2015 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.