3 namespace Drupal\comment\Plugin\Action;
5 use Drupal\Component\Utility\Tags;
6 use Drupal\Core\Action\ConfigurableActionBase;
7 use Drupal\Core\Entity\EntityViewBuilderInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Render\RendererInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Unpublishes a comment containing certain keywords.
18 * id = "comment_unpublish_by_keyword_action",
19 * label = @Translation("Unpublish comment containing keyword(s)"),
23 class UnpublishByKeywordComment extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
26 * The comment entity builder handler.
28 * @var \Drupal\Core\Entity\EntityViewBuilderInterface
30 protected $viewBuilder;
35 * @var \Drupal\Core\Render\RendererInterface
40 * Constructs a UnpublishByKeywordComment object.
42 * @param array $configuration
43 * A configuration array containing information about the plugin instance.
44 * @param string $plugin_id
45 * The plugin ID for the plugin instance.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\Core\Entity\EntityViewBuilderInterface $comment_view_builder
49 * The comment entity builder handler.
50 * @param \Drupal\Core\Render\RendererInterface $renderer
53 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityViewBuilderInterface $comment_view_builder, RendererInterface $renderer) {
54 parent::__construct($configuration, $plugin_id, $plugin_definition);
56 $this->viewBuilder = $comment_view_builder;
57 $this->renderer = $renderer;
63 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68 $container->get('entity.manager')->getViewBuilder('comment'),
69 $container->get('renderer')
76 public function execute($comment = NULL) {
77 $build = $this->viewBuilder->view($comment);
78 $text = $this->renderer->renderPlain($build);
79 foreach ($this->configuration['keywords'] as $keyword) {
80 if (strpos($text, $keyword) !== FALSE) {
81 $comment->setUnpublished();
91 public function defaultConfiguration() {
100 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
101 $form['keywords'] = [
102 '#title' => $this->t('Keywords'),
103 '#type' => 'textarea',
104 '#description' => $this->t('The comment will be unpublished if it contains any of the phrases above. Use a case-sensitive, comma-separated list of phrases. Example: funny, bungee jumping, "Company, Inc."'),
105 '#default_value' => Tags::implode($this->configuration['keywords']),
113 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
114 $this->configuration['keywords'] = Tags::explode($form_state->getValue('keywords'));
120 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
121 /** @var \Drupal\comment\CommentInterface $object */
122 $result = $object->access('update', $account, TRUE)
123 ->andIf($object->status->access('edit', $account, TRUE));
125 return $return_as_object ? $result : $result->isAllowed();