public function FileFeatureForm::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/ FileFeatureForm.php, line 54
Class
- FileFeatureForm
- Creates or edits a file feature for a product.
Namespace
Drupal\uc_file\FormCode
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL, $feature = NULL) {
$file_config = $this
->config('uc_file.settings');
if (!is_dir($file_config
->get('base_dir'))) {
$this
->messenger()
->addWarning($this
->t('A file directory needs to be configured in <a href=":url">product settings</a> under the file download settings tab before a file can be selected.', [
':url' => Url::fromRoute('uc_product.settings')
->toString(),
]));
return $form;
}
// Rescan the file directory to populate {uc_files} with the current list
// because files uploaded via any method other than the Upload button
// (e.g. by FTP) won'b be in {uc_files} yet.
uc_file_refresh();
if (!\Drupal::database()
->queryRange('SELECT 1 FROM {uc_files}', 0, 1)
->fetchField()) {
$form['file']['file_message'] = [
'#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 model.', [
':url' => Url::fromRoute('uc_file.downloads', [], [
'query' => [
'destination' => 'node/' . $node
->id() . '/edit/features/file/add',
],
])
->toString(),
]),
];
return $form;
}
// Grab all the models on this product.
$models = uc_product_get_models($node
->id());
// Use the feature's values to fill the form, if they exist.
if (!empty($feature)) {
$file_product = \Drupal::database()
->query('SELECT * FROM {uc_file_products} p LEFT JOIN {uc_files} f ON p.fid = f.fid WHERE pfid = :pfid', [
':pfid' => $feature['pfid'],
])
->fetchObject();
$default_feature = $feature['pfid'];
$default_model = $file_product->model;
$default_filename = $file_product->filename;
$default_description = $file_product->description;
$default_shippable = $file_product->shippable;
$download_status = $file_product->download_limit != UC_FILE_LIMIT_SENTINEL;
$download_value = $download_status ? $file_product->download_limit : NULL;
$address_status = $file_product->address_limit != UC_FILE_LIMIT_SENTINEL;
$address_value = $address_status ? $file_product->address_limit : NULL;
$time_status = $file_product->time_granularity != UC_FILE_LIMIT_SENTINEL;
$quantity_value = $time_status ? $file_product->time_quantity : NULL;
$granularity_value = $time_status ? $file_product->time_granularity : 'never';
}
else {
$default_feature = NULL;
$default_model = '';
$default_filename = '';
$default_description = '';
$default_shippable = $node->shippable->value;
$download_status = FALSE;
$download_value = NULL;
$address_status = FALSE;
$address_value = NULL;
$time_status = FALSE;
$quantity_value = NULL;
$granularity_value = 'never';
}
$form['#attached']['library'][] = 'uc_file/uc_file.styles';
$form['nid'] = [
'#type' => 'value',
'#value' => $node
->id(),
];
$form['pfid'] = [
'#type' => 'value',
'#value' => $default_feature,
];
$form['uc_file_model'] = [
'#type' => 'select',
'#title' => $this
->t('SKU'),
'#default_value' => $default_model,
'#description' => $this
->t('This is the SKU that will need to be purchased to obtain the file download.'),
'#options' => $models,
];
$form['uc_file_filename'] = [
'#type' => 'textfield',
'#title' => $this
->t('File download'),
'#default_value' => $default_filename,
'#autocomplete_route_name' => 'uc_file.autocomplete_filename',
'#description' => $this
->t('The file that can be downloaded when product is purchased (enter a path relative to the %dir directory).', [
'%dir' => $file_config
->get('base_dir'),
]),
'#maxlength' => 255,
'#required' => TRUE,
];
$form['uc_file_description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $default_description,
'#maxlength' => 255,
'#description' => $this
->t('A description of the download associated with the product.'),
];
$form['uc_file_shippable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Shippable product'),
'#default_value' => $default_shippable,
'#description' => $this
->t('Check if this product model/SKU file download is also associated with a shippable product.'),
];
$form['uc_file_limits'] = [
'#type' => 'fieldset',
'#description' => $this
->t('Use these options to override any global download limits set at the :url.', [
':url' => Link::createFromRoute($this
->t('Ubercart product settings page'), 'uc_product.settings', [], [
'query' => [
'destination' => 'node/' . $node
->id() . '/edit/features/file/add',
],
])
->toString(),
]),
'#title' => $this
->t('File limitations'),
];
$form['uc_file_limits']['download_override'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Override download limit'),
'#default_value' => $download_status,
];
$form['uc_file_limits']['download_limit_number'] = [
'#type' => 'textfield',
'#title' => $this
->t('Downloads'),
'#default_value' => $download_value,
'#description' => $this
->t('The number of times this file can be downloaded.'),
'#maxlength' => 4,
'#size' => 4,
'#states' => [
'visible' => [
'input[name="download_override"]' => [
'checked' => TRUE,
],
],
],
];
$form['uc_file_limits']['location_override'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Override IP address limit'),
'#default_value' => $address_status,
];
$form['uc_file_limits']['download_limit_addresses'] = [
'#type' => 'textfield',
'#title' => $this
->t('IP addresses'),
'#default_value' => $address_value,
'#description' => $this
->t('The number of unique IPs that a file can be downloaded from.'),
'#maxlength' => 4,
'#size' => 4,
'#states' => [
'visible' => [
'input[name="location_override"]' => [
'checked' => TRUE,
],
],
],
];
$form['uc_file_limits']['time_override'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Override time limit'),
'#default_value' => $time_status,
];
$form['uc_file_limits']['download_limit_duration'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'duration',
],
],
];
$form['uc_file_limits']['download_limit_duration']['duration_qty'] = [
'#type' => 'textfield',
'#title' => $this
->t('Time'),
'#default_value' => $quantity_value,
'#size' => 4,
'#maxlength' => 4,
'#states' => [
'disabled' => [
'select[name="duration_granularity"]' => [
'value' => 'never',
],
],
'visible' => [
'input[name="time_override"]' => [
'checked' => TRUE,
],
],
],
];
$form['uc_file_limits']['download_limit_duration']['duration_granularity'] = [
'#type' => 'select',
'#default_value' => $granularity_value,
'#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)'),
],
'#description' => $this
->t('How long after this product has been purchased until this file download expires.'),
'#states' => [
'visible' => [
'input[name="time_override"]' => [
'checked' => TRUE,
],
],
],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save feature'),
];
return $form;
}