MobileDeviceDetectionConditionPlugin.php in Mobile Device Detection 8.2
File
src/Plugin/Condition/MobileDeviceDetectionConditionPlugin.php
View source
<?php
namespace Drupal\mobile_device_detection\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
class MobileDeviceDetectionConditionPlugin extends ConditionPluginBase {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['negate'] = [];
$form['devices'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('When the device is determined'),
'#default_value' => $this->configuration['devices'],
'#options' => [
'mobile' => $this
->t('Mobile'),
'tablet' => $this
->t('Tablet'),
'desktop' => $this
->t('Desktop'),
],
'#description' => $this
->t('If you select no devices, the condition will evaluate to TRUE for all devices.'),
];
return $form;
}
public function defaultConfiguration() {
return [
'devices' => [],
] + parent::defaultConfiguration();
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['devices'] = array_filter($form_state
->getValue('devices'));
parent::submitConfigurationForm($form, $form_state);
}
public function summary() {
$devices = $this->configuration['devices'];
if (count($devices) > 1) {
$devices = implode(', ', $devices);
}
else {
$devices = reset($devices);
}
if (!empty($this->configuration['negate'])) {
return $this
->t('The device is not @devices', [
'@devices' => $devices,
]);
}
else {
return $this
->t('The device is @devices', [
'@devices' => $devices,
]);
}
}
public function evaluate() {
if (empty($this->configuration['devices']) && !$this
->isNegated()) {
return TRUE;
}
\Drupal::service('page_cache_kill_switch')
->trigger();
$entity = \Drupal::service('object.mdd');
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;
}
}