function ViewsSend::viewsFormSubmit in Views Send 8
Overrides \Drupal\system\Plugin\views\field\BulkForm::viewsFormSubmit().
Overrides BulkForm::viewsFormSubmit
File
- src/
Plugin/ views/ field/ ViewsSend.php, line 88
Class
- ViewsSend
- Defines a simple send mass mail form element.
Namespace
Drupal\views_send\Plugin\views\fieldCode
function viewsFormSubmit(&$form, FormStateInterface $form_state) {
switch ($form_state
->get('step')) {
case 'views_form_views_form':
$field_name = $this->options['id'];
$selection = array_filter($form_state
->getValue($field_name));
$form_state
->set('selection', array_keys($selection));
$form_state
->set('step', 'views_send_config_form');
// Preserve the URL as it gets lost if block display and batch API.
if ($this->view
->hasUrl()) {
$url = $this->view
->getUrl();
}
else {
// For some reason Url::fromRoute('<current>') doesn't work.
$url = Url::fromUserInput(\Drupal::service('path.current')
->getPath());
}
$query = UrlHelper::filterQueryParameters($_GET, array(
'q',
));
$form_state
->set('url', $url
->setOption('query', $query));
$form_state
->setRebuild(TRUE);
break;
case 'views_send_config_form':
$display = $form['display']['#value'];
$config = \Drupal::configFactory()
->getEditable('views_send.user_settings');
$config_basekey = $display . '.uid:' . \Drupal::currentUser()
->id();
$form_state_values = $form_state
->getValues();
if ($form_state
->getValue('views_send_remember')) {
foreach ($form_state_values as $key => $value) {
$key = $key == 'format' ? 'views_send_message_format' : $key;
if (substr($key, 0, 11) == 'views_send_') {
$config
->set($config_basekey . '.' . substr($key, 11), $value);
}
}
$config
->save();
}
else {
$config
->clear($config_basekey);
$config
->save();
}
$form_state
->set('configuration', $form_state_values);
// If a file was uploaded, process it.
if (VIEWS_SEND_MIMEMAIL && \Drupal::currentUser()
->hasPermission('attachments with views_send') && isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['views_send_attachments'])) {
// attempt to save the uploaded file
$dir = \Drupal::config('system.file')
->get('default_scheme') . '://views_send_attachments';
\Drupal::service('file_system')
->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);
$files = file_save_upload('views_send_attachments', [], $dir);
// set error if file was not uploaded
if (!$files) {
$form_state
->setErrorByName('views_send_attachment', $this
->t('Error uploading file.'));
}
else {
// set files to form_state, to process when form is submitted
// @todo: when we add a multifile formfield then loop through to add each file to attachments array
$form_state
->set(array(
'configuration',
'views_send_attachments',
), $files);
}
}
$form_state
->set('step', 'views_send_confirm_form');
$form_state
->setRebuild(TRUE);
break;
case 'views_send_confirm_form':
// Queue the email for sending.
views_send_queue_mail($form_state
->get('configuration'), $form_state
->get('selection'), $this->view);
$form_state
->setRedirectUrl($form_state
->get('url'));
break;
}
}