Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #20 from Pronovix/issue/8015
Browse files Browse the repository at this point in the history
Create user greeting service
  • Loading branch information
andretothie authored Apr 9, 2020
2 parents 1618fbe + 7709710 commit db708a3
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/sync/block.block.subtheme_videogame_breadcrumbs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _core:
id: subtheme_videogame_breadcrumbs
theme: subtheme_videogame
region: header
weight: -100
weight: -7
provider: null
plugin: system_breadcrumb_block
settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _core:
id: subtheme_videogame_local_actions
theme: subtheme_videogame
region: header
weight: 20
weight: -3
provider: null
plugin: local_actions_block
settings:
Expand Down
2 changes: 1 addition & 1 deletion config/sync/block.block.subtheme_videogame_local_tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _core:
id: subtheme_videogame_local_tasks
theme: subtheme_videogame
region: header
weight: 10
weight: -4
provider: null
plugin: local_tasks_block
settings:
Expand Down
2 changes: 1 addition & 1 deletion config/sync/block.block.subtheme_videogame_messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ _core:
id: subtheme_videogame_messages
theme: subtheme_videogame
region: header
weight: -50
weight: -6
provider: null
plugin: system_messages_block
settings:
Expand Down
2 changes: 1 addition & 1 deletion config/sync/block.block.subtheme_videogame_page_title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _core:
id: subtheme_videogame_page_title
theme: subtheme_videogame
region: header
weight: 0
weight: -5
provider: null
plugin: page_title_block
settings:
Expand Down
20 changes: 20 additions & 0 deletions config/sync/block.block.usertimezoneblock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
uuid: 79adec60-f230-402c-a160-e0e421b15b2f
langcode: en
status: true
dependencies:
module:
- user_timezone
theme:
- subtheme_videogame
id: usertimezoneblock
theme: subtheme_videogame
region: header
weight: -2
provider: null
plugin: user_timezone_block
settings:
id: user_timezone_block
label: 'User timezone block'
provider: user_timezone
label_display: visible
visibility: { }
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module:
tour: 0
update: 0
user: 0
user_timezone: 0
views_ui: 0
menu_link_content: 1
pathauto: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types = 1);

namespace Drupal\user_timezone\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\user_timezone\UserTimezoneSalutation;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a UserTimezone block.
*
* @Block(
* id = "user_timezone_block",
* admin_label = @Translation("User timezone block"),
* )
*/
class UserTimezoneBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The salutation.
*
* @var \Drupal\user_timezone\UserTimezoneSalutation
*/
protected $salutation;

/**
* Constructs the UserTimezoneBlock object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\user_timezone\UserTimezoneSalutation $salutation
* The salutation.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, UserTimezoneSalutation $salutation) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->salutation = $salutation;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('user_timezone.salutation')
);
}

/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$build['#cache']['max-age'] = 0;
$build['#cache']['tags'] = 'user_timezone';

return [
'#markup' => $this->salutation->getSalutation()->render(),
];
}

}
77 changes: 77 additions & 0 deletions web/modules/custom/user_timezone/src/UserTimezoneSalutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types = 1);

namespace Drupal\user_timezone;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Prepares user greeting.
*/
class UserTimezoneSalutation {
use StringTranslationTrait;

/**
* The current user service.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;

/**
* {@inheritdoc}
*/
public function __construct(AccountProxyInterface $currentUser, TimeInterface $time, TranslationInterface $stringTranslation) {
$this->currentUser = $currentUser;
$this->time = $time;
$this->stringTranslation = $stringTranslation;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user')
);
}

/**
* {@inheritdoc}
*/
public function getSalutation() {
$time = (int) date('G', $this->time->getRequestTime());

if ($time >= 06 && $time < 12) {
$salutation = $this->t('Good morning, %username!', [
'%username' => $this->currentUser->getAccountName(),
]);
}

elseif ($time >= 12 && $time < 18) {
$salutation = $this->t('Good afternoon, %username!', [
'%username' => $this->currentUser->getAccountName(),
]);
}

elseif ($time >= 18 && $time < 24) {
$salutation = $this->t('Good evening, %username!', [
'%username' => $this->currentUser->getAccountName(),
]);
}

else {
$salutation = $this->t('Good night, %username!', [
'%username' => $this->currentUser->getAccountName(),
]);
}

return $salutation;
}

}
6 changes: 6 additions & 0 deletions web/modules/custom/user_timezone/user_timezone.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: User timezone
type: module
description: Provides user greeting with current timezone module
core: 8.x
version: 1.0
package: Custom
17 changes: 17 additions & 0 deletions web/modules/custom/user_timezone/user_timezone.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @file
* This file stops cache for anonymous users.
*/

declare(strict_types = 1);

use Drupal\Core\Cache\Cache;

/**
* Implements hook_cron().
*/
function user_timezone_cron(): void {
Cache::invalidateTags(['user_timezone']);
}
4 changes: 4 additions & 0 deletions web/modules/custom/user_timezone/user_timezone.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
user_timezone.salutation:
class: Drupal\user_timezone\UserTimezoneSalutation
arguments: ['@current_user', '@datetime.time', '@string_translation']

0 comments on commit db708a3

Please sign in to comment.