At the moment we have the class SimplenewsSourceNode, with its own interface an load of code that actually works regardless of entity type. It would be better to have a SimplenewsSourceEntity class that worked for any entity and make SimplenewsSourceNode and extension of that.

I was trying to write a Source class for another entity (a commerce order) and found myself duplicating alot of the code in SimplenewsSourceNode. At the same time, extending SimplenewsSourceNode for a different entity felt wrong.

Comments

rlmumford’s picture

Here's a patch - currently it depends on #1890468: SimplenewsSourceCacheStatic hard coded to only accept node sources. so one of the patches here includes that change to ease testing.

rlmumford’s picture

Here's a re-run as the use of entity_view was a little wrong.

berdir’s picture

Status: Needs review » Needs work

Looks like a nice start. I guess there will be more changes required to actually support different entity types (e.g. the whole spool handling) but we can start with this and then partially convert the other things too.

+++ b/includes/simplenews.source.incundefined
@@ -434,13 +401,6 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
   /**
-   * Returns the corresponding newsletter.
-   */
-  public function getNewsletter() {
-    return $this->newsletter;
-  }
-
-  /**
    * Set the active subscriber.
    */
   public function setSubscriber($subscriber) {
@@ -455,6 +415,13 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {

@@ -455,6 +415,13 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
   }
 
   /**
+   * Get Newsletter
+   */
+  public function getNewsletter() {
+    return $this->newsletter;
+  }

Why is this moved?

docblock should at least be kept?

+++ b/includes/simplenews.source.incundefined
@@ -653,10 +627,12 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
     // Build message body
     // Supported view modes: 'email_plain', 'email_html', 'email_textalt'
-    $build = node_view($this->node, 'email_' . $format);
+    $build = entity_view($this->getEntityType(), array($this->getEntity()), 'email_' . $format);
+    $build = reset($build[$this->getEntityType()]);

Hm. We only add those view modes to node. Note sure, we should probably at least document that they need to be created (e.g. with DS or a similar module) if another entity type should be used.

+++ b/includes/simplenews.source.incundefined
@@ -653,10 +627,12 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
+    list(,,$bundle) = entity_extract_ids($this->getEntityType(), $this->getEntity());
+    foreach (field_info_instances($this->getEntityType(),$bundle) as $field_name => $field) {

@@ -803,7 +779,8 @@ class SimplenewsSourceNode implements SimplenewsSourceNodeInterface {
-    foreach (field_info_instances('node', $this->node->type) as $field_name => $field_instance) {
+    list(,,$bundle) = entity_extract_ids($this->getEntityType(), $this->getEntity());

Missing spaces after some , (also for list())

+++ b/includes/simplenews.source.incundefined
@@ -856,7 +905,7 @@ abstract class SimplenewsSourceCacheStatic implements SimplenewsSourceCacheInter
-  public function __construct(SimplenewsSourceNodeInterface $source) {
+  public function __construct(SimplenewsSourceInterface $source) {

As mentioned in the other issue, this actually needs to be updated as well, it currently does rely on something node (with this patch entity) specific.

So we need need to specify Entity here or change they way the cid is generated in there.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new10.14 KB

I guess there will be more changes required to actually support different entity types (e.g. the whole spool handling) but we can start with this and then partially convert the other things too.

I agree, I think there will be some changes to make to the spool and also to some of the theme functions called by the Simplenews Source class.

Why is this moved?

I think I accidentally deleted it then put it back in. Should be fixed in this patch.

Note sure, we should probably at least document that they need to be created (e.g. with DS or a similar module) if another entity type should be used.

I think documentation is most important. I also thought about making a 'getViewMode($format)' method. This would allow one entity type to be rendered in different ways for different emails (e.g. an event entity may have a Confirmation source class associated with it and a Cancellation source class associated with it - two different source classes, one entity type, 6 (?) view modes; confirmation_(html/plain/textalt) and cancellation_(html/plain/textalt))

Missing spaces after some , (also for list())

Fixed in patch.

As mentioned in the other issue, this actually needs to be updated as well, it currently does rely on something node (with this patch entity) specific.

So we need need to specify Entity here or change they way the cid is generated in there.

I've added a fix in for this in the latest patch. I built the cid off of entity_type:entity_id:language and restricted the source parameter to being SimplenewsSourceEntityInterface. It sound like trying to tackle this and the other issue independently is going to cause more trouble than its worth, maybe just fix them both in here? This patch therefore includes some changes to the Cacheing classes and makes the other issue redundant.

berdir’s picture

Status: Needs review » Fixed

Ok, committed and pushed.

The other issue can be closed as a duplicate now I think.

Status: Fixed » Closed (fixed)

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