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

Commit

Permalink
Merge pull request #46 from Pronovix/issue/8247
Browse files Browse the repository at this point in the history
Time of the day - correction (issue/8247)
  • Loading branch information
kingfisher88 authored Apr 24, 2020
2 parents 9ff2b83 + 113646a commit 3206944
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ usergreeting.settings:
morning_start:
type: integer
label: 'Time value for start point of morning greeting'
afernoon_start:
afternoon_start:
type: integer
label: 'Time value for start point of afternoon greeting'
evening_start:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#required' => TRUE,
];

$form['usergreeting_settings']['afernoon_start'] = [
$form['usergreeting_settings']['afternoon_start'] = [
'#type' => 'number',
'#min' => 0,
'#max' => 23,
'#title' => $this->t('Set the start point of greeting "Good Afternoon", it will also be the end point of greeting "Good Morning"'),
'#default_value' => $config->get('afernoon_start'),
'#default_value' => $config->get('afternoon_start'),
'#required' => TRUE,
];

Expand All @@ -67,15 +67,15 @@ public function buildForm(array $form, FormStateInterface $form_state) {
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$morning = $form_state->getValue('morning_start');
$afternoon = $form_state->getValue('afernoon_start');
$afternoon = $form_state->getValue('afternoon_start');
$evening = $form_state->getValue('evening_start');

if ($morning === $afternoon) {
$form_state->setErrorByName('morning_start', $this->t('The start point of morning can not be the same like the start point of afternoon'));
}

if ($afternoon === $evening) {
$form_state->setErrorByName('afernoon_start', $this->t('The start point of afternoon can not be the same like the start point of evening'));
$form_state->setErrorByName('afternoon_start', $this->t('The start point of afternoon can not be the same like the start point of evening'));
}

if ($evening === $morning) {
Expand All @@ -87,7 +87,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
}

if ($afternoon > $evening) {
$form_state->setErrorByName('afernoon_start', $this->t('The value of afternoon has to be less than the value of evening'));
$form_state->setErrorByName('afternoon_start', $this->t('The value of afternoon has to be less than the value of evening'));
}
}

Expand All @@ -97,7 +97,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('usergreeting.settings')
->set('morning_start', $form_state->getValue('morning_start'))
->set('afernoon_start', $form_state->getValue('afernoon_start'))
->set('afternoon_start', $form_state->getValue('afternoon_start'))
->set('evening_start', $form_state->getValue('evening_start'))
->save();

Expand Down
40 changes: 6 additions & 34 deletions web/modules/custom/usergreeting/src/GreetingTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;

/**
Expand All @@ -31,49 +31,32 @@ final class GreetingTime {
*/
protected $timeOutput;

/**
* The user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;

/**
* The string to translation.
*
* @var \Drupal\usergreeting\Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;

/**
* GreetingTime constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The value of time.
* @param \Drupal\Component\Datetime\TimeInterface $timeOutput
* The timestamp output.
* @param \Drupal\Core\Session\AccountInterface $account
* The user account.
* @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
* The sentences to be translated.
*/
public function __construct(ConfigFactoryInterface $config_factory, TimeInterface $timeOutput, AccountInterface $account, TranslationInterface $stringTranslation) {
public function __construct(ConfigFactoryInterface $config_factory, TimeInterface $timeOutput, TranslationInterface $stringTranslation) {
$this->configFactory = $config_factory;
$this->timeOutput = $timeOutput;
$this->account = $account;
$this->stringTranslation = $stringTranslation;
}

/**
* Returns the greeting.
*
* @return string
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The greeting message output.
*/
public function greetingMessage(): array {
public function greetingMessage(): TranslatableMarkup {
$time_output = (int) date('G', $this->timeOutput->getRequestTime());
$config_morning = $this->configFactory->get('usergreeting.settings')->get('morning_start');
$config_afternoon = $this->configFactory->get('usergreeting.settings')->get('afernoon_start');
$config_afternoon = $this->configFactory->get('usergreeting.settings')->get('afternoon_start');
$config_evening = $this->configFactory->get('usergreeting.settings')->get('evening_start');

if ($time_output >= $config_morning && $time_output < $config_afternoon) {
Expand All @@ -86,18 +69,7 @@ public function greetingMessage(): array {
$greeting = $this->t('Good Evening');
}

return [
'#cache' => [
'contexts' => [
'timezone',
'user',
],
],
'#prefix' => $greeting,
'#theme' => 'username',
'#account' => $this->account,
'#suffix' => '!',
];
return $greeting;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\usergreeting\GreetingTime;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -27,6 +28,13 @@ class UsergreetingBlock extends BlockBase implements ContainerFactoryPluginInter
*/
protected $greeting;

/**
* The user account.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;

/**
* Construct a UsergreetingBlock object.
*
Expand All @@ -38,33 +46,44 @@ class UsergreetingBlock extends BlockBase implements ContainerFactoryPluginInter
* The plugin implementation definition.
* @param \Drupal\usergreeting\GreetingTime $greeting
* The greeting message.
* @param \Drupal\Core\Session\AccountInterface $account
* The user account.
*/
public function __construct(array $configuration, string $plugin_id, $plugin_definition, GreetingTime $greeting) {
public function __construct(array $configuration, string $plugin_id, $plugin_definition, GreetingTime $greeting, AccountInterface $account) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->greeting = $greeting;
$this->account = $account;
}

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

/**
* {@inheritdoc}
*/
public function build() {
$build = [];
public function build(): array {
$greetuser = $this->greeting->greetingMessage();
$message = [
'#cache' => [
'max-age' => 0,
],
'#prefix' => $greetuser,
'#suffix' => '!',
];

$build['greetuser'] = $greetuser;

$build['#cache']['max-age'] = 0;
return $build;
if ($this->account->isAuthenticated() === TRUE) {
$message['#theme'] = 'username';
$message['#account'] = $this->account;
}

return $message;
}

}
2 changes: 1 addition & 1 deletion web/modules/custom/usergreeting/usergreeting.services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
services:
usergreeting.greeting:
class: Drupal\usergreeting\GreetingTime
arguments: ['@config.factory', '@datetime.time', '@current_user', '@string_translation']
arguments: ['@config.factory', '@datetime.time', '@string_translation']

0 comments on commit 3206944

Please sign in to comment.