Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Created December 23, 2017 04:03
Show Gist options
  • Select an option

  • Save cyberlex404/e84d0b2c85d1b51d997b66f0e43b0314 to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/e84d0b2c85d1b51d997b66f0e43b0314 to your computer and use it in GitHub Desktop.
How to create a new Selection Rule(Condition) for Panels in Drupal 8
<?php
/**
* Created by PhpStorm.
* User: Lex
* Date: 23.12.2017
* Time: 6:52
*/
namespace Drupal\housing\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\housing\Entity\HousingInterface;
/**
* Provides a 'Subscription Is Active' condition.
*
* @Condition(
* id = "subscription_is_active",
* label = @Translation("Subscription Is Active"),
* category = @Translation("Housing"),
* context = {
* "housing" = @ContextDefinition("entity:housing",
* label = @Translation("Housing")
* )
* }
* )
*
*/
class SubscriptionIsActive extends ConditionPluginBase{
/**
* @inheritDoc
*/
public function evaluate() {
$pass = FALSE;
/** @var \Drupal\housing\Entity\HousingInterface $housing */
$housing = $this->getContextValue('housing');
if (!$housing instanceof HousingInterface) {
return $pass;
}
if(!$housing->getSubscription()) {
return $pass;
}
$subscription = $housing->getSubscription();
$pass = $subscription->isActive();
return $pass;
}
/**
* @inheritDoc
*/
public function summary() {
return $this->t('The TRUE if Subscription is active.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment