inline_feedback-1.0.x-dev/inline_feedback.module
inline_feedback.module
<?php
declare(strict_types=1);
/**
* @file
* Provides an inline feedback entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_theme().
*/
function inline_feedback_theme(): array {
return [
'inline_feedback' => ['render element' => 'elements'],
];
}
/**
* Prepares variables for inline feedback templates.
*
* Default template: inline-feedback.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the inline feedback information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_inline_feedback(array &$variables): void {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function inline_feedback_user_cancel($edit, UserInterface $account, $method): void {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish inline feedbacks.
$storage = \Drupal::entityTypeManager()->getStorage('inline_feedback');
$inline_feedback_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($inline_feedback_ids) as $inline_feedback) {
$inline_feedback->set('status', FALSE)->save();
}
break;
case 'user_cancel_reassign':
// Anonymize inline feedbacks.
$storage = \Drupal::entityTypeManager()->getStorage('inline_feedback');
$inline_feedback_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
foreach ($storage->loadMultiple($inline_feedback_ids) as $inline_feedback) {
$inline_feedback->setOwnerId(0)->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function inline_feedback_user_predelete(UserInterface $account): void {
// Delete inline feedbacks that belong to this account.
$storage = \Drupal::entityTypeManager()->getStorage('inline_feedback');
$inline_feedback_ids = $storage->getQuery()
->condition('uid', $account->id())
->accessCheck(FALSE)
->execute();
$storage->delete(
$storage->loadMultiple($inline_feedback_ids)
);
}
/**
* Implements hook_page_attachments().
*/
function inline_feedback_page_attachments(array &$attachments) {
$current_user = \Drupal::currentUser();
$config = \Drupal::config('inline_feedback.settings');
$allowed_roles_to_see = $config->get('$allowed_roles_to_see') ?? [];
// check if current user has allowed rol
$user_roles = $current_user->getRoles();
$has_access = array_intersect($allowed_roles_to_see, $user_roles);
if (empty($has_access)) {
return;
}
// node data
$route_match = \Drupal::routeMatch();
$node = $route_match->getParameter('node');
// Loads attachments
if ($node instanceof NodeInterface) {
$nid = $node->id();
$attachments['#attached']['library'][] = 'inline_feedback/feedback';
$attachments['#attached']['drupalSettings']['node']['nid'] = $nid;
$attachments['#attached']['library'][] = 'inline_feedback/toastify';
$attachments['#attached']['library'][] = 'inline_feedback/fontawesome';
// if there are feedbacks in the current node
$feedbacks = \Drupal::entityTypeManager()->getStorage('inline_feedback')->loadByProperties([
'node' => $nid
]);
if (!empty($nid)) {
$feedback_data = [];
foreach ($feedbacks as $feedback) {
/** @var \Drupal\inline_feedback\Entity\InlineFeedback $feedback */
$author = $feedback->getOwner();
$feedback_data[] = [
'id' => $feedback->id(),
'label' => $feedback->label(),
'description' => $feedback->get('description')->value,
'selector' => $feedback->get('selector')->value,
'author' => $author ? $author->getDisplayName() : 'Anonymous',
'authored_on' => $feedback->get('created')->value,
];
}
$attachments['#attached']['drupalSettings']['inline_feedback']['feedbacks'] = $feedback_data;
$attachments['#attached']['drupalSettings']['inline_feedback']['styles'] = [
'background' => $config->get('background_color') ?: '#2b3c82',
'color' => $config->get('text_color') ?: '#FFFFFF',
];
$allowed_to_delete = FALSE;
$allowed_roles_to_delete = $config->get('allowed_roles_to_delete') ?? [];
$has_permission = array_intersect($allowed_roles_to_delete, $user_roles);
if (!empty($has_permission)) {
$allowed_to_delete = TRUE;
}
$attachments['#attached']['drupalSettings']['inline_feedback']['allowed_to_delete'] = $allowed_to_delete;
}
}
}
