As the title states, in flag.api.php there is a hook documented hook_flag_options_alter that is never applied by the module.

Attached is a patch that applies this hook on hook_ENTITY_TYPE_load, and adjustments are made to FlagTypeBase::submitConfigurationForm() to automatically save any data that is expected by the configuration array.

Not sure this is the desired approach, but I needed this documented functionality. Any feedback is appreciated.

CommentFileSizeAuthor
flag-options-alter.patch1.39 KBdaggerhart

Comments

daggerhart created an issue. See original summary.

joachim’s picture

Status: Needs review » Postponed (maintainer needs more info)
Parent issue: » #2409859: [Policy, No Patch] Where did the Flag 7.x hooks go in 8.x?

In the summary at #2409859: [Policy, No Patch] Where did the Flag 7.x hooks go in 8.x? is suggests this should be doable with a custom flag type plugin.

What's your use case?

daggerhart’s picture

I have some Flagging/Unflagging event subscribers that need to respond to some-but-not-all flaggings. So I thought it would be great if I could provide a checkbox on the Edit Flag page to easily inform the event whether or not the flag should be responded to.

With the included patch applied to flag, I am doing this is my custom module to add my new setting to the flag form:


/**
 * Add custom flag settings.
 */
function my_module_flag_options_alter(array &$options, FlagInterface $flag) {
  $options['my_module_track_changes'] = FALSE;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function epi_notes_form_flag_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /** @var \Drupal\flag\Entity\Flag $flag */
  $flag = $form['#flag'];
  $flag_type_config = $flag->get('flagTypeConfig');

  $form['my_module_track_changes'] = [
    '#type' => 'checkbox',
    '#title' => 'Track changes in flag state to admin notes.',
    '#weight' => -2,
    '#default_value' => $flag_type_config['my_module_track_changes'] ?: 0,
  ];
}

Then in my event subscriber, I'm looking for that setting to determine if the event should handle the given flag:

class FlaggingEventSubscriber implements EventSubscriberInterface {

  static function getSubscribedEvents() {
    $events = [
      FlagEvents::ENTITY_FLAGGED => 'entityFlagged',
    ];
    return $events;
  }

  function entityFlagged(FlaggingEvent $event) {
    $flagging = $event->getFlagging();

    // Only execute the event if this flag wants changes tracked
    $flag = $flagging->getFlag();
    $flag_type_config = $flag->get('flagTypeConfig');
    $execute = !empty($flag_type_config['my_module_track_changes']) ? $flag_type_config['my_module_track_changes'] : FALSE;

    if (!$execute) {
      return;
    }

    // ... more stuff
  }
}

This is much more ideal than checking the flag names within the event subscriber.

joachim’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

Ok so you're not supposed to add extra stuff into a plugin's configuration array, or a config entity's properties for that matter:

  $flag_type_config = $flag->get('flagTypeConfig');

You should be using set/getThirdPartySettings() and adding a submit handler in your form alteration to save it.

  $options['my_module_track_changes'] = FALSE;

You don't need this. Admittedly, then you don't have the default value of your setting defined anywhere, but that is a flaw in core, not here.