You are here

public function QueryTestForm::buildForm in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_query/src/Form/QueryTestForm.php \Drupal\ldap_query\Form\QueryTestForm::buildForm()

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

ldap_query/src/Form/QueryTestForm.php, line 49

Class

QueryTestForm
Test form for queries.

Namespace

Drupal\ldap_query\Form

Code

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;
}