Skip to content
Snippets Groups Projects

Fixed Drupal calls.

2 files
+ 79
53
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 78
52
@@ -2,13 +2,15 @@
namespace Drupal\simplifying\Services;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Menu\MenuLinkTree;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Render\Markup;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
/**
* Class Toolbar.
@@ -47,6 +49,20 @@ class Toolbar {
*/
protected $currentroutematch;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Add __construct.
*
@@ -62,12 +78,18 @@ class Toolbar {
* @param \Drupal\Core\Routing\CurrentRouteMatch $currentroutematch
*
* Add CurrentRouteMatch.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(SettingsActions $settingsactions, ModuleHandler $modulehandler, MenuLinkTree $menutree, CurrentRouteMatch $currentroutematch) {
public function __construct(SettingsActions $settingsactions, ModuleHandler $modulehandler, MenuLinkTree $menutree, CurrentRouteMatch $currentroutematch, EntityTypeManagerInterface $entity_type_manager, Connection $connection) {
$this->settingsactions = $settingsactions;
$this->modulehandler = $modulehandler;
$this->menutree = $menutree;
$this->currentroutematch = $currentroutematch;
$this->entityTypeManager = $entity_type_manager;
$this->connection = $connection;
}
/**
@@ -129,7 +151,7 @@ class Toolbar {
$tree_rout = '';
$this->treeOptions($tree, $options, $tree_rout);
$defs = $this->settingsactions->getSettings('menu_links');
$form['menu_links'] = [
'#type' => 'details',
'#title' => $this->t('Menu links'),
@@ -161,10 +183,10 @@ class Toolbar {
$tree = ['system.admin' => $tree['system.admin']];
}
foreach ($tree as $rout => $val) {
$new_tree_rout = $tree_rout.' | '.$rout;
$new_tree_rout = $tree_rout . ' | ' . $rout;
if (!empty($val->link)) {
$link = $val->link;
if($link->getRouteName()){
if ($link->getRouteName()) {
$path = Url::fromRoute($link->getRouteName(), $link->getRouteParameters(), $link->getOptions())->getInternalPath();
if (!empty($path)) {
$title = $link->getTitle();
@@ -189,13 +211,13 @@ class Toolbar {
public function toolbarTabs(&$items) {
$design = $this->settingsactions->getSettings('design');
$route_name = $this->currentroutematch->getRouteName();
if($this->modulehandler->moduleExists('webform') || $this->modulehandler->moduleExists('comment')){
if ($this->modulehandler->moduleExists('webform') || $this->modulehandler->moduleExists('comment')) {
$class = ['toolbar-icon', 'toolbar-icon-simplifying_unread'];
$tab_name = $this->t('Form applications');
$items['simplifying_unread'] = [
'#type' => 'toolbar_item',
'#cache' => [ 'max-age' => 0 ],
'#cache' => ['max-age' => 0],
'#wrapper_attributes' => [
'class' => ['toolbar_simplifying_unread'],
],
@@ -204,7 +226,7 @@ class Toolbar {
'unread_menu' => [
'#theme' => 'item_list',
'#items' => [],
'#attributes' => [ 'class' => ['toolbar-menu', 'simplifying-unread-menu'] ],
'#attributes' => ['class' => ['toolbar-menu', 'simplifying-unread-menu']],
],
],
'#weight' => 1000,
@@ -212,15 +234,15 @@ class Toolbar {
'#type' => 'html_tag',
'#tag' => 'button',
'#value' => $tab_name,
'#attributes' => [ 'class' => $class ],
'#attributes' => ['class' => $class],
],
];
$unread_count = 0;
if($this->modulehandler->moduleExists('webform')){
$webforms = \Drupal::entityTypeManager()->getStorage('webform')->loadMultiple();
foreach($webforms as $webform){
$query = \Drupal::database()->select('simplifying_entity_unread', 'u');
if ($this->modulehandler->moduleExists('webform')) {
$webforms = $this->entityTypeManager->getStorage('webform')->loadMultiple();
foreach ($webforms as $webform) {
$query = $this->connection->select('simplifying_entity_unread', 'u');
$query->condition('u.entity_type', 'webform_submission');
$query->condition('u.bundle', $webform->id());
$count = $query->countQuery()->execute()->fetchField();
@@ -229,25 +251,25 @@ class Toolbar {
$class = [];
if ($route_name == 'entity.webform.results_submissions') {
$route_webform = $this->currentroutematch->getParameter('webform');
if(isset($route_webform) && $route_webform->id() == $webform->id()){
if (isset($route_webform) && $route_webform->id() == $webform->id()) {
$class[] = 'is-active';
}
}
$items['simplifying_unread']['tray']['unread_menu']['#items'][ $webform->id() ] = [
$items['simplifying_unread']['tray']['unread_menu']['#items'][$webform->id()] = [
'#type' => 'link',
'#title' => $webform->get('title').(!empty($count) ? ' ('.$count.')' : ''),
'#title' => $webform->get('title') . (!empty($count) ? ' (' . $count . ')' : ''),
'#url' => Url::fromRoute('entity.webform.results_submissions', ['webform' => $webform->id()]),
'#attributes' => [ 'class' => $class ],
'#attributes' => ['class' => $class],
'#wrapper_attributes' => [
'class' => ['menu-item', 'menu-item--'.$webform->id()],
'class' => ['menu-item', 'menu-item--' . $webform->id()],
],
];
}
ksort($items['simplifying_unread']['tray']['unread_menu']['#items']);
}
if($this->modulehandler->moduleExists('comment')){
$query = \Drupal::database()->select('simplifying_entity_unread', 'u');
if ($this->modulehandler->moduleExists('comment')) {
$query = $this->connection->select('simplifying_entity_unread', 'u');
$query->condition('u.entity_type', 'comment');
$count = $query->countQuery()->execute()->fetchField();
$unread_count += $count;
@@ -258,28 +280,28 @@ class Toolbar {
}
$items['simplifying_unread']['tray']['unread_menu']['#items']['comment'] = [
'#type' => 'link',
'#title' => $this->t('Comments').(!empty($count) ? ' ('.$count.')' : ''),
'#title' => $this->t('Comments') . (!empty($count) ? ' (' . $count . ')' : ''),
'#url' => Url::fromRoute('comment.admin'),
'#attributes' => [ 'class' => $class ],
'#attributes' => ['class' => $class],
'#wrapper_attributes' => [
'class' => ['menu-item', 'menu-item--comment'],
],
];
}
if(!empty($unread_count)){
$items['simplifying_unread']['tab']['#value'] .= ' ('.$unread_count.')';
if (!empty($unread_count)) {
$items['simplifying_unread']['tab']['#value'] .= ' (' . $unread_count . ')';
}
}
if (!file_exists('public://simplifying-ui.css')) {
$this->createCss($design);
}
$class = ['toolbar-icon', 'toolbar-icon-simplifying_switch'];
$tab_name = $this->t('Glyanec');
$items['simplifying'] = [
'#type' => 'toolbar_item',
'#cache' => [ 'max-age' => 0 ],
'#cache' => ['max-age' => 0],
'#wrapper_attributes' => [
'class' => ['toolbar_simplifying', 'toolbar_simplifying_switch'],
],
@@ -288,13 +310,13 @@ class Toolbar {
'simplifying_menu' => [
'#theme' => 'item_list',
'#items' => [],
'#attributes' => [ 'class' => ['toolbar-menu', 'simplifying-menu'] ],
'#attributes' => ['class' => ['toolbar-menu', 'simplifying-menu']],
],
],
'#attached' => [
'library' => [
'simplifying/simplifying_ui',
'simplifying/simplifying'
'simplifying/simplifying',
],
],
'#weight' => 1001,
@@ -302,7 +324,7 @@ class Toolbar {
'#type' => 'html_tag',
'#tag' => 'button',
'#value' => $tab_name,
'#attributes' => [ 'class' => $class ],
'#attributes' => ['class' => $class],
],
];
@@ -323,9 +345,10 @@ class Toolbar {
if (!empty($_COOKIE['simplifying'])) {
$class[] = 'is-active';
// $text = $this->t('Small administration');
$text .= ' ('.$this->t('on').')';
}else{
$text .= ' ('.$this->t('off').')';
$text .= ' (' . $this->t('on') . ')';
}
else {
$text .= ' (' . $this->t('off') . ')';
}
$items['simplifying']['tray']['simplifying_menu']['#items']['minify'] = [
'#type' => 'link',
@@ -400,9 +423,9 @@ class Toolbar {
*/
public function entityInsert($entity) {
$entity_type_id = $entity->getEntityTypeId();
if($entity_type_id == 'webform_submission' || $entity_type_id == 'comment'){
if ($entity_type_id == 'webform_submission' || $entity_type_id == 'comment') {
$entity_bundle = $entity->bundle();
$query = \Drupal::database()->insert('simplifying_entity_unread');
$query = $this->connection->insert('simplifying_entity_unread');
$query->fields([
'entity_type' => $entity_type_id,
'bundle' => $entity_bundle,
@@ -416,7 +439,7 @@ class Toolbar {
* Entity delete actions.
*/
public function entityDelete($entity_type, $bundle, $entity_id) {
$query = \Drupal::database()->delete('simplifying_entity_unread');
$query = $this->connection->delete('simplifying_entity_unread');
$query->condition('entity_type', $entity_type);
$query->condition('bundle', $bundle);
$query->condition('entity_id', $entity_id);
@@ -427,33 +450,36 @@ class Toolbar {
* Entity Reading actions.
*/
public function entityReadingRoute() {
switch($this->currentroutematch->getRouteName()){
switch ($this->currentroutematch->getRouteName()) {
case 'entity.webform.results_submissions':
$webform = $this->currentroutematch->getParameter('webform');
if(!empty($webform)){
$query = \Drupal::database()->delete('simplifying_entity_unread');
if (!empty($webform)) {
$query = $this->connection->delete('simplifying_entity_unread');
$query->condition('entity_type', 'webform_submission');
$query->condition('bundle', $webform->id());
$query->execute();
}
break;
break;
case 'comment.admin':
case 'comment.admin_approval':
$query = \Drupal::database()->delete('simplifying_entity_unread');
$query = $this->connection->delete('simplifying_entity_unread');
$query->condition('entity_type', 'comment');
$query->execute();
break;
break;
}
}
/**
* Create css.
*/
public function createCss($design = []) {
$css = ':root {'.PHP_EOL;
$css .= ' --topBackground: '.($design['top_background'] ?? '#3A9F00').';'.PHP_EOL;
$css .= ' --topColor: '.($design['top_color'] ?? '#000000').';'.PHP_EOL;
$css .= ' --submenuBackground: '.($design['submenu_background'] ?? '#555555').';'.PHP_EOL;
$css .= '}'.PHP_EOL;
$css = ':root {' . PHP_EOL;
$css .= ' --topBackground: ' . ($design['top_background'] ?? '#3A9F00') . ';' . PHP_EOL;
$css .= ' --topColor: ' . ($design['top_color'] ?? '#000000') . ';' . PHP_EOL;
$css .= ' --submenuBackground: ' . ($design['submenu_background'] ?? '#555555') . ';' . PHP_EOL;
$css .= '}' . PHP_EOL;
file_put_contents('public://simplifying-ui.css', $css);
}
}
Loading