Function provide functional for button "take action".

Paramètres

\Drupal\Core\Form\FormStateInterface $form_state: FormState.

\Drupal\user\UserInterface|null $account: Account who will take action.

\Drupal\Core\Config\ImmutableConfig|null $config: Config for get api key.

array|null $values: FormState values.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException|\Drupal\Core\Entity\EntityStorageException

1 call to SpambotUserspamForm::actionSubmit()
SpambotUserspamForm::submitForm dans src/Form/SpambotUserspamForm.php

Fichier

src/Form/SpambotUserspamForm.php, line 456

Classe

SpambotUserspamForm
Settings form to save the configuration for Spambot.

Namespace

Drupal\spambot\Form

Code

public function actionSubmit(FormStateInterface $form_state, $account, $config, $values) {
    $comments_enabled = $this->moduleHandler
        ->moduleExists('comment');
    if ($account->id() == 1) {
        $this->messenger
            ->addMessage($this->t('Sorry, taking action against uid 1 is not allowed.'), 'warning');
        return;
    }
    // Prepare some data.
    $nids = $this->connection
        ->select('node_field_data', 'n')
        ->fields('n', [
        'nid',
    ])
        ->condition('uid', $account->id())
        ->orderBy('nid')
        ->execute()
        ->fetchCol();
    $node_hostnames = [];
    $result = $this->connection
        ->select('node_spambot')
        ->fields('node_spambot', [
        'nid',
        'hostname',
    ])
        ->condition('uid', $account->id())
        ->orderBy('nid', 'DESC')
        ->execute();
    foreach ($result as $record) {
        $node_hostnames[$record->nid] = $record->hostname;
    }
    $cids = [];
    if ($comments_enabled) {
        $cids = $this->connection
            ->select('comment_field_data', 'c')
            ->fields('c', [
            'cid',
        ])
            ->condition('uid', $account->id(), '=')
            ->orderBy('cid')
            ->execute()
            ->fetchCol();
    }
    // Report posts to www.stopforumspam.com.
    if (!empty($values['report']['nids'])) {
        foreach (array_filter($values['report']['nids']) as $nid => $unused) {
            
            /** @var \Drupal\node\Entity\Node $node */
            $node = $this->entityTypeManager
                ->getStorage('node')
                ->load($nid);
            if ($node && !empty($node->id())) {
                $body = $node->get('body')
                    ->getValue();
                $api_key = $config->get('spambot_sfs_api_key');
                if (spambot_report_account($account, $node_hostnames[$nid], $node->getTitle() . "\n\n" . $body[0]['summary'] . "\n\n" . $body[0]['value'], $api_key)) {
                    $this->messenger
                        ->addMessage($this->t('Node %title has been reported.', [
                        '%title' => $node->getTitle(),
                    ]));
                }
                else {
                    $this->messenger
                        ->addMessage($this->t('There was a problem reporting node %title.', [
                        '%title' => $node->getTitle(),
                    ]));
                }
            }
        }
    }
    if ($comments_enabled && !empty($values['report']['cids'])) {
        foreach (array_filter($values['report']['cids']) as $cid => $unused) {
            
            /** @var \Drupal\comment\Entity\Comment $comment */
            $comment = $this->entityTypeManager
                ->getStorage('comment')
                ->load($cid);
            if ($comment && !empty($comment->id())) {
                $body = $comment->get('comment_body')
                    ->getValue();
                $api_key = $config->get('spambot_sfs_api_key');
                if (spambot_report_account($account, $comment->getHostname(), $comment->getSubject() . "\n\n" . $body[0]['value'], $api_key)) {
                    $this->messenger
                        ->addMessage($this->t('Comment %title has been reported.', [
                        '%title' => $comment->getSubject(),
                    ]));
                }
                else {
                    $this->messenger
                        ->addMessage($this->t('There was a problem reporting comment %title.', [
                        '%title' => $comment->getSubject(),
                    ]));
                }
            }
        }
    }
    // Delete nodes and comments.
    if (!empty($values['action_content'])) {
        static::actionUserContent($values, $nids, $cids);
    }
    // Block or delete account.
    if (!empty($values['action_to_user'])) {
        if ($values['action_to_user'] === 'block_user') {
            $status = $account->get('status')
                ->getValue();
            if ($status[0]['value']) {
                $account->set('status', 0);
                $account->save();
                $this->messenger
                    ->addMessage($this->t('Account blocked.'));
            }
            else {
                $this->messenger
                    ->addMessage($this->t('This account is already blocked.'));
            }
        }
        else {
            // Redirect to user delete form.
            $form_state->setRedirect('entity.user.cancel_form', [
                'user' => $account->id(),
            ], []);
        }
    }
}