全部!
我正在尝试返回基于附加到它的 HTML 的格式化消息,这是我在模块内创建的一个库,用于在消息内容中设置表格样式,并且消息本身基于主题。这是我做的
Drupal->modules->custom->mailify->css->my_library.css
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
Drupal->modules->custom->mailify->mailify.libraries.yml
mailify-library:
version: 1.x
css:
header: true
theme:
css/my_library.css: {}
Drupal->modules->custom->mailify->templates->mailify-hello-world-salutation.html.twig
<div {{ attributes }}>
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<tr>
<td>Host</td>
<td>{{ host }}</td>
</tr>
<tr>
<td>SMTPAuth</td>
<td> {{ smtpauth }} </td>
</tr>
<tr>
<td>Username</td>
<td>{{ username }}</td>
</tr>
<tr>
<td>Password</td>
<td>{{ password }}</td>
</tr>
<tr>
<td>Port</td>
<td> {{ port }}</td>
</tr>
</table>
</body>
</div>
Drupal->modules->custom->mailify->mailify.module
/**
* Implements hook_theme().
*/
function mailify_theme($existing, $type, $theme, $path)
{
return [
'mailify_hello_world_salutation' => [
'variables' => [
'host' => NULL,
'smtpauth' => NULL,
'username' => NULL,
'password' => NULL,
'port' => NULL,
'overridden' => FALSE
]
]
];
}
/**
* Default preprocessor function for the mailify_hello_world_salutation theme hook.
*/
function template_preprocess_mailify_hello_world_salutation(&$variables)
{
$variables['attributes'] = [
'class' => [
'mailify'
]
];
}
Drupal->modules->custom->mailify->src->Plugin->rest->resource->RestSample.php
class RestSample extends ResourceBase
{
/**
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a Drupal\Plugin\rest\resource\RestSample object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
*
*/
public function __construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger, $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('rest'),
$container->get('config.factory'),
);
}
public function post()
{
// You must to implement the logic of your REST Resource here.
$render = [
'#theme' => 'mailify_hello_world_salutation'
'#attached' => [
'library' => [
'mailify/mailify-library',
],
],
];
$to = 'joe@example.net';
$langcode = $this->configFactory->get('system.site')->get('langcode');
$config = $this->configFactory->get('mailify.custom_mailable');
$render['#host'] = $config->get('host');
$render['#smtpauth'] = $config->get('smtpauth');
$render['#username'] = $config->get('username');
$render['#password'] = $config->get('password');
$render['#port'] = $config->get('port');
try {
$result = \Drupal::service('plugin.manager.mail')->mail('mailify', 'mailgenix', $to, $langcode, [
'message' => \Drupal::service('renderer')->renderRoot($render),
'subject' => 'postmail'
]);
$response = null;
$data = null;
if ($result) {
$data[]['message'] = 'Message has been sent';
} else {
$data[]['message'] = 'Message could not be sent';
$data[]['status'] = 400;
}
$response = new ResourceResponse($data[0], $data[1]['status'] ?? 200);
// In order to generate fresh result every time (without clearing
// the cache), you need to invalidate the cache.
$response->addCacheableDependency($data);
return $response;
} catch (\Exception $e) {
$response = new ResourceResponse("Message could not be sent. Mailer Error: " . $e->getMessage(), 500);
$response->addCacheableDependency($data);
return $response;
}
}
}
我首先使用了函数renderPlain然后renderRoot并且两次消息都返回了未设置样式。请注意,当我单独使用渲染函数时,我收到一个错误,提示我过早地尝试渲染。有人可以帮我找出原因吗?我对库的定义是否有错误,或者我应该为渲染器使用另一个函数?
注意:我肯定使用了自定义插件RestSample上的注解