0

I have a block that creates a private_tempstore value (which I understand works exactly like PHP sessions).

How do I unset that value.

This is the code I am using for the block.

class SsoSessionBlock extends BlockBase {
    /**
     * {@inheritdoc}
     */
    public function build(){
        $value = 'dav';
        $session = \Drupal::service('user.private_tempstore')->get('achva_sso');
        $session->set('name', $value);


        return [
            '#type' => 'markup',
            '#markup' => $this->t('The output of super SsoSessionBlock block.'),
        ];
    }
}

I saw this method, but I am not sure how I am supposed to use it.

I tried the following code, but I get an error.

namespace Drupal\achva_sso\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\user;


class SsoController extends ControllerBase{
    public function ssoLogout(){

        $session = \Drupal::service('user.private_tempstore')->get('achva_sso');
        PrivateTempStore::delete('name');

        if(isset($session)){
            $status = $session .' is logged in';
        }else{
            $status = 'is logged out';
        }
        return[
            '#type' => 'markup',
            '#markup' => '<p>' . $status . '</p>' ,
        ];



    }
}

How do I unset a value I previously set?

2

I have a block that creates a private_tempstore value (which I understand works exactly like PHP sessions).

No, both are for different use cases.

Private tempstore is used mainly for larger quantities of data. To store a single value it would be too much overhead. Instead use the session object of the current request:

$request = \Drupal::request(); 
// in a class inject RequestStack and $request = $this->requestStack->getCurrentRequest();
// or get the request as controller parameter, see link below
$session = $request->getSession();

// set a value
$session->set('my_value', $my_value);

// unset a value
$session->remove('my_value');

This is the official Drupal 8 way to store session data and since you've tagged the question with sessions this probably what you are looking for, see Access session data through the Request object

  • Hey,few questions, 1. Where do I get the set function now that I have removed the ` \Drupal::service('user.private_tempstore')->get('achva_sso');` 2. I have added the folowing namespace use Symfony\Component\HttpFoundation\RequestStack; in the block file, than inside the build function I wrote this code: ` $request = \Drupal::request(); $session = $request->getSession('name');` and than I return the $session var. I keep getting an error – DavSev Nov 15 '18 at 8:16
  • this is the error Recoverable fatal error: Object of class Symfony\Component\HttpFoundation\Session\Session could not be converted to string in Drupal\Component\Utility\Xss::filter() (line 67 of core\lib\Drupal\Component\Utility\Xss.php). – DavSev Nov 15 '18 at 8:17
  • The code in the answer works as it is. Dependency injection is a different issue and you can include this as a TODO comment for later when you want to test the class. – 4k4 Nov 15 '18 at 8:22
1

You call the delete() method in the same way you call $session->set().

$session = \Drupal::service('user.private_tempstore')->get('achva_sso');
$session->delete('name');

You don't use PrivateTempStore::delete() since it's not a static method and it would throw errors or warnings, if called as static method.

As side note, user.private_tempstore is a deprecated service. Instead, the tempstore.private service should be used. (See User module's user.private_tempstore and user.shared_tempstore services moved to core for more details.)

It should also be better to use dependency injection: A class extending BlockBase, or ControllerBase, can implement the create() method to load new dependencies.

  • Hey, Thank you for your answer. Tried this but the tempstore is not deleted. I created a new page with the $session->get('name') . I go to the page with the delete, than go back to the page with the get and I still see the name value... – DavSev Nov 15 '18 at 8:44
  • 1
    Keep in mind that when a user logs out, or logs in, the tempstore is changed. If, for example, you set a value when the user is logged-in, and delete the same value after the user logged out, that value is still associated to the user account. – kiamlaluno Nov 15 '18 at 8:48
  • o.k I undestand. thank you... now I see I asked the wrong question... I need to store a session for an anonymus user... thank you! – DavSev Nov 15 '18 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.