class SkipForm in Notify 2.0.x
Same name and namespace in other branches
- 8 src/Form/SkipForm.php \Drupal\notify\Form\SkipForm
- 1.0.x src/Form/SkipForm.php \Drupal\notify\Form\SkipForm
Defines a form that configures forms module settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\notify\Form\SkipForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SkipForm
1 string reference to 'SkipForm'
File
- src/
Form/ SkipForm.php, line 16
Namespace
Drupal\notify\FormView source
class SkipForm extends ConfigFormBase {
/**
* Drupal\Core\Messenger\MessengerInterface definition.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Class constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The core messenger service.
*/
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
parent::__construct($config_factory);
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'notify_skip_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'notify.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('notify.settings');
// Fetch list of nodes and comments scheduled for notification.
list($res_nodes, $res_comms, $res_nopub, $res_copub, $res_nounp, $res_counp) = _notify_select_content();
// Get the nodes and comments queued.
$count = 0;
$nodes = $comments = [];
// Ordinary nodes.
foreach ($res_nodes as $row) {
$nodes[$row->nid] = \Drupal::entityTypeManager()
->getStorage('node')
->load($row->nid);
$count++;
}
// Ordinary comments.
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 = Comment::load($row->cid);
$comments[$comment
->get('entity_id')->target_id][$row->cid] = $comment;
$count++;
}
foreach ($res_copub as $row) {
$comment = Comment::load($row->cid);
if (!isset($comments[$row->nid][$row->cid])) {
$comments[$row
->get('entity_id')->target_id][$row->cid] = $comment;
$count++;
}
}
}
// Published nodes in unpublished queue.
foreach ($res_nopub as $row) {
if (!isset($nodes[$row->nid])) {
$nodes[$row->nid] = \Drupal::entityTypeManager()
->getStorage('node')
->load($row->nid);
$count++;
}
}
// Unpublished nodes in unpublished queue.
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'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('The following table shows all messages that are candidates for notification emails:'),
'#suffix' => '</p>',
];
$skpnodes = $config
->get('notify_skip_nodes');
if (NULL == $skpnodes) {
$skpnodes = [];
}
$skpcomts = $config
->get('notify_skip_comments');
if (NULL == $skpcomts) {
$skpcomts = [];
}
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Set skip flags'),
];
$form['settings']['table'] = [
'#tree' => TRUE,
'#type' => 'table',
'#header' => [
$this
->t('NID'),
$this
->t('CID'),
$this
->t('Published'),
$this
->t('Created'),
$this
->t('Updated'),
$this
->t('Title'),
$this
->t('Skip'),
],
];
$ii = 0;
foreach ($nodes as $node) {
$ii++;
$form['settings']['table'][$ii]['nid'] = [
'#markup' => $node
->id(),
];
$form['settings']['table'][$ii]['cid'] = [
'#markup' => '-',
];
$form['settings']['table'][$ii]['published'] = [
'#markup' => $node
->isPublished() ? 'Yes' : 'No',
];
$form['settings']['table'][$ii]['created'] = [
'#markup' => \Drupal::service('date.formatter')
->format($node
->getCreatedTime(), 'short'),
];
$form['settings']['table'][$ii]['updated'] = [
'#markup' => $node
->getChangedTime() != $node
->getCreatedTime() ? \Drupal::service('date.formatter')
->format($node
->getChangedTime(), 'short') : '-',
];
$form['settings']['table'][$ii]['title'] = [
'#markup' => $node
->label(),
];
$flag = in_array($node
->id(), $skpnodes) ? 1 : 0;
$form['settings']['table'][$ii]['dist'] = [
'#type' => 'checkbox',
'#default_value' => $flag,
];
}
foreach ($comments as $thread) {
foreach ($thread as $comment) {
$ii++;
$form['settings']['table'][$ii]['nid'] = [
'#markup' => $comment
->get('entity_id')->target_id,
];
$form['settings']['table'][$ii]['cid'] = [
'#markup' => $comment
->id(),
];
$form['settings']['table'][$ii]['published'] = [
'#markup' => $comment
->isPublished() ? 'Yes' : 'No',
];
$form['settings']['table'][$ii]['created'] = [
'#markup' => \Drupal::service('date.formatter')
->format($comment
->getCreatedTime(), 'short'),
];
$form['settings']['table'][$ii]['updated'] = [
'#markup' => $comment
->getChangedTime() != $comment
->getCreatedTime() ? \Drupal::service('date.formatter')
->format($comment
->getChangedTime(), 'short') : '-',
];
$form['settings']['table'][$ii]['title'] = [
'#markup' => $comment
->label(),
];
$flag = in_array($comment
->id(), $skpcomts) ? 1 : 0;
$form['settings']['table'][$ii]['dist'] = [
'#type' => 'checkbox',
'#default_value' => $flag,
];
}
}
$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);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$form_values = $form_state
->getCompleteForm();
$nodes = [];
$comts = [];
if (isset($values['settings']['table']) && $values['settings']['table']) {
foreach ($values['settings']['table'] as $dist => $ii) {
if ($ii['dist']) {
$nid = $form_values['settings']['table'][$dist]['nid']['#markup'];
$cid = $form_values['settings']['table'][$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.'));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SkipForm:: |
protected | property |
Drupal\Core\Messenger\MessengerInterface definition. Overrides MessengerTrait:: |
|
SkipForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SkipForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SkipForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SkipForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SkipForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SkipForm:: |
public | function |
Class constructor. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |