Same name and namespace in other branches
  1. 6.0.x advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::generateForm() 1 commentaire
  2. 8.x-2.x advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::generateForm() 1 commentaire
  3. 8.x-3.x advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::generateForm() 1 commentaire
  4. 8.x-4.x advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::generateForm() 1 commentaire

Generate a hierarchical form sorted by path from asset files.

Paramètres

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.

Fichier

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

Classe

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