1

I want to redirect users to a view when they flag a taxonomy term.

This question is about how to use Rules to redirect on flag save. I want to do this programmatically.

I tried to use hook_form_alter() to add a submit handler with the redirect. Unfortunately, while flaggings have an edit form that can be targeted by hook_form_alter() (MYMODULE_form_flagging_FLAGTYPE_edit_form_alter()), they do not seem to have a "create" form.

So how can I add a redirect programmatically upon flagging?

EDIT

Based on 4k4's comment I looked at /flag/srs/Entity/Flagging.php:

 *    "form" = {
 *      "add" = "Drupal\flag\Form\FlaggingForm",
 *      "edit" = "Drupal\flag\Form\FlaggingForm",
 *      "delete" = "Drupal\flag\Form\UnflagConfirmForm"

However, when I tried to do a hook_form_alter() with:

  • function MYMODULE_form_flagging_FLAGGINGTYPE_add_form_alter(&$form, FormStateInterface $form_state, $form_id)
  • function MYMODULE_form_flagging_add_form_alter(&$form, FormStateInterface $form_state, $form_id)

I could not get either to pick up my code (I rebuilt the cache).

3
  • 1
    Yes, this is the right approach, setting a redirect in a form submit handler and it's correct, flaggings don't have a "create" form, but they should have an "add" form by looking at the entity class.
    – 4uk4
    Commented Nov 7, 2020 at 10:43
  • @4k4 Thanks for the tip. I looked at the class and there is an "add" form, but I can't get it to hit with hook_form_alter() for some reason. (Question edited) Commented Nov 7, 2020 at 15:43
  • I find the naming scheme of that specific hook quite confusing, too. You can take a different approach using the general form alter hook and narrowing down the new entity step by step. I add it as answer.
    – 4uk4
    Commented Nov 7, 2020 at 16:14

2 Answers 2

1

You can take a different approach to target entity forms:

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;


/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(array &$form, FormStateInterface $form_state) {

  $form_object = $form_state->getFormObject();
  if (!($form_object instanceof ContentEntityFormInterface)) {
    return;
  }

  $entity = $form_object->getEntity();
  if (!(entity instanceof FooEntityInterface)) {
    return;
  }

  if (!$entity->isNew()) {
    return;
  }

  if ($entity->bundle() !== 'bar') {
    return;
  }

  // alter form
}
1

This can be done in hook_entity_presave().

function MYMODULE_flagging_presave(FlaggingInterface $flagging) {
  switch ($flagging->getFlagId()) {
    case 'FLAGTYPE':
      $redirect_url = Url::fromRoute('view.VIEW_MACHINE_NAME.VIEW_DISPLAY_ID')->toString();
      $response = new RedirectResponse($redirect_url);
      $response->send();
     break;
   }
 }

Your Answer

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

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