View source
<?php
namespace Drupal\civicrm_entity\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\civicrm_entity\CiviCrmApiInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\StringFilter as BaseStringFilter;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\views\Views;
class ContactReference extends BaseStringFilter {
protected $civicrmApi;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $connection, CiviCrmApiInterface $civicrm_api) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $connection);
$this->civicrmApi = $civicrm_api;
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->civicrmApi
->civicrmInitialize();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('database'), $container
->get('civicrm_entity.api'));
}
public function query() {
$this
->ensureMyTable();
$civicrm_contact_table = 'civicrm_contact';
$configuration = [
'table' => $civicrm_contact_table,
'field' => 'id',
'left_table' => $this->tableAlias,
'left_field' => $this->realField,
'operator' => '=',
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$civicrm_contact_table_alias = $this->query
->addRelationship($civicrm_contact_table, $join, $this->tableAlias);
$field = "{$civicrm_contact_table_alias}.sort_name";
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($field);
}
}
}