1

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:

  1. I don't always see the session value on block 2
  2. I don't see the markup in block No 3.
  3. The session is not removed in block No 3.
  4. 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...

16
  • 1
    Are you on your dev site with caching disabled? As in your code it doesn't look like you're doing anything for caching so if you do have caching enabled you'll probably run into issues with that
    – Leigh
    Commented Nov 15, 2018 at 11:59
  • 2
    You need to add cache context to the render array ('session' specifically), and possibly override getCacheContexts of the plugin to do the same
    – Clive
    Commented Nov 15, 2018 at 12:03
  • 1
    One big question is: How do you render these blocks? Via Twig? Via placement on Block structure page?
    – leymannx
    Commented Nov 15, 2018 at 12:04
  • 1
    @leymannx Shouldn't make a difference really, cacheability bubbles up
    – Clive
    Commented Nov 15, 2018 at 12:05
  • 1
    I asked this just for clarification. I remembered a question where we had multiple Views rendered via one Twig template and then the caching needed to get busted in between. But your issue is different. Just follow @Clive's suggestion. See drupal.stackexchange.com/q/199527/15055
    – leymannx
    Commented Nov 15, 2018 at 12:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.