diff --git a/flag.module b/flag.module index 60a0c8a..c0440d1 100644 --- a/flag.module +++ b/flag.module @@ -137,9 +137,9 @@ function flag_form_alter(&$form, FormStateInterface $form_state, $form_id) { $flag_service = \Drupal::service('flag'); $entity = $object->getEntity(); $flags = $flag_service->getFlags($entity->getEntityTypeId(), $entity->bundle(), \Drupal::currentUser()); - $filtered_flags = array_filter($flags, function(FlagInterface $flag) { + $filtered_flags = array_filter($flags, function(FlagInterface $flag) use ($object) { $plugin = $flag->getFlagTypePlugin(); - return $plugin instanceof EntityFlagType && $plugin->showOnForm(); + return $plugin instanceof EntityFlagType && $plugin->showOnForm() && $plugin->isAddEditForm($object->getOperation()); }); if (!empty($filtered_flags)) { $form['flag'] = array( diff --git a/src/Plugin/Flag/EntityFlagType.php b/src/Plugin/Flag/EntityFlagType.php index 442d55a..a5b368e 100644 --- a/src/Plugin/Flag/EntityFlagType.php +++ b/src/Plugin/Flag/EntityFlagType.php @@ -184,6 +184,19 @@ class EntityFlagType extends FlagTypeBase { } /** + * Determines if the given form operation is add or edit. + * + * @param string $operation + * The form operation. + * + * @return bool + * Returns TRUE if the operation is an add edit operation. + */ + public function isAddEditForm($operation) { + return in_array($operation, ['default', 'edit']); + } + + /** * Returns the show on contextual link setting. * * @return bool diff --git a/src/Plugin/Flag/UserFlagType.php b/src/Plugin/Flag/UserFlagType.php index bf7ad99..21cb7b7 100644 --- a/src/Plugin/Flag/UserFlagType.php +++ b/src/Plugin/Flag/UserFlagType.php @@ -96,6 +96,15 @@ class UserFlagType extends EntityFlagType { /** * {@inheritdoc} */ + public function isAddEditForm($operation) { + // The user profile form uses 'default' as the operation for editing, and + // 'register' for adding. + return in_array($operation, ['register', 'default']); + } + + /** + * {@inheritdoc} + */ public function actionAccess($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) { $access = parent::actionAccess($action, $flag, $account, $flaggable); diff --git a/src/Tests/FlagTestBase.php b/src/Tests/FlagTestBase.php index 9663c0d..f5c8178 100644 --- a/src/Tests/FlagTestBase.php +++ b/src/Tests/FlagTestBase.php @@ -62,7 +62,9 @@ abstract class FlagTestBase extends WebTestBase { 'administer node display', 'administer modules', 'administer nodes', + 'create ' . $this->nodeType . ' content', 'edit any ' . $this->nodeType . ' content', + 'delete any ' . $this->nodeType . ' content', ]); } diff --git a/src/Tests/ShowOnEntityFormTest.php b/src/Tests/ShowOnEntityFormTest.php index f2fb404..07e0856 100644 --- a/src/Tests/ShowOnEntityFormTest.php +++ b/src/Tests/ShowOnEntityFormTest.php @@ -65,6 +65,32 @@ class ShowOnEntityFormTest extends FlagTestBase { // Go back to the node edit page and check if the flag checkbox is updated. $this->drupalGet($node_edit_path); $this->assertNoFieldChecked($flag_checkbox_id, $this->t('The flag checkbox is unchecked on the entity form.')); + + // Verify link is on the add form. + $this->drupalGet('node/add/' . $this->nodeType); + $this->assertField($flag_checkbox_id, $this->t('The flag checkbox exists on the entity add form.')); + + // Tests flagging via the add form. + $edit = [ + 'title[0][value]' => $this->randomString(), + 'flag[' . $flag->id() . ']' => TRUE, + ]; + $this->drupalPostForm('node/add/' . $this->nodeType, $edit, $this->t('Save and publish')); + $node = $this->getNodeByTitle($edit['title[0][value]']); + $this->assertTrue($flag->isFlagged($node, $this->adminUser)); + + // Tests submitting a new node and not flagging. + $edit = [ + 'title[0][value]' => $this->randomString(), + 'flag[' . $flag->id() . ']' => FALSE, + ]; + $this->drupalPostForm('node/add/' . $this->nodeType, $edit, $this->t('Save and publish')); + $node = $this->getNodeByTitle($edit['title[0][value]']); + $this->assertFalse($flag->isFlagged($node, $this->adminUser)); + + // Form element should not appear on the delete form. + $this->drupalGet($node->toUrl('delete-form')); + $this->assertNoField($flag_checkbox_id); } }