Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

[OS-25] Google translate improvements #1057

Merged
merged 2 commits into from May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/custom/openy_gtranslate/openy_gtranslate.info.yml
Expand Up @@ -2,5 +2,5 @@ name: OpenY Google Translate
type: module
description: OpenY module for Google Translate integration.
core: 8.x
version: 8.0.1
version: 8.0.2
package: OpenY
25 changes: 24 additions & 1 deletion modules/custom/openy_gtranslate/openy_gtranslate.install
Expand Up @@ -12,7 +12,7 @@ use Drupal\menu_link_content\Entity\MenuLinkContent;
*/
function openy_gtranslate_install() {
$config = \Drupal::config('system.theme');
$menuName = ($config->get('default') == 'openy_rose') ? 'account' : 'main';
$menuName = (!empty($config->get('default')) && $config->get('default') == 'openy_lily') ? 'main' : 'account';
$menuLink = MenuLinkContent::create([
'title' => t('Language'),
'link' => [
Expand All @@ -28,3 +28,26 @@ function openy_gtranslate_install() {
]);
$menuLink->save();
}

/**
* Implements hook_uninstall().
*/
function openy_gtranslate_uninstall() {
$config = \Drupal::config('system.theme');
$menuName = (!empty($config->get('default')) && $config->get('default') == 'openy_lily') ? 'main' : 'account';

$database = \Drupal::database();
$query = $database->select('menu_link_content_data', 'm');
$query->condition('bundle', 'menu_link_content');
$query->condition('enabled', 1);
$query->condition('m.menu_name', $menuName);
$query->condition('title', "%Language%", 'LIKE');
$query->condition('link__options', "%language%", 'LIKE');
$query->fields('m', ['id']);
$res = $query->execute()->fetchField();
// Delete Language link.
if (!empty($res)) {
$menuLink = MenuLinkContent::load($res);
$menuLink->delete();
}
}
7 changes: 7 additions & 0 deletions modules/custom/openy_gtranslate/openy_gtranslate.services.yml
@@ -0,0 +1,7 @@
services:
openy_gtranslate_events_subscriber:
class: '\Drupal\openy_gtranslate\EventSubscriber\ConfigEventSubscriber'
arguments:
- '@database'
tags:
- { name: 'event_subscriber' }
@@ -0,0 +1,99 @@
<?php

namespace Drupal\openy_gtranslate\EventSubscriber;

use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Database\Connection;

/**
* Class ConfigEventsSubscriber to react on changing default theme.
*/
class ConfigEventSubscriber implements EventSubscriberInterface {

/**
* The Connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* ConfigEventSubscriber constructor.
*
* @param \Drupal\Core\Database\Connection $connection
* Connection.
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
}

/**
* {@inheritdoc}
*
* @return array
* The event names to listen for, and the methods that should be executed.
*/
public static function getSubscribedEvents() {
return [
ConfigEvents::SAVE => 'configSave',
];
}

/**
* React to a config object being saved.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* Config crud event.
*/
public function configSave(ConfigCrudEvent $event) {
$config = $event->getConfig();
$original = $config->getOriginal();
// Skip new and not theme config.
if (empty($original) || ($config->getName() !== 'system.theme')) {
return;
}
$originalTheme = $original['default'];
$updatedTheme = $config->get('default');

// Act only with default theme changes.
if ($originalTheme == $updatedTheme || !in_array($updatedTheme, ['openy_rose', 'openy_lily'])) {
return;
}

// Delete previous theme menu link.
$originalMenuName = ($originalTheme == 'openy_lily') ? 'main' : 'account';
$query = $this->connection->select('menu_link_content_data', 'm');
$query->condition('bundle', 'menu_link_content');
$query->condition('enabled', 1);
$query->condition('m.menu_name', $originalMenuName);
$query->condition('title', "%Language%", 'LIKE');
$query->condition('link__options', "%language%", 'LIKE');
$query->fields('m', ['id']);
$res = $query->execute()->fetchField();
if (!empty($res)) {
$menuLink = MenuLinkContent::load($res);
$menuLink->delete();
}

// Create new menu link.
$updatedMenuName = ($updatedTheme == 'openy_lily') ? 'main' : 'account';
$menuLink = MenuLinkContent::create([
'title' => t('Language'),
'link' => [
'uri' => 'internal:/',
'options' => [
'attributes' => [
'class' => ['language hidden-md hidden-lg'],
],
],
],
'menu_name' => $updatedMenuName,
'weight' => 50,
]);
$menuLink->save();
}

}