You are here

public function BaseValidatorForm::generateForm in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.2 advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::generateForm()
  2. 8.3 advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::generateForm()

Generate a hierarchical form sorted by path from asset files.

Parameters

string $type: The asset extension - usually 'css' or 'js'.

bool $run_client_side: Determines whether to assign submit functions to buttons.

Return value

array A Form API array.

3 calls to BaseValidatorForm::generateForm()
CssLintForm::buildForm in advagg_validator/src/Form/CssLintForm.php
Form constructor.
CssW3Form::buildForm in advagg_validator/src/Form/CssW3Form.php
Form constructor.
JsHintForm::buildForm in advagg_validator/src/Form/JsHintForm.php
Form constructor.

File

advagg_validator/src/Form/BaseValidatorForm.php, line 52

Class

BaseValidatorForm
Base form for all advagg validator options.

Namespace

Drupal\advagg_validator\Form

Code

public function generateForm($type, $run_client_side = TRUE) {
  $form = [];
  $files = $this
    ->scanAllDirs($type);
  rsort($files);
  foreach ($files as $file) {
    $dir = dirname($file);

    // Build the directory structure.
    $levels = explode('/', $dir === '.' ? '{ROOT}' : $dir);
    $point =& $form;
    $built = [];
    foreach ($levels as $key => $value) {

      // Build directory structure.
      $built[] = $value;
      $point =& $point[$value];
      if (!is_array($point)) {
        $form_api_dirname = str_replace([
          '/',
          '.',
        ], [
          '__',
          '--',
        ], $dir);
        $wrapper = 'advagg-validator-' . $type . '-validator-ajax' . $form_api_dirname;
        $point = [
          '#type' => 'details',
          '#title' => $value,
          '#description' => '<strong>' . $this
            ->t('Directory:') . ' </strong>' . implode('/', $built),
          '#weight' => 100,
        ];
        if (!isset($point['check_all_levels']) && $value !== '{ROOT}' && count($levels) != $key + 1) {
          $point['check_all_levels'] = [
            '#type' => 'submit',
            '#value' => $this
              ->t('Check directory and all subdirectories'),
            '#name' => implode('/', $built),
          ];
          if (!$run_client_side) {
            $point['check_all_levels'] += [
              '#submit' => [
                '::submitCheckAll',
              ],
              '#ajax' => [
                'callback' => '::ajaxCheck',
                'wrapper' => $wrapper,
              ],
            ];
          }
          else {
            $point['check_all_levels'] += [
              '#attributes' => [
                'class' => [
                  'advagg_validator_recursive_' . $type,
                ],
              ],
            ];
          }
        }
        $point['break'] = [
          '#markup' => '<div></div>',
        ];
        $point['wrapper'] = [
          '#markup' => "<div id='" . $wrapper . "' class='results'></div>",
          '#weight' => 90,
        ];
      }

      // Drop in button and info if we reached the point where a file lives.
      if (count($levels) == $key + 1) {
        $form_api_filename = str_replace([
          '/',
          '.',
        ], [
          '__',
          '--',
        ], $file);
        if (!isset($point['check_this_level'])) {
          $point['check_this_level'] = [
            '#type' => 'submit',
            '#value' => $this
              ->t('Check directory'),
          ];
          if (!$run_client_side) {
            $point['check_this_level'] += [
              '#submit' => [
                '::submitCheckDirectory',
              ],
              '#name' => $dir,
              '#ajax' => [
                'callback' => '::ajaxCheck',
                'wrapper' => $wrapper,
              ],
            ];
          }
          else {
            $point['check_this_level'] += [
              '#attributes' => [
                'class' => [
                  'advagg_validator_' . $type,
                ],
              ],
            ];
          }
        }
        if (!isset($point['start'])) {
          $point['start'] = [
            '#markup' => '<br /><strong>' . $this
              ->t('File:') . ' </strong><div class="filenames">',
          ];
        }
        else {
          $point['start'] = [
            '#markup' => '<br /><strong>' . $this
              ->t('Files:') . ' </strong><div class="filenames">',
          ];
        }
        $point[$form_api_filename] = [
          '#markup' => $file . " </br>\n",
        ];
        if (!isset($point['end'])) {
          $point['end'] = [
            '#markup' => '</div>',
          ];
        }
        $point['hidden_' . $form_api_filename] = [
          '#type' => 'hidden',
          '#value' => $file,
          '#attributes' => [
            'class' => [
              'filenames',
            ],
          ],
        ];
      }
    }
  }
  return $form;
}