MailListController.php in Mail Editor 8
File
src/Controller/MailListController.php
View source
<?php
namespace Drupal\mail_edit\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\Xss;
class MailListController extends ControllerBase {
public function listAll() {
$render = [
'#theme' => 'table',
'#attributes' => [],
'#header' => [
$this
->t('Module'),
$this
->t('Description'),
$this
->t('Length (characters)'),
$this
->t('Actions'),
],
'#rows' => [],
];
foreach ($this
->getAllTemplates() as $config_name => $templates) {
$config_label = $config_name;
if ($config_name == 'user.mail') {
$config_label = t('Drupal core');
}
foreach ($templates as $name => $data) {
$body_length = mb_strlen($data['body']);
if ($body_length === 0) {
$body_length = $this
->t('empty');
}
$args = [
'id' => $config_name . '.' . $name,
];
$render['#rows'][] = [
$config_label,
Xss::filter($data['description']),
$body_length,
Link::createFromRoute($this
->t('Edit'), 'mail_edit.edit', $args),
];
}
}
return $render;
}
private function getAllTemplates() {
$all_templates = [];
$module_handler = \Drupal::moduleHandler();
foreach ($module_handler
->invokeAll('mail_edit_templates') as $config_name => $templates) {
$config_data = $this
->config($config_name)
->getRawData();
if (empty($config_data)) {
$config_data = [];
}
foreach ($templates as $key => $label) {
if (is_numeric($key)) {
$key = $label;
}
if (!isset($config_data[$key])) {
$config_data[$key] = [
'subject' => '',
'body' => '',
];
}
$data = $config_data[$key];
if ($key != $label) {
$data['description'] = $label;
}
else {
$data['description'] = $data['subject'];
}
$all_templates[$config_name][$key] = $data;
}
}
$module_handler
->alter('mail_edit_templates_list', $all_templates);
return $all_templates;
}
}