siteimprove.module in Siteimprove 8
Same filename and directory in other branches
Drupal module: Siteimprove Plugin.
File
siteimprove.moduleView source
<?php
/**
* @file
* Drupal module: Siteimprove Plugin.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\NestedArray;
/**
* Implements hook_toolbar().
*
* Only show prepublish action for enabled taxonomies and content types.
*/
function siteimprove_toolbar() {
$user = \Drupal::currentUser();
if (!$user
->hasPermission('use siteimprove')) {
return [];
}
$node = \Drupal::routeMatch()
->getParameter('node');
$taxonomy = \Drupal::routeMatch()
->getParameter('taxonomy_term');
$bundle = '';
//In event node parameter is not a node object.
if (is_string($node)) {
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($node);
}
// In event taxonomy parameter is not an Term object.
if (is_string($taxonomy)) {
$taxonomy = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->load($taxonomy);
}
$urls = \Drupal::service('siteimprove.utils')
->getEntityUrls($node ? $node : $taxonomy);
$toolbar_items = [];
$toolbar = [];
// Get bundle from either node or taxonomy.
if (is_object($node) && $node instanceof \Drupal\node\Entity\Node) {
$bundle = $node
->bundle();
}
elseif (is_object($taxonomy) && $taxonomy instanceof \Drupal\taxonomy\Entity\Term) {
$bundle = $taxonomy
->bundle();
}
/**
* Prepublish toolbar item.
*/
$prepublish_access = $user
->hasPermission('use siteimprove prepublish');
$prepublish_available = FALSE;
$config = \Drupal::service('config.factory')
->get('siteimprove.settings');
$prepublish_enabled = $config
->get('prepublish_enabled');
$enabled_content_types = $config
->get('enabled_content_types');
$enabled_taxonomies = $config
->get('enabled_taxonomies');
$enabled_route_names = [
'entity.node.canonical',
'entity.node.latest_version',
'entity.taxonomy_term.canonical',
'entity.taxonomy_term.latest_version',
];
$current_route_name = \Drupal::routeMatch()
->getRouteName();
// Prepublish should be available for an enabled taxonomy or content type.
if ($prepublish_enabled && in_array($current_route_name, $enabled_route_names) && ($node && $enabled_content_types[$bundle] || $taxonomy && $enabled_taxonomies[$bundle])) {
$prepublish_available = TRUE;
}
// Add prepublish action if enabled.
if ($prepublish_access && $prepublish_available) {
$toolbar_items['siteimprove_toolbar']['tray']['actions']['#items']['prepublish'] = [
'#type' => 'link',
'#title' => 'Prepublish check',
'#url' => Url::fromUserInput('#'),
'#attributes' => [
'class' => [
'siteimprove-contentcheck-button',
],
],
'#wrapper_attributes' => [
'class' => [
'menu-item',
],
],
];
}
/**
* Recheck toolbar item.
*/
$recheck_access = $user
->hasPermission('use siteimprove');
$enabled_route_names = [
'entity.node.canonical',
'entity.node.edit_form',
'entity.taxonomy_term.canonical',
'entity.taxonomy_term.edit_form',
];
$current_route_name = \Drupal::routeMatch()
->getRouteName();
if ($recheck_access && in_array($current_route_name, $enabled_route_names)) {
$toolbar_items['siteimprove_toolbar']['tray']['actions']['#items']['recheck'] = [
'#type' => 'link',
'#title' => 'Recheck',
'#url' => Url::fromUserInput('#'),
'#attributes' => [
'class' => [
'siteimprove-recheck-button',
],
],
'#wrapper_attributes' => [
'class' => [
'menu-item',
],
],
];
$toolbar_items['siteimprove_toolbar']['tray']['#attached']['drupalSettings']['siteimprove']['recheck'] = \Drupal::service('siteimprove.utils')
->getSiteimproveSettings($urls, 'recheck', FALSE);
}
/**
* Settings page toolbar item.
*/
if ($user
->hasPermission('administer siteimprove')) {
$toolbar_items['siteimprove_toolbar']['tray']['actions']['#items']['settings'] = [
'#type' => 'link',
'#title' => 'Configuration',
'#url' => Url::fromRoute('siteimprove.settings_form'),
'#wrapper_attributes' => [
'class' => [
'menu-item',
],
],
];
}
// Add Siteimprove toolbar items.
if (!empty($toolbar_items)) {
$toolbar_basic = _siteimprove_toolbar_basic();
$toolbar = NestedArray::mergeDeep($toolbar_basic, $toolbar_items);
}
// Current entity or null.
$current_entity = $node ? $node : ($taxonomy ? $taxonomy : null);
// Add current viewed entity as cacheable dependency.
$renderer = \Drupal::service('renderer');
if ($current_entity) {
$renderer
->addCacheableDependency($toolbar, $current_entity);
}
return $toolbar;
}
/**
* Basic toolbar array.
*
* @return array[]
*/
function _siteimprove_toolbar_basic() {
return [
'siteimprove_toolbar' => [
'#weight' => 500,
'#attached' => [
'library' => 'siteimprove/siteimprove.toolbar',
],
'#cache' => [
'contexts' => [
'user.permissions',
'url.path',
],
'tags' => [
'siteimprove_toolbar',
],
],
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'link',
'#title' => 'Siteimprove',
'#url' => Url::fromUserInput('#'),
'#attributes' => [
'class' => [
'siteimprove-link',
],
],
],
'#wrapper_attributes' => [
'class' => [
'siteimprove-toolbar',
],
],
'tray' => [
'#heading' => 'Siteimprove',
'actions' => [
'#theme' => 'item_list',
'#items' => [],
'#attributes' => [
'class' => [
'toolbar-menu',
],
],
],
'urls' => [],
'empty' => [],
],
],
];
}
/**
* Implements hook_entity_insert().
*/
function siteimprove_entity_insert(EntityInterface $entity) {
_siteimprove_entity_save($entity);
}
/**
* Implements hook_entity_update().
*/
function siteimprove_entity_update(EntityInterface $entity) {
_siteimprove_entity_save($entity);
}
/**
* Entity save common method.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity being saved.
*/
function _siteimprove_entity_save(EntityInterface $entity) {
if (Drupal::currentUser()
->hasPermission('use siteimprove')) {
switch ($entity
->getEntityTypeId()) {
case 'node':
if (!empty($entity
->get('status')->value)) {
\Drupal::service('siteimprove.utils')
->setSessionUrl($entity);
}
break;
case 'taxonomy_term':
\Drupal::service('siteimprove.utils')
->setSessionUrl($entity);
break;
}
}
}
/**
* Implements hook_page_attachments().
*/
function siteimprove_page_attachments(array &$attachments) {
// Check if user has access.
if (\Drupal::currentUser()
->hasPermission('use siteimprove')) {
// Add Siteimprove js.
$siteimprove = \Drupal::service('siteimprove.utils');
$attachments['#attached']['drupalSettings']['siteimprove']['token'] = $siteimprove
->getSiteimproveToken();
$attachments['#attached']['library'][] = $siteimprove
->getSiteimproveOverlayLibrary();
$attachments['#attached']['library'][] = $siteimprove
->getSiteimproveLibrary();
// Get the active frontend domain plugin.
$config = \Drupal::service('config.factory')
->get('siteimprove.settings');
// If node pages or taxonomy term pages, add input method, else domain method.
$enabled_route_names = [
'entity.node.canonical',
'entity.node.latest_version',
'entity.node.edit_form',
'entity.taxonomy_term.canonical',
'entity.taxonomy_term.latest_version',
'entity.taxonomy_term.edit_form',
];
$route_match = \Drupal::routeMatch();
$current_route_name = $route_match
->getRouteName();
if (in_array($current_route_name, $enabled_route_names)) {
$method = 'input';
}
else {
$method = 'domain';
}
$type = '';
$bundle = '';
$parameters = $route_match
->getParameters();
foreach ($parameters as $parameter) {
if (is_object($parameter) && $parameter instanceof \Drupal\node\Entity\Node) {
$type = 'enabled_content_types';
$bundle = $parameter
->bundle();
}
elseif (is_object($parameter) && $parameter instanceof \Drupal\taxonomy\Entity\Term) {
$type = 'enabled_taxonomies';
$bundle = $parameter
->bundle();
}
}
$conf = $type ? $config
->get($type) : [];
$enabled = $type && isset($conf[$bundle]) && $conf[$bundle];
if (\Drupal::currentUser()
->hasPermission('use siteimprove prepublish') && $bundle && $enabled) {
$attachments['#attached']['library'][] = 'siteimprove/siteimprove.prepublish';
}
if (!empty($method)) {
$parameters = \Drupal::routeMatch()
->getParameters();
foreach ($parameters as $param) {
if (is_object($param) && $param instanceof \Drupal\Core\Entity\ContentEntityInterface) {
if ($param->in_preview !== TRUE) {
/** @var \Drupal\Core\Entity\ContentEntityType $type */
$entity_type = $param
->getEntityType()
->id();
$id = $param
->id();
$entity = \drupal::entityTypeManager()
->getStorage($entity_type)
->load($id);
$urls = \drupal::service('siteimprove.utils')
->getEntityUrls($entity);
$attachments['#attached']['drupalSettings']['siteimprove'][$method] = $siteimprove
->getSiteimproveSettings($urls, $method);
}
}
}
}
// If siteimprove_url exists in SESSION, send to Siteimprove.
if (!empty($_SESSION['siteimprove_url'])) {
$urls = $_SESSION['siteimprove_url'];
$method = 'recheck';
$attachments['#attached']['drupalSettings']['siteimprove'][$method] = $siteimprove
->getSiteimproveSettings($urls, $method);
unset($_SESSION['siteimprove_url']);
}
}
}
Functions
Name | Description |
---|---|
siteimprove_entity_insert | Implements hook_entity_insert(). |
siteimprove_entity_update | Implements hook_entity_update(). |
siteimprove_page_attachments | Implements hook_page_attachments(). |
siteimprove_toolbar | Implements hook_toolbar(). |
_siteimprove_entity_save | Entity save common method. |
_siteimprove_toolbar_basic | Basic toolbar array. |