diff --git a/config/install/entity_notify.settings.yml b/config/install/entity_notify.settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..c75251105416356022121a19df64bc17d895f4c0 --- /dev/null +++ b/config/install/entity_notify.settings.yml @@ -0,0 +1,2 @@ +enable: true +ignore_paths: '' diff --git a/config/schema/entity_notify.schema.yml b/config/schema/entity_notify.schema.yml index 496718dc48708a79b7cbf0c45f7dcfa1be3ef83e..a8c9834dd98a7e7318e2bfd4f0d178738fcedcf4 100644 --- a/config/schema/entity_notify.schema.yml +++ b/config/schema/entity_notify.schema.yml @@ -5,6 +5,9 @@ entity_notify.settings: enable: label: 'Enable the notifications' type: boolean + ignore_paths: + label: 'Ignore some paths' + type: string enabled_target_entity_types: label: 'Target entity types' type: sequence diff --git a/entity_notify.install b/entity_notify.install index d4a938cd70ace16b630b6b4469e09f45cc382684..5c4e84b024fd0998bfbc62b21630fff6abcdefe1 100644 --- a/entity_notify.install +++ b/entity_notify.install @@ -84,3 +84,13 @@ function entity_notify_update_10002(): void { $config->set('enable', TRUE); $config->save(); } + +/** + * Add "ignore paths" config. + */ +function entity_notify_update_10003(): void { + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('entity_notify.settings'); + $config->set('ignore_paths', ''); + $config->save(); +} diff --git a/entity_notify.module b/entity_notify.module index a489ae0462c05c56ad414937476daaa68f92a4a5..f1540677bae31eabedbb0b697561fa59b42610e0 100644 --- a/entity_notify.module +++ b/entity_notify.module @@ -64,10 +64,18 @@ function _entity_notify_send_mail($to, $params) { */ function _entity_notify_event(EntityInterface $entity, string $event) { $config = \Drupal::config('entity_notify.settings'); - $entities = $config->get('enabled_target_entity_types'); $enable = $config->get('enable'); if ($enable) { + $entities = $config->get('enabled_target_entity_types'); + $ignore_paths = explode("\r\n", $config->get('ignore_paths')); + $request = \Drupal::service('request_stack')->getCurrentRequest(); + $current_path = $request->getRequestUri(); + + if (in_array($current_path, $ignore_paths, TRUE)) { + return; + } + if (($entity->getEntityTypeId() === 'node') || ($entity->getEntityTypeId() === 'comment') || ($entities && in_array($entity->getEntityTypeId(), $entities, TRUE))) { diff --git a/src/Form/EntityNotifySettingsForm.php b/src/Form/EntityNotifySettingsForm.php index 600d49cc89d97053c7c54925a700f82a4d8473d4..d802b1a7307f9f1d32587392e691014da042396c 100644 --- a/src/Form/EntityNotifySettingsForm.php +++ b/src/Form/EntityNotifySettingsForm.php @@ -79,6 +79,13 @@ class EntityNotifySettingsForm extends ConfigFormBase { '#default_value' => $config->get('enable'), ]; + $form['ignore_paths'] = [ + '#type' => 'textarea', + '#title' => t('Ignore some paths'), + '#default_value' => $config->get('ignore_paths'), + '#description' => t('Delimiter: new line. Example: /node/1/edit'), + ]; + $all_entity_types = $this->entityTypeManager->getDefinitions(); $content_entity_types = []; /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_type_options */ @@ -121,6 +128,7 @@ class EntityNotifySettingsForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('entity_notify.settings') ->set('enable', $form_state->getValue('enable')) + ->set('ignore_paths', $form_state->getValue('ignore_paths')) ->set('enabled_target_entity_types', array_values(array_filter($form_state->getValue('enabled_target_entity_types')['entity_types']))) ->set('entity_notify_admin', $form_state->getValue('entity_notify_admin')) ->set('entity_notify_roles', array_values(array_filter($form_state->getValue('entity_notify_roles')))) diff --git a/tests/src/Functional/EntityNotifyCommentNotificationsTest.php b/tests/src/Functional/EntityNotifyCommentNotificationsTest.php index 28fce4e48a8544467192e33e02b849e9698bcd8c..da8391b9301480ac88bc88518f87d7404688e6f8 100644 --- a/tests/src/Functional/EntityNotifyCommentNotificationsTest.php +++ b/tests/src/Functional/EntityNotifyCommentNotificationsTest.php @@ -66,10 +66,6 @@ class EntityNotifyCommentNotificationsTest extends BrowserTestBase { public function setUp(): void { parent::setUp(); - $config = $this->config('entity_notify.settings'); - $config->set('enable', TRUE); - $config->save(); - $node_type = 'article'; $this->createContentType(['type' => $node_type]); $this->addDefaultCommentField('node', $node_type); diff --git a/tests/src/Functional/EntityNotifyNodeNotificationsTest.php b/tests/src/Functional/EntityNotifyNodeNotificationsTest.php index 78403b46ff4d000026fa85d1e1765a3586c834ad..a92812cd9b7464deaa8e00d0e27e2074691d89a0 100644 --- a/tests/src/Functional/EntityNotifyNodeNotificationsTest.php +++ b/tests/src/Functional/EntityNotifyNodeNotificationsTest.php @@ -59,10 +59,6 @@ class EntityNotifyNodeNotificationsTest extends BrowserTestBase { public function setUp(): void { parent::setUp(); - $config = $this->config('entity_notify.settings'); - $config->set('enable', TRUE); - $config->save(); - $node_type = 'article'; $this->createContentType(['type' => $node_type]); $this->settingsRoute = Url::fromRoute('entity.node_type.edit_form', ['node_type' => $node_type]); @@ -73,6 +69,7 @@ class EntityNotifyNodeNotificationsTest extends BrowserTestBase { $this->adminUser = $this->DrupalCreateUser([ 'administer entity_notify configuration', 'administer content types', + "edit any $node_type content", ]); $this->user = $this->DrupalCreateUser(); @@ -241,4 +238,55 @@ class EntityNotifyNodeNotificationsTest extends BrowserTestBase { $this->assertCount(3, $captured_emails, 'Three emails were captured.'); } + /** + * Test ignored paths. + */ + public function testIgnoredPaths() { + $this->drupalLogin($this->adminUser); + $this->drupalGet($this->settingsRoute); + $this->assertSession()->statusCodeEquals(200); + + $edit = [ + 'entity_notify_enable' => '1', + 'entity_notify_admin' => '1', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('has been updated'); + + $captured_emails = $this->drupalGetMails(); + $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.'); + + $node = $this->createNode([ + 'type' => 'article', + ]); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(1, $captured_emails, 'One email was captured.'); + + $node->setTitle('New name')->save(); + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(2, $captured_emails, 'Two emails were captured.'); + + // Ignore some paths. + $this->drupalGet(Url::fromRoute('entity_notify.settings')); + $this->assertSession()->statusCodeEquals(200); + + $base_path = \Drupal::request()->getBasePath(); + $edit = [ + 'ignore_paths' => "$base_path/node/1/edit", + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('have been saved'); + + $this->drupalGet('node/1/edit'); + $edit = [ + 'title[0][value]' => 'New name', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('has been updated'); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(2, $captured_emails, 'Two emails were captured.'); + } + } diff --git a/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php b/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php index e89d14c0edd9dbc0df34e9deef100fbed4d87251..a4864391ab116bb79c44852f3d96dc84bba8c049 100644 --- a/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php +++ b/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php @@ -67,10 +67,6 @@ class EntityNotifyOtherEntitiesNotificationsTest extends BrowserTestBase { public function setUp(): void { parent::setUp(); - $config = $this->config('entity_notify.settings'); - $config->set('enable', TRUE); - $config->save(); - $this->settingsRoute = Url::fromRoute('entity_notify.settings'); $this->drupalCreateRole([], 'llamalovers', 'Llama Lovers'); diff --git a/tests/src/Functional/EntityNotifySettingsFormTest.php b/tests/src/Functional/EntityNotifySettingsFormTest.php index fb60a3f5c57a1f1b0c2d3892bc8e79d916e2f011..de6d641f21406bd337b92856b3a662371aeb697e 100644 --- a/tests/src/Functional/EntityNotifySettingsFormTest.php +++ b/tests/src/Functional/EntityNotifySettingsFormTest.php @@ -73,6 +73,7 @@ class EntityNotifySettingsFormTest extends BrowserTestBase { $telegram_chat_id = $this->randomMachineName(); $edit = [ 'enable' => '0', + 'ignore_paths' => '/test\r\n/bla-bla', 'enabled_target_entity_types[entity_types][taxonomy_term]' => 'taxonomy_term', 'entity_notify_admin' => '1', 'entity_notify_roles[llamalovers]' => 'llamalovers', @@ -88,6 +89,7 @@ class EntityNotifySettingsFormTest extends BrowserTestBase { ]; $expected_values = [ 'enable' => FALSE, + 'ignore_paths' => '/test\r\n/bla-bla', 'enabled_target_entity_types' => ['taxonomy_term'], 'entity_notify_admin' => TRUE, 'entity_notify_roles' => ['llamalovers', 'catcuddlers'],