public function RoleFeatureForm::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_role/
src/ Form/ RoleFeatureForm.php, line 67
Class
- RoleFeatureForm
- Creates or edits a role feature for a product.
Namespace
Drupal\uc_role\FormCode
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL, $feature = NULL) {
$roles_config = $this
->config('uc_role.settings');
$models = uc_product_get_models($node
->id());
// Check if editing or adding to set default values.
if (!empty($feature)) {
$product_role = \Drupal::database()
->query('SELECT * FROM {uc_roles_products} WHERE pfid = :pfid', [
':pfid' => $feature['pfid'],
])
->fetchObject();
$default_model = $product_role->model;
$default_role = $product_role->rid;
$default_qty = $product_role->duration;
$default_granularity = $product_role->granularity;
$default_shippable = $product_role->shippable;
$default_by_quantity = $product_role->by_quantity;
if ($product_role->end_time) {
$end_time = $product_role->end_time;
$default_end_type = 'abs';
}
else {
$end_time = _uc_role_get_expiration($default_qty, $default_granularity);
$default_end_type = 'rel';
}
$form['pfid'] = [
'#type' => 'value',
'#value' => $feature['pfid'],
];
$form['rpid'] = [
'#type' => 'value',
'#value' => $product_role->rpid,
];
$default_end_override = $product_role->end_override;
}
else {
$default_model = 0;
$default_role = $roles_config
->get('default_role');
$default_qty = $roles_config
->get('default_granularity') == 'never' ? NULL : $roles_config
->get('default_length');
$default_granularity = $roles_config
->get('default_granularity');
$default_shippable = $node->shippable->value;
$default_by_quantity = $roles_config
->get('default_by_quantity');
$end_time = (int) $roles_config
->get('default_end_time');
$default_end_type = $roles_config
->get('default_end_expiration');
$default_end_override = FALSE;
}
$roles = _uc_role_get_choices();
if (!count($roles)) {
// No actions can be done. Remove submit buttons.
unset($form['buttons']);
$form['no_roles'] = [
'#markup' => $this
->t('You need to <a href=":url">create new roles</a> before any can be added as product features.', [
':url' => Url::fromRoute('user.role_add', [], [
'query' => [
'destination' => 'admin/store/config/products',
],
])
->toString(),
]),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
return $form;
}
$form['nid'] = [
'#type' => 'value',
'#value' => $node
->id(),
];
$form['model'] = [
'#type' => 'select',
'#title' => $this
->t('SKU'),
'#default_value' => $default_model,
'#description' => $this
->t('This is the SKU of the product that will grant the role.'),
'#options' => $models,
];
$form['role'] = [
'#type' => 'select',
'#title' => $this
->t('Role'),
'#default_value' => $default_role,
'#description' => $this
->t('This is the role the customer will receive after purchasing the product.'),
'#options' => $roles,
];
$form['shippable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Shippable product'),
'#default_value' => $default_shippable,
'#description' => $this
->t('Check if this product SKU that uses role assignment is associated with a shippable product.'),
];
$form['end_override'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Override the <a href=":url">default role expiration</a>.', [
':url' => Url::fromRoute('uc_product.settings')
->toString(),
]),
'#default_value' => $default_end_override,
];
$form['role_lifetime'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Role expiration'),
'#states' => [
'visible' => [
'input[name="end_override"]' => [
'checked' => TRUE,
],
],
],
];
$form['role_lifetime']['expiration'] = [
'#type' => 'select',
'#title' => $this
->t('Expiration type'),
'#options' => [
'rel' => $this
->t('Relative to purchase date'),
'abs' => $this
->t('Fixed date'),
],
'#default_value' => $default_end_type,
];
$form['role_lifetime']['expire_relative_duration'] = [
'#type' => 'textfield',
'#default_value' => $default_qty,
'#size' => 4,
'#maxlength' => 4,
'#prefix' => '<div class="expiration">',
'#suffix' => '</div>',
'#states' => [
'visible' => [
'select[name="expiration"]' => [
'value' => 'rel',
],
],
'invisible' => [
'select[name="expire_relative_granularity"]' => [
'value' => 'never',
],
],
],
];
$form['role_lifetime']['expire_relative_granularity'] = [
'#type' => 'select',
'#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)'),
],
'#default_value' => $default_granularity,
'#description' => $this
->t('From the time the role was purchased.'),
'#prefix' => '<div class="expiration">',
'#suffix' => '</div>',
'#states' => [
'visible' => [
'select[name="expiration"]' => [
'value' => 'rel',
],
],
],
];
$form['role_lifetime']['absolute'] = [
'#type' => 'container',
'#states' => [
'visible' => [
'select[name="expiration"]' => [
'value' => 'abs',
],
],
],
];
$date = empty($end_time) ? DrupalDateTime::createFromTimestamp($this->time
->getRequestTime()) : DrupalDateTime::createFromTimestamp($end_time);
$form['role_lifetime']['absolute']['expire_absolute'] = [
'#type' => 'datetime',
'#description' => $this
->t('Expire the role at the beginning of this day.'),
'#date_date_element' => 'date',
'#date_time_element' => 'none',
'#default_value' => $date,
];
$form['role_lifetime']['by_quantity'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Multiply by quantity'),
'#default_value' => $default_by_quantity,
'#description' => $this
->t('Check if the role duration should be multiplied by the quantity purchased.'),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save feature'),
];
return $form;
}