View source
<?php
namespace Drupal\crm_core_match\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MatcherForm extends EntityForm {
protected $messenger;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->messenger = $container
->get('messenger');
return $instance;
}
protected $entity;
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$matcher = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $matcher
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => $this
->t('ID'),
'#maxlength' => 255,
'#default_value' => $matcher
->id(),
'#description' => $this
->t('ID of the matcher.'),
'#required' => TRUE,
'#machine_name' => [
'exists' => 'Drupal\\crm_core_match\\Entity\\Matcher::load',
],
'#disabled' => !$matcher
->isNew(),
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $matcher
->getDescription(),
'#maxlength' => 255,
'#description' => $this
->t('Description of the matcher.'),
];
if ($matcher
->isNew()) {
$plugin_types = [];
foreach (crm_core_match_matcher_manager()
->getDefinitions() as $plugin_id => $definition) {
$plugin_types[$plugin_id] = $definition['label'];
}
$default_value = NULL;
$single_plugin = count($plugin_types) == 1;
if ($single_plugin) {
$default_value = key($plugin_types);
$matcher
->set('plugin_id', $default_value);
}
$form['plugin_id'] = [
'#type' => 'select',
'#default_value' => $default_value,
'#options' => $plugin_types,
'#empty_value' => $this
->t('- Select -'),
'#title' => $this
->t('Matcher Plugin'),
'#limit_validation_errors' => [
[
'plugin_id',
],
],
'#submit' => [
'::submitSelectPlugin',
],
'#required' => TRUE,
'#executes_submit_callback' => TRUE,
'#ajax' => [
'callback' => '::ajaxReplacePluginSpecificForm',
'wrapper' => 'crm-core-match-match-engine-plugin',
'method' => 'replace',
],
'#access' => !$single_plugin,
];
$form['select_plugin'] = [
'#type' => 'submit',
'#value' => $this
->t('Select plugin'),
'#limit_validation_errors' => [
[
'plugin_id',
],
],
'#submit' => [
'::submitSelectPlugin',
],
'#attributes' => [
'class' => [
'js-hide',
],
],
'#access' => !$single_plugin,
];
}
else {
$form['current_plugin_id'] = [
'#type' => 'item',
'#title' => $this
->t('Match engine'),
'#markup' => $matcher
->getPlugin()
->getPluginDefinition()['label'],
];
}
$form['plugin_container'] = [
'#type' => 'container',
'#prefix' => '<div id="crm-core-match-match-engine-plugin">',
'#suffix' => '</div>',
];
if ($plugin = $matcher
->getPlugin()) {
$form['plugin_container']['configuration'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('@plugin configuration', [
'@plugin' => $matcher
->getPluginTitle(),
]),
'#tree' => TRUE,
];
$form['plugin_container']['configuration'] += (array) $plugin
->buildConfigurationForm($form['plugin_container']['configuration'], $form_state);
}
return $form;
}
public function submitSelectPlugin(array $form, FormStateInterface $form_state) {
$this->entity = $this
->buildEntity($form, $form_state);
$form_state
->setRebuild();
}
public function ajaxReplacePluginSpecificForm($form, FormStateInterface $form_state) {
return $form['plugin_container'];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$matcher = $this->entity;
if ($matcher
->isNew()) {
$plugin_id = $form_state
->getValue('plugin_id');
$plugin = crm_core_match_matcher_manager()
->createInstance($plugin_id, $matcher
->getConfiguration());
}
else {
$plugin = $matcher
->getPlugin();
}
$plugin
->validateConfigurationForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$matcher = $this->entity;
$plugin = $matcher
->getPlugin();
$plugin
->submitConfigurationForm($form, $form_state);
$this->messenger
->addMessage($this
->t('The configuration has been saved.'));
$form_state
->setRedirect('entity.crm_core_match.edit_form', [
'crm_core_match' => $matcher
->id(),
]);
}
}