You are here

class SessionDataComparison in Rules Session Variables 2.0.x

Same name and namespace in other branches
  1. 2.x src/Plugin/Condition/SessionDataComparison.php \Drupal\rules_session_vars\Plugin\Condition\SessionDataComparison

Provides a 'Session Data Comparison' condition.

Plugin annotation


@Condition(
  id = "session_data_comparison",
  label = @Translation("Session data comparison"),
  category = @Translation("Rules session vars"),
  context_definitions = {
    "session_key" = @ContextDefinition("any",
      label = @Translation("Session key"),
      description = @Translation("Session key to identify your data.")
    ),
    "operation" = @ContextDefinition("string",
      label = @Translation("Operator"),
      description = @Translation("The comparison operator. Valid values are == (default), <, >, CONTAINS (for strings or arrays) and IN (for arrays or lists)."),
      assignment_restriction = "input",
      default_value = "==",
      required = FALSE
    ),
    "session_value" = @ContextDefinition("any",
      label = @Translation("Session value"),
      description = @Translation("The value to compare the session data with.")
    ),
  }
)

Hierarchy

Expanded class hierarchy of SessionDataComparison

File

src/Plugin/Condition/SessionDataComparison.php, line 36

Namespace

Drupal\rules_session_vars\Plugin\Condition
View source
class SessionDataComparison extends DataComparison implements ContainerFactoryPluginInterface {

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\SessionInterface|null
   */
  protected $session;

  /**
   * Constructor for SessionDataComparison.
   *
   * @param array $configuration
   *   The configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Symfony\Component\HttpFoundation\Request|null $request
   *   The request.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->session = $request ? $request
      ->getSession() : NULL;
  }

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

  /**
   * {@inheritdoc}
   */
  protected function doEvaluate($sessionKey, $operation, $value) {
    if ($this->session && ($data = $this->session
      ->get($sessionKey))) {
      return parent::doEvaluate($data, $operation, $value);
    }

    // No SESSION data, so fail evaluation.
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SessionDataComparison::$session protected property The session.
SessionDataComparison::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SessionDataComparison::doEvaluate protected function
SessionDataComparison::__construct public function Constructor for SessionDataComparison.