Problem/Motivation

Now are able to show unconfirmed and unsubscribed subscribers as well, and filter on them, but the subscription status is not visible.

Proposed resolution

Add " (Unconfirmed)" and " (Unsubscribed)" behind the subscription, this needs a custom field formatter that subclasses entity reference label.

Remaining tasks

User interface changes

API changes

Comments

edurenye’s picture

Status: Active » Needs review
StatusFileSize
new4.71 KB

Added the new formatter and edited the config file.

Status: Needs review » Needs work

The last submitted patch, 1: show_subscription-2420129-1.patch, failed testing.

berdir’s picture

  1. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriberStatusFormatter.php
    @@ -0,0 +1,56 @@
    + * @file
    + * Contains \Drupal\simplenews\Plugin\Field\FieldFormatter\CollectUriTailFormatter.
    + */
    

    This hasn't been updated.

  2. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriberStatusFormatter.php
    @@ -0,0 +1,56 @@
    + * @FieldFormatter(
    + *   id = "simplenews_status",
    

    simplenes_subscription_status is a better name for this.

  3. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriberStatusFormatter.php
    @@ -0,0 +1,56 @@
    + *   label = @Translation("Subscriber Status Formatter"),
    

    This should also mention subscription.

  4. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriberStatusFormatter.php
    @@ -0,0 +1,56 @@
    +      $elements[]['status'] = array(
    +        '#markup' => 'Unsubscribed',
    ...
    +      foreach ($items as $delta => $item) {
    +        if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED) {
    +          $output = 'Subscribed';
    +        }
    +        if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED) {
    +          $output = 'Unconfirmed';
    +        }
    +        if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED) {
    +          $output = 'Unsubscribed';
    +        }
    +        $elements[$delta]['status'] = array(
    +          '#markup' => $output,
    +        );
    

    Visible strings must always use t() or $this->t() if available so that they can be translated.

    Also not sure this here makes sense. I'd just leave it empty in this case. You don't really need the if/else then, you can always loop over items.

  5. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriberStatusFormatter.php
    @@ -0,0 +1,56 @@
    +        if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED) {
    +          $output = 'Subscribed';
    +        }
    +        if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED) {
    +          $output = 'Unconfirmed';
    +        }
    +        if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED) {
    +          $output = 'Unsubscribed';
    +        }
    +        $elements[$delta]['status'] = array(
    +          '#markup' => $output,
    

    This still needs to show the actual subscription label like the parent implementation does.

berdir’s picture

Issue tags: +Needs tests
edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new3.97 KB
new4.63 KB

Ok, all changed, now the label is like the parent.

miro_dietiker’s picture

This is a UI issue, please provide a screenshot.

mbovan’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
@@ -0,0 +1,48 @@
+      if ($items[$delta]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED) {
+        $output = 'Subscribed';
+      }
+      if ($items[$delta]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED) {
+        $output = 'Unconfirmed';
+      }
+      if ($items[$delta]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED) {
+        $output = 'Unsubscribed';
+      }
+      $elements[$delta]['#title'] = $output;

This still requires t() function...

edurenye’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new13.93 KB
new4.65 KB
new1.06 KB

Sorry, I did a rollback and I forgot to add the t() again.

Screnshot:

sasanikolic’s picture

Status: Needs review » Needs work

This also needs some tests as said by Berdir (comment #4).

mbovan’s picture

Issue summary: View changes
StatusFileSize
new7.54 KB

Do we want to have status linkable?

We already have link on the newsletter, and status links points to the same page.

Btw, how can I add a subscriber with Unsubscribed/Unconfirmed status?

miro_dietiker’s picture

Discussed. The subscription status should not get a separate column. It needs to be added to the subscription (newsletter) label and each subscription should be output as an item of a subscription list.

edurenye’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new14.21 KB
new5.66 KB
new4.95 KB

I did it as we discussed and also add the tests.

Screnshot:

miro_dietiker’s picture

Status: Needs review » Needs work

Looks pretty OK for me. One nit.

+++ b/src/Tests/SimplenewsAdministrationTest.php
@@ -810,10 +812,14 @@ class SimplenewsAdministrationTest extends SimplenewsTestBase {
+    $unsubscribed_status_text = $newsletters['default']->name . ' (' . t('Unconfirmed') . ')';
+    $this->assertText($unsubscribed_status_text);
...
+    $unsubscribed_status_text = $newsletters['default']->name . ' (' . t('Unsubscribed') . ')';
+    $this->assertText($unsubscribed_status_text);

No variable assignment here. Just check for it on one line.

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new1.25 KB
new4.83 KB

Ok, now it's in one line.

LKS90’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now!

miro_dietiker’s picture

  1. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
    @@ -0,0 +1,57 @@
    + * Implementation of the 'status' formatter for Simplenews Subscription Status.
    

    remove one "status".

  2. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
    @@ -0,0 +1,57 @@
    + *   label = @Translation("Subscriptions Status Formatter"),
    

    Remove "Formatter"

Can be done on commit since all nonfunctional.

edurenye’s picture

Done.

edurenye’s picture

StatusFileSize
new911 bytes

Sorry, this was the interdiff.

The last submitted patch, 17: show_subscription-2420129-17.patch, failed testing.

berdir’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests
+++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
@@ -0,0 +1,57 @@
+      // Add the label.
+      if ($output) {
+        $elements[$delta]['status'] = [
+          '#markup' => $output,
+        ];
+      }

Hm. While this is nice to make it show up after it... there is a problem with it.

This isn't going to work for left-to right languages, as the output won't be in the right order for them.

I think we need to change this to use t('@label (Unsubscribed)') and same for the other variation, so it can be translated properly. And then build the full output ourself and don't rely on the parent.

edurenye’s picture

Ok, but rebuild totally the method could rely on repeated code, I think you mean just for the title.

edurenye’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work

No, you must have two separate t() calls for the two cases with additional label to have it properly translatable. for the confirmed case, you can just use the entity label as is.

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new1.59 KB
new4.21 KB

Ok, I made those changes.

berdir’s picture

Status: Needs review » Needs work

Hm.

Is it really helpful to have the newsletter there linked? it doesn't seem like a common operation to got to the newsletter form from there, and having all those links on a page with lots of newsletters seems quite distracting.

Also, when you edit the view and disable the link option then this doesn't work anymore because the parent structure doesn't match anymore.

Let's change this to extend from EntityReferenceFormatterBase, no longer call the parent at all and just set #markup => $output.

  1. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
    @@ -0,0 +1,56 @@
    +/**
    + * Implementation of the 'status' formatter for Simplenews Subscription.
    + *
    

    Formatter that displays a newsletter subscription with the status.

  2. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
    @@ -0,0 +1,56 @@
    +      // Add label for subscribed.
    +      $output = $this->t('@label', array('@label' => $label));
    

    You can just directly assign $label.

    And the comment should be something like "// Do not explicitly display the status for confirmed subscriptions."

  3. +++ b/src/Plugin/Field/FieldFormatter/SimplenewsSubscriptionStatusFormatter.php
    @@ -0,0 +1,56 @@
    +      // Add the label.
    +      $elements[$delta]['#title'] = $output;
    

    This will then become #markup.

berdir’s picture

We have a pretty good demo module now to test this with some data. We should make sure to have at least one unconfirmed and unsubscribed subscription set up in simplenews_demo_install().

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new4.89 KB
new2.82 KB

Ok, just unconfirmed was missing in demo, now is added.

berdir’s picture

Status: Needs review » Needs work
+++ b/modules/simplenews_demo/simplenews_demo.install
@@ -85,6 +85,8 @@ function simplenews_demo_install() {
   $subscription_manager->unsubscribe('c@example.com', $newsletters['press_releases']->id(), FALSE);
+  // Unconfirmed subscriber.
+  $subscription_manager->subscribe('d@example.com', $newsletters['special_offers']->id(), SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, FALSE);

That's how this API works. There are arguments that except the constant, it just expects NULL, TRUE or FALSE. And what you want is TRUE.

The last submitted patch, 27: show_subscription-2420129-27.patch, failed testing.

berdir’s picture

Test fails can be ignored, that's just missing dependencies. You might want to run the demo test locally though to make sure that is passing.

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new898 bytes
new4.89 KB

Done. In my machine it pass the tests.

berdir’s picture

Status: Needs review » Needs work
+++ b/modules/simplenews_demo/simplenews_demo.install
@@ -85,6 +85,8 @@ function simplenews_demo_install() {
   $subscription_manager->unsubscribe('c@example.com', $newsletters['press_releases']->id(), FALSE);
+  // Unconfirmed subscriber.
+  $subscription_manager->subscribe('d@example.com', $newsletters['special_offers']->id(), SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED, TRUE);

I'm sorry but this is still not correct.

There is no argument that would accept that constant. It works, but that's only because the value is cast to a boolean.

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new4.85 KB
new853 bytes

Done.

Status: Needs review » Needs work

The last submitted patch, 33: show_subscription-2420129-33.patch, failed testing.

juanse254’s picture

+++ b/modules/simplenews_demo/simplenews_demo.install
@@ -85,6 +85,8 @@ function simplenews_demo_install() {
+  // Unconfirmed subscriber.

Same comment as above, delete the comment or give a more accurate description.

Besides that looks good to me, test failing due to core issues.

edurenye’s picture

Status: Needs work » Needs review

Those comments are not saying the same.
Yes, it's failing for a core change, there is a issue open for this, so it's not related to this issue.

juanse254’s picture

Right, by the way the related issue is #2540586: Link "Add newsletter" not rendered properly as soon as that gets fixed this will be green as well.

berdir’s picture

Status: Needs review » Fixed

Now it looks fine, committed.

  • Berdir committed 6bb64e8 on 8.x-1.x authored by edurenye
    Issue #2420129 by edurenye: Show subscription status behind newsletter...

Status: Fixed » Closed (fixed)

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