public function UserForm::buildForm in Ubercart 8.4
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
- uc_file/
src/ Form/ UserForm.php, line 72
Class
- UserForm
- Creates or edits a file feature for a product.
Namespace
Drupal\uc_file\FormCode
public function buildForm(array $form, FormStateInterface $form_state, AccountInterface $account = NULL) {
$this->account = $account;
$form['file'] = [
'#type' => 'details',
'#title' => $this
->t('Administration'),
];
// Drop out early if we don't even have any files uploaded.
if (!\Drupal::database()
->queryRange('SELECT 1 FROM {uc_files}', 0, 1)
->fetchField()) {
$form['file']['file_message'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('You must add files at the <a href=":url">Ubercart file download administration page</a> in order to attach them to a user.', [
':url' => Url::fromRoute('uc_file.downloads', [], [
'query' => [
'destination' => 'user/' . $account
->id() . '/edit',
],
])
->toString(),
]),
'#suffix' => '</p>',
];
return $form;
}
// Table displaying current downloadable files and limits.
$form['file']['download']['#theme'] = 'uc_file_hook_user_file_downloads';
$form['file']['download']['file_download']['#tree'] = TRUE;
$form['#attached']['library'][] = 'uc_file/uc_file.scripts';
$form['#attached']['library'][] = 'uc_file/uc_file.styles';
$downloadable_files = [];
$file_downloads = \Drupal::database()
->query('SELECT * FROM {uc_file_users} ufu INNER JOIN {uc_files} uf ON ufu.fid = uf.fid WHERE ufu.uid = :uid ORDER BY uf.filename ASC', [
':uid' => $account
->id(),
]);
$behavior = 0;
foreach ($file_downloads as $file_download) {
// Store a flat array so we can array_diff the ones already allowed when
// building the list of which can be attached.
$downloadable_files[$file_download->fid] = $file_download->filename;
$form['file']['download']['file_download'][$file_download->fid] = [
'fuid' => [
'#type' => 'value',
'#value' => $file_download->fuid,
],
'expiration' => [
'#type' => 'value',
'#value' => $file_download->expiration,
],
'remove' => [
'#type' => 'checkbox',
],
'filename' => [
'#markup' => $file_download->filename,
],
'expires' => [
'#markup' => $file_download->expiration ? $this->dateFormatter
->format($file_download->expiration, 'short') : $this
->t('Never'),
],
'time_polarity' => [
'#type' => 'select',
'#default_value' => '+',
'#options' => [
'+' => '+',
'-' => '-',
],
],
'time_quantity' => [
'#type' => 'textfield',
'#size' => 2,
'#maxlength' => 2,
],
'time_granularity' => [
'#type' => 'select',
'#default_value' => 'day',
'#options' => [
'never' => $this
->t('never'),
'day' => $this
->t('day(s)'),
'week' => $this
->t('week(s)'),
'month' => $this
->t('month(s)'),
'year' => $this
->t('year(s)'),
],
],
'downloads_in' => [
'#markup' => $file_download->accessed,
],
'download_limit' => [
'#type' => 'textfield',
'#maxlength' => 3,
'#size' => 3,
'#default_value' => $file_download->download_limit ? $file_download->download_limit : NULL,
],
'addresses_in' => [
'#markup' => count(unserialize($file_download->addresses)),
],
'address_limit' => [
'#type' => 'textfield',
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $file_download->address_limit ? $file_download->address_limit : NULL,
],
];
// Incrementally add behaviors.
// @todo _uc_file_download_table_behavior($behavior++, $file_download->fid);
$form['#attached']['drupalSettings']['behavior'][$behavior++] = $file_download->fid;
// Store old values for comparing to see if we actually made any changes.
$less_reading =& $form['file']['download']['file_download'][$file_download->fid];
$less_reading['download_limit_old'] = [
'#type' => 'value',
'#value' => $less_reading['download_limit']['#default_value'],
];
$less_reading['address_limit_old'] = [
'#type' => 'value',
'#value' => $less_reading['address_limit']['#default_value'],
];
$less_reading['expiration_old'] = [
'#type' => 'value',
'#value' => $less_reading['expiration']['#value'],
];
}
// Create the list of files able to be attached to this user.
$available_files = [];
$files = \Drupal::database()
->query('SELECT * FROM {uc_files} ORDER BY filename ASC');
foreach ($files as $file) {
if (substr($file->filename, -1) != '/' && substr($file->filename, -1) != '\\') {
$available_files[$file->fid] = $file->filename;
}
}
// Dialog for uploading new files.
$available_files = array_diff($available_files, $downloadable_files);
if (count($available_files)) {
$form['file']['file_add'] = [
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 6,
'#title' => $this
->t('Add file'),
'#description' => [
'#markup' => $this
->t('Select a file to add as a download. Newly added files will inherit the settings at the <a href=":url">Ubercart products settings page</a>.', [
':url' => Url::fromRoute('uc_product.settings')
->toString(),
]),
],
'#options' => $available_files,
'#tree' => TRUE,
];
}
$form['file']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}