function simplenews_scheduler_form_simplenews_node_tab_alter in Simplenews Scheduler 8
Same name and namespace in other branches
- 2.0.x simplenews_scheduler.module \simplenews_scheduler_form_simplenews_node_tab_alter()
Implements hook_form_FORM_ID_alter().
@todo replace the "This newsletter has been sent" checkbox of simplenews module by a message like "Last edition of this newsletter was sent at 12.12.2012"
File
- ./
simplenews_scheduler.module, line 26 - Simplenews Scheduler module allows a schedule to be set for sending (and resending) a Simplenews item.
Code
function simplenews_scheduler_form_simplenews_node_tab_alter(array &$form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
// Add schedule settings to the send newsletter form.
if (\Drupal::currentUser()
->hasPermission('send scheduled newsletters')) {
// Make sure that this is not an edition.
$node = $form_state
->get('node');
// Only add the schedule send options if the newsletter has not been sent,
// in which case there is no send form element.
if (isset($form['send']) && !isset($node->simplenews_scheduler_edition)) {
$scheduler = array();
$record = db_select('simplenews_scheduler', 's')
->fields('s')
->condition('nid', $node
->id())
->execute()
->fetchAssoc();
if (!empty($record)) {
$scheduler = $record;
$checked = TRUE;
}
else {
$scheduler['activated'] = 0;
$checked = FALSE;
}
$form_state
->set('scheduler', $scheduler);
$form['scheduler'] = array(
'#type' => 'details',
'#open' => TRUE,
'#title' => t('Scheduled Newsletter'),
);
$form['scheduler']['enable_scheduler'] = array(
'#type' => 'checkbox',
'#title' => t('Enable scheduled newsletter'),
'#default_value' => $checked,
);
// If the this is a scheduled newsletter then close send and test.
if ($checked) {
$form['test']['#open'] = FALSE;
$form['send']['#open'] = FALSE;
}
// If there is no default value, use the current time for start.
$start_date = !empty($scheduler['start_date']) ? $scheduler['start_date'] : REQUEST_TIME;
// and Today + 2 years for stop, that should be enough.
$stop_date = !empty($scheduler['stop_date']) ? $scheduler['stop_date'] : REQUEST_TIME + 2 * 365 * 24 * 60 * 60;
// Get DateTime objects for the default values of start and stop date.
$default_start_date = DrupalDateTime::createFromTimestamp($start_date);
$default_stop_date = DrupalDateTime::createFromTimestamp($stop_date);
// Get DateFormat objects for the default date and time formats.
// @todo formats not working?! localization?
$date_format = '';
$time_format = '';
if ($date_format_entity = DateFormat::load('html_date')) {
/** @var $date_format_entity \Drupal\Core\Datetime\DateFormatInterface */
$date_format = $date_format_entity
->getPattern();
}
if ($time_format_entity = DateFormat::load('html_time')) {
/** @var $time_format_entity \Drupal\Core\Datetime\DateFormatInterface */
$time_format = $time_format_entity
->getPattern();
}
$site_timezones_url = Url::fromRoute('entity.date_format.collection');
$user_timezones_url = Url::fromRoute('entity.user.edit_form', [
'user' => $user
->id(),
]);
$form['scheduler']['settings'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
':input[name="enable"]' => array(
'checked' => FALSE,
),
),
),
);
$form['scheduler']['settings']['start_date'] = array(
'#type' => 'datetime',
'#title' => t('Start sending on'),
'#default_value' => $default_start_date,
'#required' => TRUE,
'#date_date_format' => $date_format,
'#date_time_format' => $time_format,
'#date_year_range' => '-0:+3',
'#description' => t('Intervals work by creating a new node at the
desired time and marking this to be sent, ensure
you have your @site_link
configured and @user_link
configured.', array(
'@site_link' => \Drupal::l(t('site timezones'), $site_timezones_url),
'@user_link' => \Drupal::l(t('user timezone'), $user_timezones_url),
)),
);
$intervals = array(
'hour' => t('Hour'),
'day' => t('Day'),
'week' => t('Week'),
'month' => t('Month'),
);
$form['scheduler']['settings']['interval'] = array(
'#type' => 'select',
'#title' => t('Sending interval'),
'#options' => $intervals,
'#description' => t('Interval to send at'),
'#default_value' => !empty($scheduler['send_interval']) ? $scheduler['send_interval'] : 'week',
);
$form['scheduler']['settings']['frequency'] = array(
'#type' => 'textfield',
'#title' => t('Interval frequency'),
'#size' => 5,
'#default_value' => !empty($scheduler['interval_frequency']) ? $scheduler['interval_frequency'] : 1,
'#description' => t('Set the number of Intervals between newsletter transmission.'),
);
$stoptypes = array(
t('Never'),
t('On a given date'),
t('After a maximum number of editions'),
);
$form['scheduler']['settings']['stoptype'] = array(
'#type' => 'radios',
'#title' => t('Stop sending'),
'#options' => $stoptypes,
'#default_value' => !empty($scheduler['stop_type']) ? $scheduler['stop_type'] : 0,
'#attributes' => array(
'class' => array(
'simplenews-command-stop',
),
),
);
$form['scheduler']['settings']['stop_edition'] = array(
'#type' => 'textfield',
'#default_value' => isset($scheduler['stop_edition']) ? $scheduler['stop_edition'] : 0,
'#size' => 5,
'#maxlength' => 5,
'#required' => TRUE,
'#description' => t('The maximum number of editions which should be sent.'),
'#states' => array(
'visible' => array(
':input[name="stoptype"]' => array(
'value' => (string) 2,
),
),
),
);
$form['scheduler']['settings']['stop_date'] = array(
'#type' => 'datetime',
'#title' => t('Stop sending on'),
'#default_value' => $default_stop_date,
'#required' => TRUE,
'#date_date_format' => $date_format,
'#date_time_format' => $time_format,
'#date_year_range' => '-0:+3',
'#description' => t('The date when the last sent newsletter will be sent.'),
'#states' => array(
'visible' => array(
':input[name="stoptype"]' => array(
'value' => (string) 1,
),
),
),
);
$form['scheduler']['settings']['title'] = array(
'#type' => 'textfield',
'#title' => t('Title pattern for new edition nodes'),
'#description' => t('New edition nodes will have their title set to the above string, with tokens replaced.'),
'#required' => TRUE,
'#default_value' => isset($scheduler['title']) ? $scheduler['title'] : '[node:title]',
);
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$form['scheduler']['settings']['token_browser'] = array(
'#theme' => 'token_tree_link',
'#token_types' => array(
'node',
),
);
}
$form['scheduler']['activated'] = array(
'#type' => 'value',
'#value' => $scheduler['activated'],
);
$form['scheduler']['settings']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save scheduler settings'),
'#submit' => array(
'simplenews_scheduler_submit',
),
);
}
elseif (isset($node->simplenews_scheduler_edition)) {
// This is a newsletter edition.
$parent_node = \Drupal::entityManager()
->getStorage('node')
->load($node->simplenews_edition_parent);
$form['scheduler_msg'] = array(
'#markup' => t('This node is part of a scheduled newsletter configuration. View the original newsletter @parent.', array(
'@parent' => \Drupal::l(t('here'), $parent_node
->urlInfo()),
)),
'#weight' => -99,
);
}
}
}