/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $default_role_choices = user_role_names(TRUE);
     unset($default_role_choices[DRUPAL_AUTHENTICATED_RID]);
     $roles_config = $this->config('uc_role.settings');
     if (!count($default_role_choices)) {
         $form['no_roles'] = array('#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['default_role'] = array('#type' => 'select', '#title' => $this->t('Default role'), '#default_value' => $roles_config->get('default_role'), '#description' => $this->t('The default role Ubercart grants on specified products.'), '#options' => _uc_role_get_choices());
     $form['default_role_choices'] = array('#type' => 'checkboxes', '#title' => $this->t('Product roles'), '#default_value' => $roles_config->get('default_role_choices'), '#multiple' => TRUE, '#description' => $this->t('These are roles that Ubercart can grant to customers who purchase specified products. If you leave all roles unchecked, they will all be eligible for adding to a product.'), '#options' => $default_role_choices);
     $form['role_lifetime'] = array('#type' => 'fieldset', '#title' => $this->t('Default role expiration'));
     $form['role_lifetime']['default_end_expiration'] = array('#type' => 'select', '#title' => $this->t('Expiration type'), '#options' => array('rel' => $this->t('Relative to purchase date'), 'abs' => $this->t('Fixed date')), '#default_value' => $roles_config->get('default_end_expiration'));
     $form['role_lifetime']['default_length'] = array('#type' => 'textfield', '#default_value' => $roles_config->get('default_granularity') == 'never' ? NULL : $roles_config->get('default_length'), '#size' => 4, '#maxlength' => 4, '#prefix' => '<div class="expiration">', '#suffix' => '</div>', '#states' => array('visible' => array('select[name="default_end_expiration"]' => array('value' => 'rel')), 'invisible' => array('select[name="default_granularity"]' => array('value' => 'never'))));
     $form['role_lifetime']['default_granularity'] = array('#type' => 'select', '#default_value' => $roles_config->get('default_granularity'), '#options' => array('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('From the time the role was purchased.'), '#prefix' => '<div class="expiration">', '#suffix' => '</div>', '#states' => array('visible' => array('select[name="default_end_expiration"]' => array('value' => 'rel'))));
     $form['role_lifetime']['absolute'] = array('#type' => 'container', '#states' => array('visible' => array('select[name="default_end_expiration"]' => array('value' => 'abs'))));
     $date = (int) $roles_config->get('default_end_time');
     $date = !empty($date) ? DrupalDateTime::createFromTimestamp($date) : DrupalDateTime::createFromTimestamp(REQUEST_TIME);
     $form['role_lifetime']['absolute']['default_end_time'] = array('#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']['default_by_quantity'] = array('#type' => 'checkbox', '#title' => $this->t('Multiply by quantity'), '#description' => $this->t('Check if the role duration should be multiplied by the quantity purchased.'), '#default_value' => $roles_config->get('default_by_quantity'));
     $form['reminder']['reminder_length'] = array('#type' => 'textfield', '#title' => $this->t('Time before reminder'), '#default_value' => $roles_config->get('reminder_granularity') == 'never' ? NULL : $roles_config->get('reminder_length'), '#size' => 4, '#maxlength' => 4, '#prefix' => '<div class="expiration">', '#suffix' => '</div>', '#states' => array('disabled' => array('select[name="reminder_granularity"]' => array('value' => 'never'))));
     $form['reminder']['reminder_granularity'] = array('#type' => 'select', '#default_value' => $roles_config->get('reminder_granularity'), '#options' => array('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('The amount of time before a role expiration takes place that a customer is notified of its expiration.'), '#prefix' => '<div class="expiration">', '#suffix' => '</div>');
     $form['default_show_expiration'] = array('#type' => 'checkbox', '#title' => $this->t('Show expirations on user page'), '#default_value' => $roles_config->get('default_show_expiration'), '#description' => $this->t('If users have any role expirations they will be displayed on their account page.'));
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 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 = db_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 = array('day' => date('j', $product_role->end_time), 'month' => date('n', $product_role->end_time), 'year' => date('Y', $product_role->end_time));
             $default_end_type = 'abs';
         } else {
             $temp = _uc_role_get_expiration($default_qty, $default_granularity);
             $end_time = array('day' => date('j', $temp), 'month' => date('n', $temp), 'year' => date('Y', $temp));
             $default_end_type = 'rel';
         }
         $form['pfid'] = array('#type' => 'value', '#value' => $feature['pfid']);
         $form['rpid'] = array('#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 = $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'] = array('#markup' => $this->t('You need to <a href=":url">create new roles</a> before any can be added as product features.', [':url' => $this->url('user.role_add', [], ['query' => ['destination' => 'admin/store/config/products']])]), '#prefix' => '<p>', '#suffix' => '</p>');
         return $form;
     }
     $form['nid'] = array('#type' => 'value', '#value' => $node->id());
     $form['uc_role_model'] = array('#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['uc_role_role'] = array('#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['uc_role_shippable'] = array('#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'] = array('#type' => 'checkbox', '#title' => $this->t('Override the <a href=":url">default role expiration</a>.', [':url' => $this->url('uc_product.settings')]), '#default_value' => $default_end_override);
     $form['role_lifetime'] = array('#type' => 'fieldset', '#title' => $this->t('Role expiration'), '#states' => array('visible' => array('input[name="end_override"]' => array('checked' => TRUE))));
     $form['role_lifetime']['expiration'] = array('#type' => 'select', '#title' => $this->t('Expiration type'), '#options' => array('rel' => $this->t('Relative to purchase date'), 'abs' => $this->t('Fixed date')), '#default_value' => $default_end_type);
     $form['role_lifetime']['uc_role_expire_relative_duration'] = array('#type' => 'textfield', '#default_value' => $default_qty, '#size' => 4, '#maxlength' => 4, '#prefix' => '<div class="expiration">', '#suffix' => '</div>', '#states' => array('visible' => array('select[name="expiration"]' => array('value' => 'rel')), 'invisible' => array('select[name="uc_role_expire_relative_granularity"]' => array('value' => 'never'))));
     $form['role_lifetime']['uc_role_expire_relative_granularity'] = array('#type' => 'select', '#options' => array('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' => array('visible' => array('select[name="expiration"]' => array('value' => 'rel'))));
     $form['role_lifetime']['absolute'] = array('#type' => 'container', '#states' => array('visible' => array('select[name="expiration"]' => array('value' => 'abs'))));
     $form['role_lifetime']['absolute']['uc_role_expire_absolute'] = array('#type' => 'datetime', '#description' => $this->t('Expire the role at the beginning of this day.'), '#date_date_element' => 'date', '#date_time_element' => 'none', '#default_value' => '');
     if ($end_time) {
         $form['role_lifetime']['absolute']['uc_role_expire_absolute']['#default_value'] = $end_time;
     }
     $form['role_lifetime']['uc_role_by_quantity'] = array('#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'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save feature'));
     return $form;
 }