class Counter
Same name and namespace in other branches
- 11.x core/modules/views/src/Plugin/views/field/Counter.php \Drupal\views\Plugin\views\field\Counter
Field handler to show a counter of the current row.
Plugin annotation
@ViewsField("counter");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
- class \Drupal\views\Plugin\views\PluginBase extends \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\views\Plugin\views\HandlerBase extends \Drupal\views\Plugin\views\ViewsHandlerInterface implements \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\field\FieldPluginBase extends \Drupal\views\Plugin\views\field\FieldHandlerInterface implements \Drupal\views\Plugin\views\HandlerBase
- class \Drupal\views\Plugin\views\field\Counter uses \Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait implements \Drupal\views\Plugin\views\field\FieldPluginBase
- class \Drupal\views\Plugin\views\field\FieldPluginBase extends \Drupal\views\Plugin\views\field\FieldHandlerInterface implements \Drupal\views\Plugin\views\HandlerBase
- class \Drupal\views\Plugin\views\HandlerBase extends \Drupal\views\Plugin\views\ViewsHandlerInterface implements \Drupal\views\Plugin\views\PluginBase
- class \Drupal\views\Plugin\views\PluginBase extends \Drupal\Core\Plugin\ContainerFactoryPluginInterface, \Drupal\views\Plugin\views\ViewsPluginInterface, \Drupal\Component\Plugin\DependentPluginInterface, \Drupal\Core\Security\TrustedCallbackInterface implements \Drupal\Core\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait implements \Drupal\Component\Plugin\PluginBase
Expanded class hierarchy of Counter
Related topics
1 file declares its use of Counter
- CounterTest.php in core/
modules/ views/ tests/ src/ Unit/ Plugin/ field/ CounterTest.php
13 string references to 'Counter'
- BigPipeTestController::trustedCallbacks in core/
modules/ big_pipe/ tests/ modules/ big_pipe_test/ src/ BigPipeTestController.php - CounterTest::testCounterRandomPagerOffset in core/
modules/ views/ tests/ src/ Unit/ Plugin/ field/ CounterTest.php - Tests a counter field with a random pager offset.
- CounterTest::testCounterRandomStart in core/
modules/ views/ tests/ src/ Unit/ Plugin/ field/ CounterTest.php - Tests a counter with a random start.
- CounterTest::testCounterSecondPage in core/
modules/ views/ tests/ src/ Unit/ Plugin/ field/ CounterTest.php - Tests a counter field on the second page.
- CounterTest::testSimpleCounter in core/
modules/ views/ tests/ src/ Unit/ Plugin/ field/ CounterTest.php - Tests a simple counter field.
File
-
core/
modules/ views/ src/ Plugin/ views/ field/ Counter.php, line 15
Namespace
Drupal\views\Plugin\views\fieldView source
class Counter extends FieldPluginBase {
use UncacheableFieldHandlerTrait;
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['counter_start'] = [
'default' => 1,
];
return $options;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function query() {
// do nothing -- to override the parent query.
}
/**
* {@inheritdoc}
*/
public function getValue(ResultRow $values, $field = NULL) {
// Note: 1 is subtracted from the counter start value below because the
// counter value is incremented by 1 at the end of this function.
$count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
$pager = $this->view->pager;
// Get the base count of the pager.
if ($pager->usePager()) {
$count += $pager->getItemsPerPage() * $pager->getCurrentPage() + $pager->getOffset();
}
// Add the counter for the current site.
$count += $this->view->row_index + 1;
return $count;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.