public function SpambotUserspamForm::buildForm in Spambot 8
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/ SpambotUserspamForm.php, line 126
Class
- SpambotUserspamForm
- Settings form to save the configuration for Spambot.
Namespace
Drupal\spambot\FormCode
public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
$config = $this
->config('spambot.settings');
$key = $config
->get('spambot_sfs_api_key');
$comments_enabled = $this->moduleHandler
->moduleExists('comment');
$node_count = $this->connection
->select('node_field_data', 'n')
->fields('n', [
'nid',
])
->condition('uid', $user
->id())
->countQuery()
->execute()
->fetchField();
$status = $this
->t('This account has @n nodes.', [
'@n' => $node_count,
]);
if ($comments_enabled) {
$comment_count = $this->connection
->select('comment_field_data', 'c')
->fields('c', [
'cid',
])
->condition('uid', $user
->id())
->countQuery()
->execute()
->fetchField();
$status = $this
->t('This account has @n nodes and @c comments.', [
'@n' => $node_count,
'@c' => $comment_count,
]);
}
$form['check'] = [
'#type' => 'submit',
'#value' => $this
->t('Check if this account matches a known spammer'),
];
$form['action'] = [
'#type' => 'details',
'#title' => $this
->t('Take action against this account'),
'#open' => TRUE,
'#description' => $status,
];
$form['action']['action_content'] = [
'#type' => 'radios',
'#options' => [
static::SPAMBOT_CONTENT_ACTION_UNPUBLISH => $this
->t('Unpublish nodes and comments by this account'),
static::SPAMBOT_CONTENT_ACTION_DELETE => $this
->t('Delete nodes and comments by this account'),
],
];
$form['action']['report'] = [
'#type' => 'details',
'#title' => $this
->t('Report this account to www.stopforumspam.com'),
'#tree' => TRUE,
'#open' => TRUE,
'#collapsible' => TRUE,
];
// Fetch a list of reportable nodes.
$form['action']['report']['nids'] = [];
$result = $this->connection
->select('node_spambot', 'ns')
->fields('ns', [
'nid',
'hostname',
])
->condition('ns.uid', $user
->id())
->orderBy('ns.nid', 'DESC')
->range(0, 20)
->execute();
$nid_hostnames = [];
foreach ($result as $record) {
$nid_hostnames[$record->nid] = $record->hostname;
}
foreach ($nid_hostnames as $nid => $hostname) {
if ($node = Node::load($nid)) {
$title = Unicode::truncate(Html::escape($node
->getTitle()), 128, TRUE, TRUE);
$url = Url::fromRoute('entity.node.canonical', [
'node' => $nid,
], [
'absolute' => TRUE,
]);
$form['action']['report']['nids'][$nid] = [
'#type' => 'checkbox',
'#title' => Link::fromTextAndUrl($title, $url)
->toString() . ' ' . $this
->t('(node, ip=@ip)', [
'@ip' => $hostname,
]),
'#disabled' => !$key,
];
}
}
// Fetch a list of reportable comments.
if ($comments_enabled) {
$form['action']['report']['cids'] = [];
$result = $this->connection
->select('comment_field_data', 'comment')
->fields('comment', [
'cid',
])
->condition('uid', $user
->id())
->orderBy('cid', 'DESC')
->range(0, 20)
->execute();
$cids = [];
foreach ($result as $record) {
$cids[$record->cid] = $record->cid;
}
foreach ($cids as $cid) {
/** @var \Drupal\comment\Entity\Comment $comment */
$comment = $this->entityTypeManager
->getStorage('comment')
->load($cid);
if ($comment) {
$subject = $comment
->getSubject();
$form['action']['report']['cids'][$cid] = [
'#type' => 'checkbox',
'#title' => Link::fromTextAndUrl($subject, $comment
->permalink())
->toString() . ' ' . $this
->t('(comment, ip=@ip)', [
'@ip' => $comment
->getHostname(),
]),
'#disabled' => !$key,
];
}
}
}
if ($key) {
$comment_cids = $comments_enabled ? count($form['action']['report']['cids']) : 0;
$evidence_count = count($form['action']['report']['nids']) + $comment_cids;
$form['action']['report']['#description'] = $evidence_count ? $this
->t('Select one or more posts below to report them to www.stopforumspam.com.') : $this
->t('This account cannot be reported because no evidence or IP address is available.');
}
else {
$url = Url::fromRoute('spambot.settings_form')
->toString();
$form['action']['report']['#description'] = $this
->t('An API key from <a href="http://www.stopforumspam.com">www.stopforumspam.com</a> must <a href="@admin-url">be configured</a> to report spammers.', [
'@admin-url' => $url,
]);
}
$form['action']['action_to_user'] = [
'#type' => 'radios',
'#options' => [
'block_user' => $this
->t('Block this account'),
'delete_user' => $this
->t('Delete this account'),
],
];
$form['action']['action'] = [
'#type' => 'submit',
'#value' => $this
->t('Take action'),
];
$form['uid'] = [
'#type' => 'value',
'#value' => $user
->id(),
];
return parent::buildForm($form, $form_state);
}