Use isNew() to determine the message text when saving a group

Created on 3 November 2023, about 1 year ago

Problem/Motivation

When a group entity is saved, the group module uses $this->operation == 'edit' to determine if the group is being created (net-new) or being updated, for the purpose of displaying the message. https://git.drupalcode.org/project/group/-/blob/3.0.x/src/Entity/Form/GroupForm.php?ref_type=heads#L93

This is different from the core Node module that uses $this->entity->isNew() to determine if the node is being created (net-new) or being updated. https://git.drupalcode.org/project/drupal/-/blob/10.2.x/core/modules/node/src/NodeForm.php?ref_type=heads#L275

When using form displays to create different displays of the group entity form, the operation comes in as the form mode machine name, and not necessarily named operations like edit. This results in the Group module outputting the message "@type %title has been created.", instead of "@type %title has been updated.", when a group is updated.

Steps to reproduce

  • Current D10 and current Group versions
  • Create 2 form displays to the Group entity type at /admin/structure/display-modes/form; group and simple_edit for this example.
  • Register the form mode to use the GroupForm.
    function mymodule_entity_type_build(array &$entity_types) {
      $entity_types['group']->setFormClass('group', 'Drupal\group\Entity\Form\GroupForm');
      $entity_types['group']->setFormClass('simple_edit', 'Drupal\group\Entity\Form\GroupForm');
    }
  • Load the forms using a controller and entityTypeManager.
    $form_object = $this->entityTypeManager
      ->getFormObject('group', 'group')
      ->setEntity($group);
    //or
    $form_object = $this->entityTypeManager
      ->getFormObject('group', 'simple_edit')
      ->setEntity($group);
  • Observe the issue

Issue:

When editing an existing group, and saving, if the form mode uses a created form mode to display the form, the confirmation message always says "...group has been created" and not "...group has been edited".

Proposed resolution

For the purpose of displaying the message, use $group->isNew() to determine if the group is being created (net-new) or being updated.

Remaining tasks

Code update

User interface changes

None

API changes

None

Data model changes

None

✨ Feature request
Status

Active

Version

3.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States tregonia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024