fe2f7ed5b0d6f09435c86c5c0d892485d76d5b56
[yaffs-website] / web / core / modules / comment / src / Plugin / Action / UnpublishByKeywordComment.php
1 <?php
2
3 namespace Drupal\comment\Plugin\Action;
4
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;
13
14 /**
15  * Unpublishes a comment containing certain keywords.
16  *
17  * @Action(
18  *   id = "comment_unpublish_by_keyword_action",
19  *   label = @Translation("Unpublish comment containing keyword(s)"),
20  *   type = "comment"
21  * )
22  */
23 class UnpublishByKeywordComment extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
24
25   /**
26    * The comment entity builder handler.
27    *
28    * @var \Drupal\Core\Entity\EntityViewBuilderInterface
29    */
30   protected $viewBuilder;
31
32   /**
33    * The renderer.
34    *
35    * @var \Drupal\Core\Render\RendererInterface
36    */
37   protected $renderer;
38
39   /**
40    * Constructs a UnpublishByKeywordComment object.
41    *
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
51    *   The renderer.
52    */
53   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityViewBuilderInterface $comment_view_builder, RendererInterface $renderer) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition);
55
56     $this->viewBuilder = $comment_view_builder;
57     $this->renderer = $renderer;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static(
65       $configuration,
66       $plugin_id,
67       $plugin_definition,
68       $container->get('entity.manager')->getViewBuilder('comment'),
69       $container->get('renderer')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
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();
82         $comment->save();
83         break;
84       }
85     }
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function defaultConfiguration() {
92     return [
93       'keywords' => [],
94     ];
95   }
96
97   /**
98    * {@inheritdoc}
99    */
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']),
106     ];
107     return $form;
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
114     $this->configuration['keywords'] = Tags::explode($form_state->getValue('keywords'));
115   }
116
117   /**
118    * {@inheritdoc}
119    */
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));
124
125     return $return_as_object ? $result : $result->isAllowed();
126   }
127
128 }