I have a module that have three blocks.
Block No 1 sets the session
Block No 2 gets the session
Block No 3 remove the session
Each block is assigned to a different page.
Now, for some reason this is not working well. I spent the last hours understanding where my mistakes are. The problems are:
- I don't always see the session value on block 2
- I don't see the markup in block No 3.
- The session is not removed in block No 3.
- The block doesn't always get set in block No 1
This is the block code:
Block 1:
namespace Drupal\achva_sso\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Class SsoSessionBlock
* @package Drupal\achva_sso\Plugin\Block
*
* @Block(
* id = "achva_sso_session_block",
* admin_label = @Translation("Example achva sso session block block")
* )
*/
class SsoSessionBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build(){
$value = time();
$session = \Drupal::request()->getSession();
$session->set('name', $value);
return [
'#type' => 'markup',
'#markup' => $this->t('The output of super SsoSessionBlock block.'),
];
}
}
Block 2:
<?php
/**
* Created by PhpStorm.
* User: david_s
* Date: 14-Nov-18
* Time: 2:07 PM
*/
namespace Drupal\achva_sso\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Class SsoSessionBlock
* @package Drupal\module_hero\Plugin\Block
*
* @Block(
* id = "achva_see_sso_session_block",
* admin_label = @Translation("Example achva see sso session block block")
* )
*/
class SsoSeeSessionBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build(){
// $session = \Drupal::service('user.private_tempstore')->get('achva_sso');
$session = \Drupal::request()->getSession();
$details = $session->get('name');
return [
'#type' => 'markup',
'#markup' => $details,
];
// $request = \Drupal::request();
// $session = $request->getSession('name');
// return [
// '#type' => 'markup',
// '#markup' => $session,
// ];
}
}
Block 3:
<?php
namespace Drupal\achva_sso\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Class SsoSessionBlock
* @package Drupal\achva_sso\Plugin\Block
*
* @Block(
* id = "achva_delete_sso_session_block",
* admin_label = @Translation("Example achva delete sso session block block")
* )
*/
class SsoSessionDestroyBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build(){
$session = \Drupal::request()->getSession();
$details = $session->remove('name');
if($details !== null){
$a = 'logged in';
}else{
$a = 'logged out';
}
return [
'#type' => 'markup',
'#markup' => '<p id="aa">sss'. $a .'</p> ',
];
}
}
I am totally lost. Any help would be great. Tried to use simple sessions doesn't work well either...
getCacheContexts
of the plugin to do the same