public function RateSettingsForm::buildForm in Rate 8
Same name and namespace in other branches
- 8.2 src/Form/RateSettingsForm.php \Drupal\rate\Form\RateSettingsForm::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 ConfigFormBase::buildForm
File
- src/
Form/ RateSettingsForm.php, line 73
Class
- RateSettingsForm
- Configure rate settings for the site.
Namespace
Drupal\rate\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('rate.settings');
$form['widget_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Widget settings'),
'#open' => TRUE,
];
$form['widget_settings']['use_ajax'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use AJAX'),
'#default_value' => $config
->get('use_ajax', FALSE),
'#description' => $this
->t('Record vote via AJAX.'),
];
$form['bot'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Bot detection'),
'#description' => $this
->t('Bots can be automatically banned from voting if they rate more than a given amount of votes within one minute or hour. This threshold is configurable below. Votes from the same IP-address will be ignored forever after reaching this limit.'),
'#collapsbile' => FALSE,
'#collapsed' => FALSE,
];
$threshold_options = array_combine([
0,
10,
25,
50,
100,
250,
500,
1000,
], [
0,
10,
25,
50,
100,
250,
500,
1000,
]);
$threshold_options[0] = $this
->t('disable');
$form['bot']['bot_minute_threshold'] = [
'#type' => 'select',
'#title' => $this
->t('1 minute threshold'),
'#options' => $threshold_options,
'#default_value' => $config
->get('bot_minute_threshold'),
];
$form['bot']['bot_hour_threshold'] = [
'#type' => 'select',
'#title' => $this
->t('1 hour threshold'),
'#options' => $threshold_options,
'#default_value' => $config
->get('bot_hour_threshold'),
];
$form['bot']['botscout_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('BotScout.com API key'),
'#default_value' => $config
->get('botscout_key'),
'#description' => $this
->t('Rate will check the voters IP against the BotScout database if it has an API key. You can request a key at %url.', [
'%url' => 'http://botscout.com/getkey.htm',
]),
];
// Start new table form to select the widget for each content type.
$form['rate_types_enabled'] = [
'#type' => 'details',
'#title' => $this
->t('Content types with enabled rate widgets'),
'#description' => $this
->t('If you set any type here to - None -, already existing data will remain untouched.'),
'#open' => TRUE,
];
// Create a table to store the entities and their widgets.
$header = [
$this
->t('Entity'),
$this
->t('Entity Type'),
$this
->t('Status'),
$this
->t('Rate Widget'),
];
$form['enabled_rate_widgets'] = [
'#type' => 'table',
'#weight' => 100,
'#header' => $header,
];
$entity_types = $this->entityTypeManager
->getDefinitions();
$entity_type_ids = array_keys($entity_types);
$enabled_types_widgets = $config
->get('enabled_types_widgets') ? $config
->get('enabled_types_widgets') : [];
// Get the widget types for the widget select table field.
$widget_type_options = RateEntityVoteWidget::getRateWidgets();
foreach ($entity_types as $entity_type_id => $entity_type) {
// Only allow voting on content entities.
// Also, don't allow voting on votes, that would be weird.
if ($entity_type
->getBundleOf() && $entity_type
->getBundleOf() != 'vote') {
$bundles = $this->entityTypeManager
->getStorage($entity_type_id)
->loadMultiple();
$content_entitites_with_bundles[] = $entity_type
->getBundleOf();
if (!empty($bundles)) {
foreach ($bundles as $bundle) {
$default_value = isset($enabled_types_widgets[$entity_type
->getBundleOf()][$bundle
->id()]) ? $enabled_types_widgets[$entity_type
->getBundleOf()][$bundle
->id()]['widget_type'] : '';
$widget_status = $default_value != '' ? 'ACTIVE' : '';
$entity_full = 'enabled|' . $entity_type
->getBundleOf() . '|' . $bundle
->id();
$form['enabled_rate_widgets'][$entity_full]['entity_type'] = [
'#plain_text' => $bundle
->label(),
];
$form['enabled_rate_widgets'][$entity_full]['entity_type_id'] = [
'#plain_text' => $entity_type
->getBundleOf(),
];
$form['enabled_rate_widgets'][$entity_full]['status'] = [
'#plain_text' => $widget_status,
];
$form['enabled_rate_widgets'][$entity_full]['widget_type'] = [
'#type' => 'select',
'#empty_value' => '',
'#required' => FALSE,
'#options' => $widget_type_options,
'#default_value' => $default_value,
];
}
}
}
elseif ($entity_type
->getGroup() == 'content' && !in_array($entity_type
->getBundleEntityType(), $entity_type_ids) && $entity_type_id != 'vote_result') {
$default_value = isset($enabled_types_widgets[$entity_type_id][$entity_type_id]) ? $enabled_types_widgets[$entity_type_id][$entity_type_id]['widget_type'] : '';
$widget_status = $default_value != '' ? 'ACTIVE' : '';
$entity_full = 'enabled|' . $entity_type_id . '|' . $entity_type_id;
$form['enabled_rate_widgets'][$entity_full]['entity_type'] = [
'#plain_text' => $entity_type
->getLabel()
->__toString(),
];
$form['enabled_rate_widgets'][$entity_full]['entity_type_id'] = [
'#plain_text' => $entity_type_id,
];
$form['enabled_rate_widgets'][$entity_full]['status'] = [
'#plain_text' => $widget_status,
];
$form['enabled_rate_widgets'][$entity_full]['widget_type'] = [
'#type' => 'select',
'#empty_value' => '',
'#required' => FALSE,
'#options' => $widget_type_options,
'#default_value' => $default_value,
];
}
}
$form['rate_types_enabled']['enabled_rate_widgets'] = $form['enabled_rate_widgets'];
unset($form['enabled_rate_widgets']);
return parent::buildForm($form, $form_state);
}