You are here

public function DefaultMatchingEngine::buildConfigurationForm in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_match/src/Plugin/crm_core_match/engine/DefaultMatchingEngine.php \Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine::buildConfigurationForm()
  2. 8.2 modules/crm_core_match/src/Plugin/crm_core_match/engine/DefaultMatchingEngine.php \Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides MatchEngineBase::buildConfigurationForm

File

modules/crm_core_match/src/Plugin/crm_core_match/engine/DefaultMatchingEngine.php, line 125

Class

DefaultMatchingEngine
DefaultMatchingEngine class.

Namespace

Drupal\crm_core_match\Plugin\crm_core_match\engine

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['threshold'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Threshold'),
    '#description' => $this
      ->t('Defines the score at which a contact is considered a match.'),
    '#maxlength' => 28,
    '#size' => 28,
    '#required' => TRUE,
    '#default_value' => $this
      ->getConfigurationItem('threshold'),
  ];
  $return_description = $this
    ->t(<<<EOF
If two or more contact records result in matches with identical scores, CRM Core
will give preference to one over the other base on selected option.
EOF
);
  $form['return_order'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Return Order'),
    '#description' => $return_description,
    '#default_value' => $this
      ->getConfigurationItem('return_order'),
    '#options' => [
      'created' => $this
        ->t('Most recently created'),
      'updated' => $this
        ->t('Most recently updated'),
      'associated' => $this
        ->t('Associated with user'),
    ],
  ];
  $strict_description = $this
    ->t(<<<EOF
Check this box to return a match for this contact type the first time one is
identified that meets the threshold. Stops redundant processing.
EOF
);
  $form['strict'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Strict matching'),
    '#description' => $strict_description,
    '#default_value' => $this
      ->getConfigurationItem('strict'),
  ];
  $form['rules'] = [
    '#title' => $this
      ->t('Field Matching'),
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => $this
      ->buildHeader(),
    '#empty' => $this
      ->t('There are no fields available.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'weight',
      ],
    ],
    '#theme_wrappers' => [
      'form_element',
    ],
  ];
  $form['unsupported'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Unsupported Fields'),
    '#open' => TRUE,
    // Hide table when empty. We set this to TRUE as soon there is at least
    // one unsupported field.
    '#access' => FALSE,
    'table' => [
      '#theme' => 'table',
      '#header' => [
        'label' => $this
          ->t('Name'),
        'field_type' => $this
          ->t('Field type'),
      ],
      '#rows' => [],
      '#empty' => $this
        ->t('There are no unsupported fields.'),
    ],
    'help_text' => [
      '#theme_wrappers' => [
        'container',
      ],
      '#attributes' => [
        'class' => [
          'description',
        ],
      ],
      '#markup' => $this
        ->t('A given field is unsupported when there is no field match handler for the field type it belongs to. In order to make it supported a handler plugin should be created for it.'),
    ],
  ];

  // @todo: Display fields per bundle.
  $contact_types = $this->entityTypeManager
    ->getStorage('crm_core_individual_type')
    ->loadMultiple();
  $fields = [];
  foreach ($contact_types as $contact_type_id => $value) {
    $fields += $this->entityFieldManager
      ->getFieldDefinitions('crm_core_individual', $contact_type_id);
  }
  foreach ($fields as $field) {
    $rules = $this
      ->getConfigurationItem('rules');
    $config = empty($rules[$field
      ->getName()]) ? [] : $rules[$field
      ->getName()];
    $config['field'] = $field;
    if ($this->pluginManager
      ->hasDefinition($field
      ->getType())) {
      $match_field_id = $field
        ->getType();

      /* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface $match_field */
      $match_field = $this->pluginManager
        ->createInstance($match_field_id, $config);
      foreach ($match_field
        ->getPropertyNames($field) as $name) {
        $row = $this
          ->buildRow($match_field, $name, FALSE);
        $form['rules'][$field
          ->getName() . ':' . $name] = $row;
      }
    }
    else {
      $form['unsupported']['table']['#rows'][] = [
        'label' => $field
          ->getLabel(),
        'type' => $field
          ->getType(),
      ];
      $form['unsupported']['#access'] = TRUE;
    }
  }
  return $form;
}