QueryTestForm.php in Lightweight Directory Access Protocol (LDAP) 8.4
File
ldap_query/src/Form/QueryTestForm.php
View source
<?php
declare (strict_types=1);
namespace Drupal\ldap_query\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Drupal\ldap_query\Controller\QueryController;
use Drupal\ldap_servers\Form\ServerTestForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
class QueryTestForm extends FormBase {
protected $ldapQuery;
public function getFormId() : string {
return 'ldap_query_test_form';
}
public function __construct(QueryController $ldap_query) {
$this->ldapQuery = $ldap_query;
}
public static function create(ContainerInterface $container) : QueryTestForm {
return new static($container
->get('ldap.query'));
}
public function buildForm(array $form, FormStateInterface $form_state, $ldap_query_entity = NULL) : array {
if ($ldap_query_entity) {
$this->ldapQuery
->load($ldap_query_entity);
$this->ldapQuery
->execute();
$data = $this->ldapQuery
->getRawResults();
$form['result_count'] = [
'#markup' => '<h2>' . $this
->t('@count results', [
'@count' => count($data),
]) . '</h2>',
];
$header[] = 'DN';
$attributes = $this->ldapQuery
->availableFields();
foreach ($attributes as $attribute) {
$header[] = $attribute;
}
$rows = [];
foreach ($data as $entry) {
$row = [
$entry
->getDn(),
];
foreach ($attributes as $attribute_data) {
if (!$entry
->hasAttribute($attribute_data, FALSE)) {
$row[] = 'No data';
}
else {
if (count($entry
->getAttribute($attribute_data, FALSE)) > 1) {
$row[] = ServerTestForm::binaryCheck(implode("\n", $entry
->getAttribute($attribute_data, FALSE)));
}
else {
$row[] = ServerTestForm::binaryCheck($entry
->getAttribute($attribute_data, FALSE)[0]);
}
}
}
$rows[] = $row;
}
$form['result'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
];
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) : void {
}
public function submitForm(array &$form, FormStateInterface $form_state) : void {
}
}