ViewsAutorefreshArea.php in Views Autorefresh D8 port 8
File
src/Plugin/views/area/ViewsAutorefreshArea.php
View source
<?php
namespace Drupal\views_autorefresh_d8\Plugin\views\area;
use Drupal\views\Plugin\views\area\AreaPluginBase;
use Drupal\Core\Form\FormStateInterface;
class ViewsAutorefreshArea extends AreaPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['interval'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['interval'] = [
'#type' => 'textfield',
'#title' => t('Interval to check for new items'),
'#default_value' => $this->options['interval'],
'#field_suffix' => 'milliseconds',
'#required' => TRUE,
];
}
public function render($empty = FALSE) {
$view = $this->view;
$interval = $this->options['interval'];
$build['#attached']['library'][] = 'views_autorefresh_d8/views_autorefresh';
$build['#attached']['drupalSettings']['views_autorefresh'][$view
->id()][$view->current_display] = $interval;
$view
->setAjaxEnabled(TRUE);
views_views_pre_render($view);
return $build;
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
if (!is_numeric($form_state
->getValue('options')['interval'])) {
$form_state
->setError($form['interval'], $this
->t('The interval has to be a numeric value.'));
}
}
}