<?php
namespace Drupal\tmgmt_local\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Views;
class ItemCount extends FieldPluginBase {
public $query = NULL;
protected function defineOptions() {
$options = parent::defineOptions();
$options['state'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$options = array(
'' => t('- All -'),
);
$options += JobItem::getStates();
$form['state'] = array(
'#title' => t('Job item state'),
'#description' => t('Count only job items of a certain state.'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $this->options['state'],
);
}
public function usesGroupBy() {
return FALSE;
}
public function query() {
$this
->ensureMyTable();
$configuration = array(
'table' => 'tmgmt_job_item',
'field' => 'tjid',
'left_table' => $this->tableAlias,
'left_field' => 'tjid',
'operator' => '=',
);
if (!empty($this->options['state'])) {
$configuration['extra'] = [
[
'field' => 'state',
'value' => $this->options['state'],
],
];
}
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$this->tableAlias = $this->query
->addTable('tmgmt_job_item', $this->relationship, $join);
$params = array(
'function' => 'count',
);
$this->field_alias = $this->query
->addField($this->tableAlias, 'tjiid', NULL, $params);
$this
->addAdditionalFields();
}
}