You are here

function _simplenews_scheduler_new_edition in Simplenews Scheduler 8

Same name and namespace in other branches
  1. 6.2 simplenews_scheduler.module \_simplenews_scheduler_new_edition()
  2. 6 simplenews_scheduler.module \_simplenews_scheduler_new_edition()
  3. 7 simplenews_scheduler.module \_simplenews_scheduler_new_edition()
  4. 2.0.x simplenews_scheduler.module \_simplenews_scheduler_new_edition()

Create a new newsletter edition based on the master edition of this newsletter.

This does no checking of whether a new edition should be made; it's up to the caller to determine this first.

Parameters

$nid: The node id of the parent newsletter node to use as a template.

$edition_time: Desired edition creation time.

Return value

The node id of the new edition node.

2 calls to _simplenews_scheduler_new_edition()
SimplenewsSchedulerEditionDueTest::testEditionsDue in src/Tests/SimplenewsSchedulerEditionDueTest.php
Test simplenews_scheduler_get_newsletters_due().
simplenews_scheduler_cron in ./simplenews_scheduler.module
Implements hook_cron().

File

./simplenews_scheduler.module, line 565
Simplenews Scheduler module allows a schedule to be set for sending (and resending) a Simplenews item.

Code

function _simplenews_scheduler_new_edition($nid, $edition_time) {

  // Load the template node and clone an edition.
  $template_node = \Drupal::entityManager()
    ->getStorage('node')
    ->load($nid);
  $edition_node = simplenews_scheduler_clone_node($template_node);

  // Set the node's creation time as the given timestamp.
  $edition_node->created = $edition_time;

  // Run the title through token replacement
  // Get title pattern from the scheduler record, not newsletter node.
  $schedrecord = db_select('simplenews_scheduler', 's')
    ->fields('s')
    ->condition('nid', $template_node
    ->id())
    ->execute()
    ->fetchAssoc();
  $edition_node->title = \Drupal::token()
    ->replace($schedrecord['title'], array(
    'node' => $edition_node,
  ));

  // Invoke simplenews_scheduler_edition_node() to give installed modules a
  // chance to modify the cloned edition node if necessary before it gets saved.
  \Drupal::moduleHandler()
    ->alter('simplenews_scheduler_edition_node', $edition_node, $template_node);

  // Save the changes of other modules
  $edition_node
    ->save();

  // Insert edition data.
  $values = array(
    'eid' => $edition_node
      ->id(),
    'pid' => $template_node
      ->id(),
    'date_issued' => $edition_time,
  );
  db_insert('simplenews_scheduler_editions')
    ->fields($values)
    ->execute();

  // Add a watchdog entry.
  $context = array(
    '%title' => $edition_node
      ->label(),
    'link' => \Drupal::l(t('view'), $edition_node
      ->urlInfo()),
  );
  \Drupal::logger('simplenews_sched')
    ->notice('Created a new newsletter edition %title', $context);

  // Prepare the correct status for Simplenews to pickup.
  simplenews_issue_update_sent_status($edition_node);
  return $edition_node
    ->id();
}