Counter.php in Drupal 8
File
core/modules/views/src/Plugin/views/field/Counter.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
class Counter extends FieldPluginBase {
use UncacheableFieldHandlerTrait;
public function usesGroupBy() {
return FALSE;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['counter_start'] = [
'default' => 1,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['counter_start'] = [
'#type' => 'textfield',
'#title' => $this
->t('Starting value'),
'#default_value' => $this->options['counter_start'],
'#description' => $this
->t('Specify the number the counter should start at.'),
'#size' => 2,
];
parent::buildOptionsForm($form, $form_state);
}
public function query() {
}
public function getValue(ResultRow $values, $field = NULL) {
$count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
$pager = $this->view->pager;
if ($pager
->usePager()) {
$count += $pager
->getItemsPerPage() * $pager
->getCurrentPage() + $pager
->getOffset();
}
$count += $this->view->row_index + 1;
return $count;
}
}
Classes
Name |
Description |
Counter |
Field handler to show a counter of the current row. |