On the flag admin edit page, hook_flag_options_alter() gets invoked about a dozen times. Similarly on a node page.

This appears to be twice per flag, even if the flag is not currently relevant (eg on the node page I get a commerce product flag being loaded).

The first time, the flag object is mostly empty, the second time it's loaded.

Comments

joachim’s picture

OK, so every flag being loaded up is apparently by design in flag_get_flags():

  // Retrieve a list of all flags, regardless of the parameters.
  if (!isset($flags) || $reset) {
    $flags = array();

Smarter minds than me have worked on flag performance optimization -- this is because any call to this can't know how many other times it'll be called to get other types of flag, so it's best to hit the DB once and hard than lots and little.

Now for the being called twice:

In factory_by_row(), it seems both of these end up invoking the hook:

    $flag = flag_create_handler($row->content_type);
...
    $options += $flag->options();

which is because the flag_create_handler() function also calls:

    $options = $this->options();

I don't know the code for building a flag object well enough to figure this out. Anyone else is welcome to take a look and have a go at figuring it out :)

joachim’s picture

Title: hook_flag_options_alter() is invoked lots of times per flag » $flag->options() called multiple times per flag
Version: 7.x-2.x-dev » 7.x-3.x-dev

Upping to 3.x.

I've added a lot of docs about flag creation since filing this.

The duplication of $flag->options() is the thing to look into here.

ivnish’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

Closed as outdated because Drupal 7 is EOL