Comments

joachim created an issue. See original summary.

joachim’s picture

Title: Problem with entity delete forms » Flag form element shows on entity delete forms
Issue summary: View changes
joachim’s picture

Priority: Critical » Major
joachim’s picture

function flag_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $object = $form_state->getFormObject();
  if (!($object instanceof ContentEntityFormInterface)) {
    return;
  }

This is the problem -- it's far too general. An entity type could have loads of forms with this interface. We need to check it's specifically the edit form.

joachim’s picture

Status: Active » Needs review
StatusFileSize
new537 bytes

Patch. Needs tests, but that can be done in #2760479: Missing tests for flags on entity form.

martin107’s picture

Status: Needs review » Reviewed & tested by the community

1) Manual testing ...

- OMG yep bug is real!
- Patch fixes bug

2) I think the new return early condition is comprehensive. and I'm happy that if I am wrong we can always adapt later.

3) Can't see any unwanted side effects.

+1 from me

berdir’s picture

There is no guarantee about a form mode being called edit. Might need to be configurable just like the view modes are configurable for some settings.

joachim’s picture

That's not a form view mode, is it? It's from this bit of the entity annotation:

 *     "form" = {
 *       "default" = "Drupal\node\NodeForm",
 *       "delete" = "Drupal\node\Form\NodeDeleteForm",
 *       "edit" = "Drupal\node\NodeForm"
 *     },
berdir’s picture

Form modes and form operations more or less have a 1:1 relationship.

Just look at user as an example:

 *     "form" = {
 *       "default" = "Drupal\user\ProfileForm",
 *       "cancel" = "Drupal\user\Form\UserCancelForm",
 *       "register" = "Drupal\user\RegisterForm"
 *     },

There, default is basically edit.

joachim’s picture

Status: Reviewed & tested by the community » Needs work

I was a bit confused because when I looked at my D8 site, nodes have only 1 form mode and users have 2.

socketwench’s picture

I'm really starting to not be a fan of non-field display methods...

Can we add a description to the "Display checkbox on entity edit form" checkbox, specifying that this will only work on entities with an "edit" form operation?

Alternatively, we can make this option node only, where we can be sure it will work as expected.

joachim’s picture

+++ b/flag.module
@@ -129,7 +129,7 @@ function flag_help($route_name, RouteMatchInterface $route_match) {
-  if (!($object instanceof ContentEntityFormInterface)) {

How about instead of bailing if the operation isn't 'edit', we bail if it *is* 'delete'?

berdir’s picture

What about checking if the form is a not an instance of ConfirmFormInterface? that works for delete and also other confirmation forms which some entity types have, e.g. payments can be refunded and that has a confirm form too.

joachim’s picture

Priority: Major » Normal
Issue tags: +Release blocker

Downgrading, as it doesn't break anything, but setting to blocker as it does look pretty rubbish.

rbayliss’s picture

Status: Needs work » Needs review
StatusFileSize
new1.77 KB

How about letting the FlagType specify what operations it should show on? Something like this.

berdir’s picture

Just looking at core entities:

default: terms, comment, aggregator feeds, contact message.

edit: node, block_content.

That doesn't scale.

My suggestion in #13 is IMHO trivial and should work fine.

joachim’s picture

Status: Needs review » Needs work

Yup, let's go with #13.

jhedstrom’s picture

Assigned: Unassigned » jhedstrom

Just ran into this, working on implementing #13.

jhedstrom’s picture

Assigned: jhedstrom » Unassigned
Status: Needs work » Needs review
StatusFileSize
new888 bytes

This implements the suggestion of checking for the confirm form interface.

jhedstrom’s picture

With this approach, the flags still appear on the book module's 'outline' tab. Seems odd to hardcode an exception for the book module, but I'm not sure how else to approach this...

joachim’s picture

Urgh.

There's no way we can know what the actual 'edit' form is, is there?

We could say that we show the flag stuff only on both 'default' and 'edit', since that's what core uses.

(And file core bugs for D9 to standardize this stuff!!!!)

joachim’s picture

Status: Needs review » Needs work

Ok here's a plan:

+++ b/flag.module
@@ -130,7 +131,7 @@ function flag_help($route_name, RouteMatchInterface $route_match) {
-  if (!($object instanceof ContentEntityFormInterface)) {
+  if (!($object instanceof ContentEntityFormInterface) || ($object instanceof ConfirmFormInterface)) {

Let's move this logic, simple as it is, to the plugin. A new method receives the form operation machine name, and returns whether the flag form element should show.

The base entity plugin says yes to the 'edit' operation, as it sounds like that's what core uses for most entities.

The user entity plugin says yes to the 'default' operation, because the user entity is being stupid.

Any custom entity types that wants something else can create a plugin and override the method.

berdir’s picture

Form modes are extensible, book.module is a good example that is added to node by another module. In that case, you wouldn't want to have it show up, but I expect that people are adding custom form modes to edit things in different places in different ways, we do that with users for example and then you might want to have it show up.

I'm wondering if we can do something based on the form display, but since that book form extends from ContentEntityForm, it might actually have a display and just not call it.

joachim’s picture

Yup, I recently made a custom entity that didn't use the 'edit' form mode at all, but two custom ones ('step1' and 'step2').

An alternative to my suggestion in #22 is that we add the same settings that we do for display modes in the flag settings.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new4.34 KB
new2.1 KB
new4.97 KB

Here's an attempt to implement the approach from #22. The alternative suggestion from #24 may also make sense.

Note that even though in both 7 and 8 the config setting text for 'show_on_form' only mentions the edit form, as things are now in 7 (as well as 8) this setting also adds the element to the add form--and it works as-is too. Given this, the new method that I added checks for both add and edit operations.

I've included a test to demonstrate the current behavior. Note the only fail in the test-only patch is that the flag appears on the delete form--the add form already works.

Given this, we might also want to update the admin help text to indicate the element will appear on add and edit forms.

The last submitted patch, 25: 2759671-25-TEST-ONLY.patch, failed testing.

The last submitted patch, 25: 2759671-25-TEST-ONLY.patch, failed testing.

jhedstrom’s picture

StatusFileSize
new4.9 KB

This is a rebase of #25.

Status: Needs review » Needs work

The last submitted patch, 28: 2759671-28.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new687 bytes
new4.98 KB

Oops, re-roll missed adding a use statement to the closure.

socketwench’s picture

Status: Needs review » Reviewed & tested by the community

Not finding anything than formatting nits, so I think this is ready to go!

  • socketwench committed c5432a1 on 8.x-4.x authored by jhedstrom
    Issue #2759671 by jhedstrom, joachim, rbayliss: Fixed flag form element...
socketwench’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

joachim’s picture

I'm going to file a 5.x issue to consider this, which ultimately might be the more flexible way to go:

> An alternative to my suggestion in #22 is that we add the same settings that we do for display modes in the flag settings.

socketwench’s picture

I'm going to file a 5.x issue to consider this

Sounds like a good idea. Maybe we should start tagging 5.x issues?

joachim’s picture

Already done :)

Status: Fixed » Closed (fixed)

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