Hi All

I have a flag on an exiting site that needs to be enabled for all users. So far I have the following code:

$users = entity_load('user');

    if ($users) {
        foreach ($users as $user) {
                $uid = $user->uid;
                if (!$uid == 0) {
                    print $uid . "</br>";
                   flag('flag', 'daily_digest', $entity_id, $uid);
               }
        }
    }

The part I am missing is what to use for the entity_id in flag().

Is this the easiest way to set a flag for all users on the site?

Thanks in advance

Comments

joachim’s picture

> The part I am missing is what to use for the entity_id in flag().

Well what is it that you want to flag?
Your question makes it sound like the flag is actually on users. In which case the entity ID is the user ID. If instead your flag is on, say, nodes, then you want to give the ID of the node that you want everyone to flag.

mikejuic3’s picture

Hi Joachim, thanks for the response.

Its basically a flag that sets a subscription to a newsletter called daily_digest. Its an inherited site and has a custom module to generate this news letter.

I am back on this today so will let you know the results.

joachim’s picture

> Its basically a flag that sets a subscription to a newsletter called daily_digest

Using Flag for that seems crazy. There are several perfectly good modules for actually subscribing to newsletters!

mikejuic3’s picture

Unfortunately this is an inherited project and has many things happening which could have been done better.

mikejuic3’s picture

For anyone else who needs a quick way to set a flag for all users please see the code below. This code is setting a flag for the user entity rather than a node so the entity_id is the user id. For a node you would change the entity_id to be the node id.

<?php
$users = entity_load('user');
    if ($users) {
        foreach ($users as $user) {
                $uid = $user->uid;
                if (!$uid == 0) {
                   flag('flag', 'your_flag_name', $uid, $user);
               }
        }
    }
?>

Thanks joachim for the help.

joachim’s picture

Status: Active » Fixed

Thanks for sharing :)

Though:

$users = entity_load('user');

That's going to totally keel over your site if you have a large number of users...

mikejuic3’s picture

Not to worry, it's not in production yet although all the users from the old version of the site are now on dev/staging. It will only be run once fortunately.

Status: Fixed » Closed (fixed)

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