View source
<?php
namespace Drupal\civicrm_entity\Plugin\views\relationship;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\core\form\FormStateInterface;
use Drupal\views\Plugin\views\relationship\RelationshipPluginBase;
use Drupal\views\Views;
class CiviCrmActivityContact extends RelationshipPluginBase {
protected $recordTypeMapping = [
1 => 'Assignee',
2 => 'Source',
3 => 'Target',
];
protected function defineOptions() {
$options = parent::defineOptions();
$options['record_type_id'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['record_type_id'] = [
'#type' => 'radios',
'#options' => [
'' => $this
->t('- Any -'),
] + $this->recordTypeMapping,
'#title' => $this
->t('Activity contact type'),
'#default_value' => $this->options['record_type_id'],
];
parent::buildOptionsForm($form, $form_state);
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->definition['extra'] = [];
if (!empty($this->options['record_type_id'])) {
$this->definition['extra'][] = [
'field' => 'record_type_id',
'value' => $this->options['record_type_id'],
'numeric' => TRUE,
];
}
}
public function query() {
$this
->ensureMyTable();
$views_data = Views::viewsData()
->get($this->table);
$left_field = $views_data['table']['base']['field'];
$first = [
'left_table' => $this->tableAlias,
'left_field' => $left_field,
'table' => 'civicrm_activity_contact',
'field' => $this->definition['first field'],
'adjusted' => TRUE,
];
if (!empty($this->options['required'])) {
$first['type'] = 'INNER';
}
if (!empty($this->definition['extra'])) {
$first['extra'] = $this->definition['extra'];
}
$first_join = Views::pluginManager('join')
->createInstance('standard', $first);
$first_alias = $this->query
->addTable('civicrm_activity_contact', $this->relationship, $first_join);
$second = [
'left_table' => $first_alias,
'left_field' => $this->definition['second field'],
'table' => $this->definition['base'],
'field' => $this->definition['base field'],
'adjusted' => TRUE,
];
if (!empty($this->options['required'])) {
$second['type'] = 'INNER';
}
$second_join = Views::pluginManager('join')
->createInstance('standard', $second);
$second_join->adjusted = TRUE;
$alias = $this->definition['base'] . '_civicrm_activity_contact';
$this->alias = $this->query
->addRelationship($alias, $second_join, $this->definition['base'], $this->relationship);
}
}