This repository has been archived by the owner on Jun 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Pronovix/issue/8015
Create user greeting service
- Loading branch information
Showing
12 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ module: | |
tour: 0 | ||
update: 0 | ||
user: 0 | ||
user_timezone: 0 | ||
views_ui: 0 | ||
menu_link_content: 1 | ||
pathauto: 1 | ||
|
71 changes: 71 additions & 0 deletions
71
web/modules/custom/user_timezone/src/Plugin/Block/UserTimezoneBlock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
web/modules/custom/user_timezone/src/UserTimezoneSalutation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |