Now that #1890718: Provide a default Entity source class is committed it makes sense to allow the mail spool to support different entity types. There is LOADS we could do here, but I think for now we should do the minimum possible to allow other entities to use the spooling system. Practically this includes:

  • Replacing the nid column with an entity_type and entity_id column
  • Updating bits of SimplenewsSpool to use entity functions rather than node functions
  • Update simplenews_mail_spool
  • Update simplenews_save_spool
  • Update simplenews_add_node_to_spool

This should mean that once mails are stored in the spool its simplenews can process them and send them effectively. We can work out how to easily add random entities to the spool in another issue as I realise there are many performance issues surrounding that (briefly mentioned in #1884290: Implement a pluggable system for who emails get sent to)

Questions

  1. How do we want to pick the Source class to use?
    SimplenewsSpool uses a method to get the Source class, which basically always returns SimplenewsSourceNode now that we're dealing with other entities this doesn't seem sufficient. My suggestion would be to try and see if SimplenewsSource exists, and if it doesn't use SimplenewsSourceEntity. What are others thoughts on this.

Comments

rlmumford’s picture

Status: Active » Needs review
StatusFileSize
new16.11 KB

Here's a first crack at this. Quite a few bits needed to be tweaked to pass tests (I'm still not sure I managed to fix all of them).

I don't like the way we work out the Simplenews source class to use. I wonder if we could store an 'simplenews source class' setting in the entity info?

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-1.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new17.69 KB

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-3.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new19.49 KB
berdir’s picture

+++ b/includes/simplenews.source.incundefined
@@ -328,7 +330,19 @@ class SimplenewsSpool implements SimplenewsSpoolInterface {
-    return variable_get('simplenews_source', 'SimplenewsSourceNode');
+    // Convert entity type into upper camel case and generate a class name.
+    $entity_type_words = explode('_', $spool_data->entity_type);
+    $class = 'SimplenewsSource';
+    foreach ($entity_type_words as $word) {
+      $class .= drupal_ucfirst(trim($word));
+    }
+
+    if (class_exists($class)) {
+      return $class;
+    }
+    else {
+      return 'SimplenewsSourceEntity';

I'd just introduce a second variable with a key like simplenews_source_$type. First check that, with the Node one as default for nodes and then fall back to the global setting if there's no specific override.

+++ b/simplenews.installundefined
@@ -1088,3 +1092,27 @@ function simplenews_update_7204() {
+  db_change_field('simplenews_mail_spool', 'nid', 'entity_id', array(
+        'description' => 'The entity id of this newsletter issue.',
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ));
+  db_add_field('simplenews_mail_spool', 'entity_type', array(
+        'description' => 'The entity type of this newsletter issue.',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ));
+
+  // Set every entity type to 'node' at this point.
+  db_update('simplenews_mail_spool')->fields(array('entity_type' => 'node'))->execute();

Indendation of the array should be like this:

  db_change_field('simplenews_mail_spool', 'nid', 'entity_id', array(
    'description' => 'The entity id of this newsletter issue.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));

And the db_update() should have the fields() and execute() on a separate line.

+++ b/simplenews.moduleundefined
@@ -409,7 +409,7 @@ function simplenews_node_delete($node) {
-  $count = simplenews_delete_spool(array('nid' => $node->nid));
+  $count = simplenews_delete_spool(array('entity_id' => $node->nid, 'entity_type' => 'node'));

Wondering if we want to continue supporting just nid here and then default to entity_type node for that.

+++ b/simplenews.moduleundefined
@@ -2524,10 +2526,10 @@ function simplenews_issue_status($node, $value = NULL) {
-function simplenews_issue_sent_count($node, $value = NULL) {
+function simplenews_issue_sent_count($entity, $entity_type, $value = NULL) {

The argument order is usually the other way round. Adding it and the end would only make sense if it were optional and default to node (but then it would have to be after $value).

berdir’s picture

Status: Needs review » Needs work
rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new20.85 KB

Here's an updated version of the patch with the changes suggested above. I've continued support for the 'nid' flag as a condition although I am tempted to set a message saying that using it is deprecated and may be unsupported in a future version.

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-8.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new20.88 KB

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-10.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new20.88 KB

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-12.patch, failed testing.

rlmumford’s picture

StatusFileSize
new20.88 KB

Forgot to update the tests after swapping the arguments for issue count over.

rlmumford’s picture

Status: Needs work » Needs review

Submit it for testing

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-14.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new20.88 KB

Missed one

Status: Needs review » Needs work

The last submitted patch, 1911322-any_entity_type_in_spool-17.patch, failed testing.

rlmumford’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work
+++ b/includes/simplenews.source.incundefined
@@ -328,7 +330,15 @@ class SimplenewsSpool implements SimplenewsSpoolInterface {
   protected function getSourceImplementation($spool_data) {
-    return variable_get('simplenews_source', 'SimplenewsSourceNode');
+    $default = ($spool_data->entity_type == 'node') ? 'SimplenewsSourceNode' : NULL;
+
+    $class = variable_get('simplenews_source_' . $spool_data->entity_type, $default);
+
+    if (empty($class)) {
+      $class = variable_get('simplenews_source', 'SimplenewsSourceEntity');
+    }
+

Maybe add a comment or two here to explain what we are doing, looks good to me otherwise.

+++ b/simplenews.installundefined
@@ -1088,3 +1092,29 @@ function simplenews_update_7204() {
+ * Replace nid with entity_type and entity_id columns in
+ * {simplenews_mail_spool}
+ * Set the entity_type column on existing rows to node

Missing a . Need to check how this looks on update.php, docblocks on update functions should be kept short (single line) if possible.

+++ b/simplenews.moduleundefined
@@ -2518,8 +2518,10 @@ function simplenews_issue_status($node, $value = NULL) {
  *
- * @param $node
+ * @param $entity
  *   The issue to get or set the status.
+ * @param $entity_type
+ *   Then entity_type of $entity.
  * @param $value
  *   (Optional) The sent count to be set or NULL to retrieve the current value.
  * @return
@@ -2527,10 +2529,10 @@ function simplenews_issue_status($node, $value = NULL) {

@@ -2527,10 +2529,10 @@ function simplenews_issue_status($node, $value = NULL) {
  *
  * @ingroup issue
  */
-function simplenews_issue_sent_count($node, $value = NULL) {
+function simplenews_issue_sent_count($entity_type, $entity, $value = NULL) {

Order of entity_type and entity in @params and function is not consistent.

rlmumford’s picture

Status: Needs work » Needs review
StatusFileSize
new53.53 KB
new21.09 KB

I've applied the changes suggested in this patch.

Missing a . Need to check how this looks on update.php, docblocks on update functions should be kept short (single line) if possible.

This looks fine to me (image included). We could make it shorter although I feel it's the right level detail to explain what's going on.

update.jpg

berdir’s picture

Status: Needs review » Fixed

Sorry for the long wait, this looks good. Commited.

Status: Fixed » Closed (fixed)

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