public function ManageFilesForm::buildForm in Minify JS 8
Same name and namespace in other branches
- 8.2 src/Form/ManageFilesForm.php \Drupal\minifyjs\Form\ManageFilesForm::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
- src/
Form/ ManageFilesForm.php, line 20
Class
- ManageFilesForm
- Displays a list of detected javascript files and allows actions to be performed on them
Namespace
Drupal\minifyjs\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$files = minifyjs_load_all_files();
$form = [];
// Statistics
$number_of_files = 0;
$minified_files = 0;
$unminified_size = 0;
$minified_size = 0;
$saved_size = 0;
// Get search query.
$session = \Drupal::service('user.private_tempstore')
->get('minifyjs');
$query = $session
->get('query');
// Filter the files based on query.
if ($query) {
$new_files = [];
foreach ($files as $fid => $file) {
if (stripos($file->uri, $query) !== FALSE) {
$new_files[$fid] = $file;
}
}
$files = $new_files;
}
// pager init
$limit = 100;
$start = 0;
if (isset($_REQUEST['page'])) {
$start = $_REQUEST['page'] * $limit;
}
$total = count($files);
pager_default_initialize($total, $limit);
// Build the rows of the table.
$rows = [];
if ($total) {
// statistics for all files
foreach ($files as $fid => $file) {
$number_of_files++;
$unminified_size += $file->size;
$minified_size += $file->minified_size;
if ($file->minified_uri) {
$saved_size += $file->size - $file->minified_size;
$minified_files++;
}
}
// build table rows
$files_subset = array_slice($files, $start, $limit, TRUE);
foreach ($files_subset as $fid => $file) {
$operations = [
'#type' => 'operations',
'#links' => $this
->operations($file),
];
$rows[$fid] = [
Link::fromTextAndUrl($file->uri, Url::fromUri('base:' . $file->uri, [
'attributes' => [
'target' => '_blank',
],
])),
date('Y-m-d', $file->modified),
$this
->format_filesize($file->size),
$this
->minified_filesize($file),
$this
->precentage($file),
$this
->minified_date($file),
$this
->minified_file($file),
\Drupal::service('renderer')
->render($operations),
];
}
}
// report on statistics
drupal_set_message(t('@files javascript files (@min_files minified). The size of all original files is @size and the size of all of the minified files is @minified for a savings of @diff (@percent% smaller overall)', [
'@files' => $number_of_files,
'@min_files' => $minified_files,
'@size' => $this
->format_filesize($unminified_size),
'@minified' => $minified_size ? $this
->format_filesize($minified_size) : 0,
'@diff' => $minified_size ? $this
->format_filesize($saved_size) : 0,
'@percent' => $minified_size ? round($saved_size / $unminified_size * 100, 2) : 0,
]), 'status');
$form['search'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'container-inline',
],
];
$form['search']['query'] = [
'#type' => 'textfield',
'#title' => t('Search'),
'#title_display' => 'hidden',
'#default_value' => $query,
];
$form['search']['submit'] = [
'#type' => 'submit',
'#value' => t('Search'),
'#submit' => [
[
$this,
'filterList',
],
],
];
if ($query) {
$form['search']['reset'] = [
'#type' => 'submit',
'#value' => t('Reset'),
'#submit' => [
[
$this,
'filterListReset',
],
],
];
}
// The table.
$form['files'] = [
'#type' => 'tableselect',
'#header' => [
t('Original File'),
t('Last Modified'),
t('Original Size'),
t('Minified Size'),
t('Savings'),
t('Last Minified'),
t('Minified File'),
t('Operations'),
],
'#options' => $rows,
'#empty' => t('No files have been found. Please scan using the action link above.'),
];
$form['pager'] = [
'#type' => 'pager',
];
// Bulk minify button.
if ($total) {
$form['actions'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'container-inline',
],
],
];
$form['actions']['action'] = [
'#type' => 'select',
'#options' => [
'minify' => t('Minify (and re-minify)'),
'minify_skip' => t('Minify (and skip minified)'),
'restore' => t('Restore'),
],
];
$form['actions']['scope'] = [
'#type' => 'select',
'#options' => [
'selected' => t('Selected files'),
'all' => t('All files'),
],
];
$form['actions']['go'] = [
'#type' => 'submit',
'#value' => t('Perform action'),
];
}
return $form;
}