View source
<?php
namespace Drupal\content_moderation_notifications\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class ContentModerationNotificationsFormBase extends EntityForm {
public static function updateWorkflowTransitions(array $form, FormStateInterface &$form_state) {
return $form['transitions_wrapper'];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$workflows = $this->entityTypeManager
->getStorage('workflow')
->loadMultiple();
if (empty($workflows)) {
$form['no_workflows'] = [
'#type' => 'markup',
'#markup' => $this
->t('No workflows available. <a href=":url">Manage workflows</a>.', [
':url' => Url::fromRoute('entity.workflow.collection')
->toString(),
]),
];
return $form;
}
$form = parent::buildForm($form, $form_state);
$content_moderation_notification = $this->entity;
$workflow_options = [];
foreach ($workflows as $workflow_id => $workflow) {
$workflow_options[$workflow_id] = $workflow
->label();
}
$workflow_keys = array_keys($workflow_options);
if ($form_state
->getValue('workflow')) {
$selected_workflow = $form_state
->getValue('workflow');
}
elseif (isset($content_moderation_notification->workflow)) {
$selected_workflow = $content_moderation_notification->workflow;
}
else {
$selected_workflow = array_shift($workflow_keys);
}
$form['label'] = [
'#title' => $this
->t('Label'),
'#type' => 'textfield',
'#default_value' => $content_moderation_notification
->label(),
'#description' => $this
->t('The label for this notification.'),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Machine name'),
'#default_value' => $content_moderation_notification
->id(),
'#machine_name' => [
'exists' => [
$this,
'exists',
],
'source' => [
'label',
],
],
'#disabled' => !$content_moderation_notification
->isNew(),
];
$form['workflow'] = [
'#type' => 'select',
'#title' => $this
->t('Workflow'),
'#options' => $workflow_options,
'#default_value' => $selected_workflow,
'#required' => TRUE,
'#description' => $this
->t('Select a workflow'),
'#ajax' => [
'wrapper' => 'workflow_transitions_wrapper',
'callback' => static::class . '::updateWorkflowTransitions',
],
];
$form['transitions_wrapper'] = [
'#type' => 'container',
'#prefix' => '<div id="workflow_transitions_wrapper">',
'#suffix' => '</div>',
];
$state_transitions_options = [];
$state_transitions = $workflows[$selected_workflow]
->getTypePlugin()
->getTransitions();
foreach ($state_transitions as $key => $transition) {
$state_transitions_options[$key] = $transition
->label();
}
$form['transitions_wrapper']['transitions'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Transitions'),
'#options' => $state_transitions_options,
'#default_value' => isset($content_moderation_notification->transitions) ? $content_moderation_notification->transitions : [],
'#required' => TRUE,
'#description' => $this
->t('Select which transitions triggers this notification.'),
];
$roles_options = user_role_names(TRUE);
$form['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Roles'),
'#options' => $roles_options,
'#default_value' => $content_moderation_notification
->getRoleIds(),
'#description' => $this
->t('Send notifications to all users with these roles.'),
];
$form['author'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Email the author?'),
'#default_value' => $content_moderation_notification
->sendToAuthor(),
'#description' => $this
->t('Send notifications to the current author of the content.'),
];
$form['site_mail'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Disable the site email address'),
'#default_value' => $content_moderation_notification
->disableSiteMail(),
'#description' => $this
->t('Do not send notifications to the site email address.'),
];
$form['emails'] = [
'#type' => 'textarea',
'#title' => $this
->t('Adhoc email addresses'),
'#default_value' => $content_moderation_notification
->getEmails(),
'#description' => $this
->t('Send notifications to these email addresses, emails should be entered as a comma separated list and optionally on separate lines.'),
];
$form['subject'] = [
'#type' => 'textfield',
'#title' => $this
->t('Email Subject'),
'#default_value' => $content_moderation_notification
->getSubject(),
'#required' => TRUE,
'#maxlength' => 1024,
];
$form['body'] = [
'#type' => 'text_format',
'#format' => $content_moderation_notification
->getMessageFormat() ?: filter_default_format(),
'#title' => $this
->t('Email Body'),
'#default_value' => $content_moderation_notification
->getMessage(),
];
if ($this->moduleHandler
->moduleExists('token')) {
$form['body']['token_tree_link'] = [
'#theme' => 'token_tree_link',
'#token_types' => array_unique([
'user',
$selected_workflow,
'node',
]),
'#weight' => 10,
];
}
return $form;
}
public function exists($entity_id, array $element, FormStateInterface $form_state) {
$query = $this->entityTypeManager
->getStorage('content_moderation_notification')
->getQuery();
$result = $query
->condition('id', $element['#field_prefix'] . $entity_id)
->execute();
return (bool) $result;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save');
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$content_moderation_notification = $this
->getEntity();
$status = $content_moderation_notification
->save();
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addMessage($this
->t('Notification <a href=":url">%label</a> has been updated.', [
'%label' => $content_moderation_notification
->label(),
':url' => $content_moderation_notification
->toUrl('edit-form')
->toString(),
]));
$this
->logger('content_moderation_notifications')
->notice('Notification has been updated.', [
'%label' => $content_moderation_notification
->label(),
]);
}
else {
$this
->messenger()
->addMessage($this
->t('Notification <a href=":url">%label</a> has been added.', [
'%label' => $content_moderation_notification
->label(),
':url' => $content_moderation_notification
->toUrl('edit-form')
->toString(),
]));
$this
->logger('content_moderation_notifications')
->notice('Notification has been added.', [
'%label' => $content_moderation_notification
->label(),
]);
}
$form_state
->setRedirect('entity.content_moderation_notification.collection');
}
}