You are here

class NameFieldHandler in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_match/src/Plugin/crm_core_match/field/NameFieldHandler.php \Drupal\crm_core_match\Plugin\crm_core_match\field\NameFieldHandler
  2. 8.2 modules/crm_core_match/src/Plugin/crm_core_match/field/NameFieldHandler.php \Drupal\crm_core_match\Plugin\crm_core_match\field\NameFieldHandler

Class for evaluating name fields.

@CrmCoreMatchFieldHandler ( id = "name" )

Hierarchy

Expanded class hierarchy of NameFieldHandler

File

modules/crm_core_match/src/Plugin/crm_core_match/field/NameFieldHandler.php, line 14

Namespace

Drupal\crm_core_match\Plugin\crm_core_match\field
View source
class NameFieldHandler extends FieldHandlerBase {
  protected $configuration = [
    'title' => [
      'score' => 3,
    ],
    'given' => [
      'score' => 10,
    ],
    'middle' => [
      'score' => 1,
    ],
    'family' => [
      'score' => 10,
    ],
    'generational' => [
      'score' => 1,
    ],
    'credentials' => [
      'score' => 1,
    ],
  ];

  /**
   * {@inheritdoc}
   */
  public function getPropertyNames() {
    return [
      'title',
      'given',
      'middle',
      'family',
      'generational',
      'credentials',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getOperators($property = 'value') {
    return [
      'CONTAINS' => t('Contains'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function match(ContactInterface $contact, $property = 'family') {
    $field_name = $this->field
      ->getName();
    $valid_parts = [];
    if ($contact
      ->get($field_name)->{$property}) {
      $valid_parts[$property] = $contact
        ->get($field_name)->{$property};
    }

    // Get the matches.
    $matches = [];
    foreach ($valid_parts as $property => $value) {
      $query = $this->entityTypeManager
        ->getStorage('crm_core_individual')
        ->getQuery();
      $query
        ->condition('type', $contact
        ->bundle());
      if ($contact
        ->id()) {
        $query
          ->condition('individual_id', $contact
          ->id(), '<>');
      }
      $query
        ->condition($field_name . '.' . $property, $value, 'CONTAINS');
      $ids = $query
        ->execute();
      foreach ($ids as $id) {
        $matches[$id] = $this
          ->getScore($property);
      }
    }
    arsort($matches);
    $result = [];
    foreach ($matches as $id => $score) {
      $result[$id] = [
        $this->field
          ->getName() . '.' . $property => $score,
      ];
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldHandlerBase::$definition protected property The plugin definition.
FieldHandlerBase::$entityTypeManager protected property Entity type manager.
FieldHandlerBase::$field protected property The field.
FieldHandlerBase::$id protected property The plugin id.
FieldHandlerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FieldHandlerBase::getLabel public function Gets the property label. Overrides FieldHandlerInterface::getLabel
FieldHandlerBase::getOperator public function Gets the current selected operator. Overrides FieldHandlerInterface::getOperator
FieldHandlerBase::getOptions public function Gets the operator options. Overrides FieldHandlerInterface::getOptions
FieldHandlerBase::getScore public function Gets the score. Overrides FieldHandlerInterface::getScore
FieldHandlerBase::getStatus public function Gets the property status. Overrides FieldHandlerInterface::getStatus
FieldHandlerBase::getType public function Gets the field type. Overrides FieldHandlerInterface::getType
FieldHandlerBase::getWeight public function Gets the weight. Overrides FieldHandlerInterface::getWeight
FieldHandlerBase::WEIGHT_DELTA constant The weight.
FieldHandlerBase::__construct public function Constructs an plugin instance.
NameFieldHandler::$configuration protected property The settings. Overrides FieldHandlerBase::$configuration
NameFieldHandler::getOperators public function Gets the operators. Overrides FieldHandlerInterface::getOperators
NameFieldHandler::getPropertyNames public function Returns the names of the field's subproperties. Overrides FieldHandlerBase::getPropertyNames
NameFieldHandler::match public function Executes the match query. Overrides FieldHandlerBase::match