View source
<?php
namespace Drupal\mobile_device_detection\Plugin\views\display_extender;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\display_extender\DisplayExtenderPluginBase;
class MobileDeviceDetectionExtenderPlugin extends DisplayExtenderPluginBase {
public function defineOptionsAlter(&$options) {
$options['device_detection'] = [
'contains' => [
'title' => [
'default' => '',
],
'description' => [
'default' => '',
],
],
];
}
public function optionsSummary(&$categories, &$options) {
$categories['device_detection'] = [
'title' => $this
->t('Show "View" on special devices'),
'column' => 'second',
];
$options['device_detection'] = [
'category' => 'other',
'title' => $this
->t('Show "View" on special devices'),
'value' => $this
->getDevices() ? implode(', ', $this
->getDevices()) : $this
->t('none'),
];
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
if ($form_state
->get('section') == 'device_detection') {
$form['#title'] .= $this
->t('Show "View" on special devices');
$form['device_detection']['#type'] = 'container';
$form['device_detection']['#tree'] = TRUE;
$form['device_detection']['devices'] = [
'#type' => 'checkboxes',
'#options' => [
'mobile' => $this
->t('Mobile'),
'tablet' => $this
->t('Tablet'),
'desktop' => $this
->t('Desktop'),
],
'#default_value' => $this
->getDevices() ? $this
->getDevices() : [],
'#title' => $this
->t('Select device'),
];
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
if ($form_state
->get('section') == 'device_detection') {
$device_detection = $form_state
->getValue('device_detection');
$this->options['device_detection'] = $device_detection;
}
}
public function preExecute() {
}
public function query() {
}
public function defaultableSections(&$sections, $section = NULL) {
}
public function getDevices() {
$devices = isset($this->options['device_detection']) ? $this->options['device_detection'] : NULL;
if ($devices && isset($devices['devices'])) {
$devices = array_filter($devices['devices'], function ($var) {
return $var != FALSE;
});
}
return $devices;
}
}