You are here

public function MobileDeviceDetectionConditionPlugin::evaluate in Mobile Device Detection 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Condition/MobileDeviceDetectionConditionPlugin.php \Drupal\mobile_device_detection\Plugin\Condition\MobileDeviceDetectionConditionPlugin::evaluate()

Evaluates the condition and returns TRUE or FALSE accordingly.

Return value

bool TRUE if the condition has been met, FALSE otherwise.

Overrides ConditionInterface::evaluate

File

src/Plugin/Condition/MobileDeviceDetectionConditionPlugin.php, line 79

Class

MobileDeviceDetectionConditionPlugin
This main class which add ability to determine device.

Namespace

Drupal\mobile_device_detection\Plugin\Condition

Code

public function evaluate() {
  if (empty($this->configuration['devices']) && !$this
    ->isNegated()) {
    return TRUE;
  }
  \Drupal::service('page_cache_kill_switch')
    ->trigger();
  $entity = \Drupal::service('mobile_device_detection.object');
  foreach ($this->configuration['devices'] as $key => $value) {
    if ($key != 'desktop') {
      $func = 'is' . ucfirst($value);
      if (is_callable([
        $entity,
        $func,
      ]) && $entity
        ->{$func}()) {
        return TRUE;
      }
    }
    else {
      if (!$entity
        ->isMobile() && !$entity
        ->isTablet()) {
        return TRUE;
      }
    }
  }
  return FALSE;
}