View source
<?php
namespace Drupal\notify\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class SkipForm extends ConfigFormBase {
protected $messenger;
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
parent::__construct($config_factory);
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'));
}
public function getFormId() {
return 'notify_skip_settings';
}
protected function getEditableConfigNames() {
return [
'notify.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('notify.settings');
list($res_nodes, $res_comms, $res_nopub, $res_copub, $res_nounp, $res_counp) = _notify_select_content();
$count = 0;
$nodes = $comments = [];
foreach ($res_nodes as $row) {
$nodes[$row->nid] = \Drupal::entityTypeManager()
->getStorage('node')
->load($row->nid);
$count++;
}
if ($res_comms) {
foreach ($res_nopub as $row) {
if (!isset($nodes[$row->nid])) {
$nodes[$row->nid] = \Drupal::entityTypeManager()
->getStorage('node')
->load($row->nid);
$count++;
}
}
foreach ($res_comms as $row) {
$comment = \Drupal\comment\Entity\Comment::load($row->cid);
$comments[$comment
->get('entity_id')->target_id][$row->cid] = $comment;
$count++;
}
foreach ($res_copub as $row) {
if (!isset($comments[$row->nid][$row->cid])) {
$comments[$row
->get('entity_id')->target_id][$row->cid] = \Drupal\comment\Entity\Comment::load($row->cid);
$count++;
}
}
}
foreach ($res_nopub as $row) {
if (!isset($nodes[$row->nid])) {
$nodes[$row->nid] = \Drupal::entityTypeManager()
->getStorage('node')
->load($row->nid);
$count++;
}
}
foreach ($res_nounp as $row) {
if (!isset($nodes[$row->nid])) {
$nodes[$row->nid] = \Drupal::entityTypeManager()
->getStorage('node')
->load($row->nid);
$count++;
}
}
$form = [];
$form['#tree'] = TRUE;
$form['info'] = [
'#markup' => '<p>' . $this
->t('The following table shows all messages that are candidates for bulk notifications:' . '</p>'),
];
$skpnodes = $config
->get('notify_skip_nodes');
$skpcomts = $config
->get('notify_skip_comments');
$ii = 0;
$entities = [];
foreach ($nodes as $node) {
$ii++;
$entities[$ii] = [];
$entities[$ii]['nid'] = [
'#markup' => $node
->id(),
];
$entities[$ii]['cid'] = [
'#markup' => '-',
];
$entities[$ii]['created'] = [
'#markup' => \Drupal::service('date.formatter')
->format($node
->getCreatedTime(), 'short'),
];
$entities[$ii]['updated'] = [
'#markup' => $node
->getChangedTime() != $node
->getCreatedTime() ? \Drupal::service('date.formatter')
->format($node
->getChangedTime(), 'short') : '-',
];
$entities[$ii]['title'] = [
'#markup' => $node
->label(),
];
$flag = in_array($node
->id(), $skpnodes) ? 1 : 0;
$entities[$ii]['dist'] = [
'#type' => 'checkbox',
'#default_value' => $flag,
];
}
foreach ($comments as $thread) {
foreach ($thread as $comment) {
$ii++;
$entities[$ii] = [];
$entities[$ii]['nid'] = [
'#markup' => $comment
->get('entity_id')->target_id,
];
$entities[$ii]['cid'] = [
'#markup' => $comment
->id(),
];
$entities[$ii]['created'] = [
'#markup' => \Drupal::service('date.formatter')
->format($comment
->getCreatedTime(), 'short'),
];
$entities[$ii]['updated'] = [
'#markup' => $comment
->getChangedTime() != $comment
->getCreatedTime() ? \Drupal::service('date.formatter')
->format($comment
->getChangedTime(), 'short') : '-',
];
$entities[$ii]['title'] = [
'#markup' => $comment
->label(),
];
$flag = in_array($comment
->id(), $skpcomts) ? 1 : 0;
$entities[$ii]['dist'] = [
'#type' => 'checkbox',
'#default_value' => $flag,
];
}
}
$form['entities'] = $entities;
$users = $config
->get('notify_users');
$batch_remain = $users ? count($users) : 0;
if ($batch_remain) {
$form['info2'] = [
'#markup' => '<p>' . $this
->t('Please note that the list above may be out of sync. Saving an altered list of skip flags is disabled as long as notifications are being processed.') . '</p> ',
];
}
else {
$form['info2'] = [
'#markup' => '<p>' . $this
->t('To flag that <em>no</em> notification about a particular message should be sent, check the checkbox in the “Skip“ column. Press “Save settings“ to save the flags.') . '</p> ',
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$form_values = $form_state
->getCompleteForm();
$nodes = [];
$comts = [];
if (isset($values['entities']) && $values['entities']) {
foreach ($values['entities'] as $dist => $ii) {
if ($ii['dist']) {
$nid = $form_values['entities'][$dist]['nid']['#markup'];
$cid = $form_values['entities'][$dist]['cid']['#markup'];
if ('-' == $cid) {
array_push($nodes, (int) $nid);
}
else {
array_push($comts, (int) $cid);
}
}
}
$this
->config('notify.settings')
->set('notify_skip_nodes', $nodes)
->set('notify_skip_comments', $comts)
->save();
}
$this->messenger
->addMessage($this
->t('Skip flags saved.'));
}
}