FieldHandlerBase.php in CRM Core 8
File
modules/crm_core_match/src/Plugin/crm_core_match/field/FieldHandlerBase.php
View source
<?php
namespace Drupal\crm_core_match\Plugin\crm_core_match\field;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\crm_core_contact\ContactInterface;
use Drupal\field\FieldConfigInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class FieldHandlerBase implements FieldHandlerInterface, ContainerFactoryPluginInterface {
const WEIGHT_DELTA = 25;
protected $id;
protected $definition;
protected $configuration;
protected $field;
protected $entityTypeManager;
public function __construct(FieldDefinitionInterface $field, EntityTypeManagerInterface $entity_type_manager, array $configuration, $id, $definition) {
$this->configuration = $configuration;
$this->definition = $definition;
$this->id = $id;
$this->field = $field;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$field = $configuration['field'];
unset($configuration['field']);
return new static($field, $container
->get('entity_type.manager'), $configuration, $plugin_id, $plugin_definition);
}
public function getPropertyNames() {
return [
'value',
];
}
public function getLabel($property = 'value') {
return $this->field
->getLabel();
}
public function getStatus($property = 'value') {
return isset($this->configuration[$property]['status']) ? $this->configuration[$property]['status'] : FALSE;
}
public function getType() {
return $this->field
->getType();
}
public function getOperator($property = 'value') {
return isset($this->configuration[$property]['operator']) ? $this->configuration[$property]['operator'] : NULL;
}
public function getOptions($property = 'value') {
return isset($this->configuration[$property]['options']) ? $this->configuration[$property]['options'] : '';
}
public function getScore($property = 'value') {
return isset($this->configuration[$property]['score']) ? $this->configuration[$property]['score'] : 0;
}
public function getWeight($property = 'value') {
return isset($this->configuration[$property]['weight']) ? $this->configuration[$property]['weight'] : 0;
}
public function match(ContactInterface $contact, $property = 'value') {
$ids = [];
$field = $this->field
->getName();
$needle = $contact
->get($field)->{$property};
$query = $this->entityTypeManager
->getStorage('crm_core_individual')
->getQuery();
if (!empty($needle)) {
$query
->condition('type', $contact
->bundle());
if ($contact
->id()) {
$query
->condition('individual_id', $contact
->id(), '<>');
}
if ($field instanceof FieldConfigInterface) {
$field .= '.' . $property;
}
$query
->condition($field, $needle, $this
->getOperator($property));
$ids = $query
->execute();
}
$score = [
$this->field
->getName() . '.' . $property => $this
->getScore($property),
];
return array_fill_keys($ids, $score);
}
}