View source
<?php
namespace Drupal\views_send\Plugin\views\field;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\BulkForm;
use Drupal\Core\File\FileSystemInterface;
class ViewsSend extends BulkForm {
protected function defineOptions() {
$options = parent::defineOptions();
$options['enable_excluded_fields'] = [
'default' => 1,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['element_label_colon']['#access'] = FALSE;
$form['exclude']['#access'] = FALSE;
$form['alter']['#access'] = FALSE;
$form['empty_field_behavior']['#access'] = FALSE;
$form['action_title']['#access'] = FALSE;
$form['include_exclude']['#access'] = FALSE;
$form['selected_actions']['#access'] = FALSE;
$form['enable_excluded_fields'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use excluded fields as tokens'),
'#default_value' => $this->options['enable_excluded_fields'],
];
}
function viewsForm(&$form, FormStateInterface $form_state) {
parent::viewsForm($form, $form_state);
if (empty($this->view->result)) {
return;
}
$form['#attached']['library'][] = 'views_send/views_send.form';
unset($form['header']);
$step = $form_state
->get('step');
if ($step == 'views_form_views_form') {
$form['actions']['submit']['#value'] = $this
->t('Next', [], [
'context' => 'views_send: Go to configure mail',
]);
$form['#prefix'] = '<div class="views-send-selection-form">';
$form['#suffix'] = '</div>';
}
else {
unset($form['output']);
$step($form, $form_state, $this->view);
}
}
protected function getBulkOptions($filtered = TRUE) {
return [];
}
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');
if ($this->view
->hasUrl()) {
$url = $this->view
->getUrl();
}
else {
$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 (VIEWS_SEND_MIMEMAIL && \Drupal::currentUser()
->hasPermission('attachments with views_send') && isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['views_send_attachments'])) {
$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);
if (!$files) {
$form_state
->setErrorByName('views_send_attachment', $this
->t('Error uploading file.'));
}
else {
$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':
views_send_queue_mail($form_state
->get('configuration'), $form_state
->get('selection'), $this->view);
$form_state
->setRedirectUrl($form_state
->get('url'));
break;
}
}
function viewsFormValidate(&$form, FormStateInterface $form_state) {
if ($form_state
->get('step') != 'views_form_views_form') {
return;
}
$field_name = $this->options['id'];
$selection = array_filter($form_state
->getValue($field_name));
if (empty($selection)) {
$form_state
->setErrorByName($field_name, $this
->t('Please select at least one item.'));
}
}
}