Problem/Motivation

We should not have functions that query the subscription table, as this is considered an implementation detail of the storage.

Proposed resolution

Add a custom SubscriptionStorage + Interface, that subsclasses \Drupal\Core\Entity\Sql\SqlContentEntityStorage, specify that as storage handler in Subscriber.

Move the function to a method there, and call it like this \Drupal::entityManager()->getStorage('simplenews_subscriber')->deleteSubscriptions().

See UserStorage::deleteRoleReferences() and #2390467: User role permission assignments are not deleted when a role is deleted as reference.

A quick grep shows that simplenews_count_subscriptions() is similar, but we can probably do that using an entity aggregate query in a storage agnostic way, which is even better.

Remaining tasks

@todo complete problem description and solution.

User interface changes

API changes

Comments

berdir’s picture

Title: todo.txt: simplenews_subscription_delete: redo in a proper way » Move simplenews_subscription_delete to SubscriptionStorage
Issue summary: View changes

Done.

berdir’s picture

Issue tags: +Novice
johnchque’s picture

I have created the SubscriptionStorage file and its Interface to for deleting subscriptions, seems it works. I attach the patch.

johnchque’s picture

Status: Active » Needs review
berdir’s picture

Status: Needs review » Needs work

Thanks, review below.

  1. +++ b/simplenews_rules/simplenews_rules.rules.inc
    @@ -225,7 +225,7 @@ function simplenews_rules_action_unsubscribe($args, $settings) {
       if (!empty($args['mail']) && !empty($args['tid'])){
    -    \Drupal::service('simplenews.subscription_manager')->deleteSubscriptions(array('mail' => $args['mail'], 'tid' => $args['tid']));
    +    \Drupal::entityManager()->getStorage('simplenews_subscriber')->deleteSubscriptions(array('mail' => $args['mail'], 'tid' => $args['tid']));
    

    entityManager() is deprecated, use entityTypeManager() instead.

  2. +++ b/src/SubscriberInterface.php
    @@ -21,7 +21,7 @@ interface SubscriberInterface extends ContentEntityInterface {
       const INACTIVE = 0;
     
    -  /**
    +  /**simplenews_count_subscriptions
        * Subscriber is active.
    

    looks like an accidental paste.

  3. +++ b/src/Subscription/SubscriptionStorage.php
    @@ -0,0 +1,28 @@
    + * User: user
    + * Date: 05.01.16
    + * Time: 14:27
    + */
    +
    +namespace Drupal\simplenews\Subscription;
    

    Check other files for the correct dochblok here, @files and then Contains \...

    Also add a class docblock and an @inheritdoc (again,see others) to the method.

  4. +++ b/src/Subscription/SubscriptionStorage.php
    @@ -0,0 +1,28 @@
    +        // @todo: redo this in a proper way
    

    I think we can remove the @todo here then.

  5. +++ b/src/Subscription/SubscriptionStorage.php
    @@ -0,0 +1,28 @@
    +        $query = db_delete('simplenews_subscriber__subscriptions');
    +        foreach ($conditions as $key => $condition) {
    +            $query->condition($key, $condition);
    

    Use the injected database service, $this->database->delete()

  6. +++ b/src/Subscription/SubscriptionStorage.php
    @@ -0,0 +1,28 @@
    +        \Drupal::entityManager()->getStorage('simplenews_subscriber')->resetCache();
    

    You are the storage. so just $this->resetCache()

  7. +++ b/src/Subscription/SubscriptionStorageInterface.php
    @@ -0,0 +1,24 @@
    +
    +/**
    + * Subscription storage; delete.
    + */
    +interface SubscriptionStorageInterface {
    +    /**
    +     * Delete subscriptions.
    

    space between interface line and docblock.

    DeleteS

    I think the ; delete part isn't necessary.

johnchque’s picture

Changes made, extra code deleted.

johnchque’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work
+++ b/src/Form/NewsletterForm.php
@@ -154,7 +154,7 @@ class NewsletterForm extends EntityForm {
 
-    $form['simplenews_subject']['subject'] = array(
+    $form['simplenews_subject']['email_subject'] = array(
       '#type' => 'textfield',

You didn't properly rebase/merge and are reverting a change that I just committed.

The last submitted patch, 6: simplenews-move-delete-to-subscriptionstorage-2410259-2.patch, failed testing.

berdir’s picture

+++ b/src/Subscription/SubscriptionStorage.php
@@ -0,0 +1,32 @@
+        $query = $this->database->delete();
+        foreach ($conditions as $key => $condition) {

you're missing the tablename now.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new6.21 KB

Merged properly and added tablename. Attached new patch.

berdir’s picture

Status: Needs review » Needs work

Note: When addressing a review from someone, always provide an interdiff: https://www.drupal.org/documentation/git/interdiff

Then I don't have to re-read the whole patch, just the lines that changed.

  1. +++ b/src/Subscription/SubscriptionStorage.php
    @@ -0,0 +1,32 @@
    +
    +namespace Drupal\simplenews\Subscription;
    +
    +
    +use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
    +
    

    Too many newlines.

  2. +++ b/src/Subscription/SubscriptionStorage.php
    @@ -0,0 +1,32 @@
    +    /**
    +     * {@inheritdoc}
    +     */
    +    public function deleteSubscriptions($conditions = array()) {
    +        if (!db_table_exists('simplenews_subscriber__subscriptions')) {
    +            // This can happen if this is called during uninstall.
    +            return;
    +        }
    +        $query = $this->database->delete('simplenews_subscriber__subscriptions');
    +        foreach ($conditions as $key => $condition) {
    

    Indentation here and in the interface is off. Two spaces, not for. Make sure that PHPStorm is configured correctly.

    Instead of hardcoding the table name, we should use the API. $this->getTableMapping()->getTableName('subscriptions') should give you that.

  3. +++ b/src/Subscription/SubscriptionStorageInterface.php
    @@ -0,0 +1,24 @@
    +
    +/**
    + * Subscription storage
    + */
    

    Missing . at the end.

  4. +++ b/src/Subscription/SubscriptionStorageInterface.php
    @@ -0,0 +1,24 @@
    +interface SubscriptionStorageInterface {
    +    /**
    +     * Delete subscriptions.
    +     *
    

    Still missing an empty line here, and should be Deletes, with S. method descriptions are always third-person.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new6.11 KB
new2.92 KB

New patch added fixing indentation. Interdiff added.

Status: Needs review » Needs work
johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new6.11 KB

Sorry, wrong method used.

Status: Needs review » Needs work
johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new6.31 KB
new3.68 KB

Added new patch based on the comment #12, still hardcoding the table name to simplify the code. Added interdiff based on comment #12.

  • Berdir committed baa7ada on 8.x-1.x authored by yongt9412
    Issue #2410259 by yongt9412: Move simplenews_subscription_delete to...
berdir’s picture

Status: Needs review » Fixed

Thank you, committed.

Status: Fixed » Closed (fixed)

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