class DashboardForm in Mail Safety 8
Class DashboardForm.
@package Drupal\mail_safety\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\mail_safety\Form\DashboardForm
Expanded class hierarchy of DashboardForm
1 string reference to 'DashboardForm'
File
- src/
Form/ DashboardForm.php, line 19
Namespace
Drupal\mail_safety\FormView source
class DashboardForm extends FormBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The date formatter.
*
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandler
*/
protected $moduleHandler;
/**
* DashboardForm constructor.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
* The date formatter.
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* The module handler.
*/
public function __construct(Connection $database, DateFormatter $date_formatter, ModuleHandler $module_handler) {
$this->database = $database;
$this->dateFormatter = $date_formatter;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('date.formatter'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
// Unique ID of the form.
return 'mail_safety_dashboard_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$table_structure = [];
// Create the headers.
$table_structure['header'] = [
[
'data' => $this
->t('Subject'),
],
[
'data' => $this
->t('Date sent'),
'field' => 'sent',
'sort' => 'desc',
],
[
'data' => $this
->t('To'),
],
[
'data' => $this
->t('CC'),
],
[
'data' => $this
->t('Bcc'),
],
[
'data' => $this
->t('Module'),
],
[
'data' => $this
->t('Key'),
],
[
'data' => $this
->t('Details'),
],
[
'data' => $this
->t('Send to original'),
],
[
'data' => $this
->t('Send to default mail'),
],
[
'data' => $this
->t('Delete'),
],
];
// Create the query.
/** @var \Drupal\Core\Database\Query\SelectInterface $query */
$query = $this->database
->select('mail_safety_dashboard', 'msd')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(50)
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($table_structure['header'])
->fields('msd', [
'mail_id',
'sent',
'mail',
]);
$results = $query
->execute();
// Fill the rows for the table.
$table_structure['rows'] = [];
foreach ($results as $row) {
$mail = unserialize($row->mail);
// Build the links for the row.
$view_url = Url::fromRoute('mail_safety.view', [
'mail_safety' => $row->mail_id,
]);
$details_url = Url::fromRoute('mail_safety.details', [
'mail_safety' => $row->mail_id,
]);
$send_original_url = Url::fromRoute('mail_safety.send_original', [
'mail_safety' => $row->mail_id,
]);
$send_default_url = Url::fromRoute('mail_safety.send_default', [
'mail_safety' => $row->mail_id,
]);
$delete_url = Url::fromRoute('mail_safety.delete', [
'mail_safety' => $row->mail_id,
]);
$cc = $mail['headers']['Cc'] ?? $this
->t('none');
$bcc = $mail['headers']['Bcc'] ?? $this
->t('none');
$table_structure['rows'][$row->mail_id] = [
'data' => [
Link::fromTextAndUrl($mail['subject'], $view_url),
$this->dateFormatter
->format((int) $row->sent, 'short'),
$mail['to'],
$cc,
$bcc,
$mail['module'],
$mail['key'],
Link::fromTextAndUrl($this
->t('Details'), $details_url),
Link::fromTextAndUrl($this
->t('Send to original'), $send_original_url),
Link::fromTextAndUrl($this
->t('Send to default mail'), $send_default_url),
Link::fromTextAndUrl($this
->t('Delete'), $delete_url),
],
];
}
// Let other modules change the table structure to add or remove
// information to be shown. E.g. attachments that need to be downloaded.
$this->moduleHandler
->alter('mail_safety_table_structure', $table_structure);
$form['mails']['table'] = [
'#theme' => 'table',
'#header' => $table_structure['header'],
'#rows' => $table_structure['rows'],
'#sticky' => TRUE,
'#empty' => $this
->t('No mails found'),
];
$form['mails']['pager'] = [
'#type' => 'pager',
'#tags' => [],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Validate submitted form data.
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Handle submitted form data.
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DashboardForm:: |
protected | property | The database connection. | |
DashboardForm:: |
protected | property | The date formatter. | |
DashboardForm:: |
protected | property | The module handler. | |
DashboardForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
DashboardForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
DashboardForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
DashboardForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
DashboardForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DashboardForm:: |
public | function | DashboardForm constructor. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
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. Overrides UrlGeneratorTrait:: |
|
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. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
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:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
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. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |