Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / node / src / Plugin / Action / UnpublishByKeywordNode.php
1 <?php
2
3 namespace Drupal\node\Plugin\Action;
4
5 use Drupal\Component\Utility\Tags;
6 use Drupal\Core\Action\ConfigurableActionBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Session\AccountInterface;
9
10 /**
11  * Unpublishes a node containing certain keywords.
12  *
13  * @Action(
14  *   id = "node_unpublish_by_keyword_action",
15  *   label = @Translation("Unpublish content containing keyword(s)"),
16  *   type = "node"
17  * )
18  */
19 class UnpublishByKeywordNode extends ConfigurableActionBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function execute($node = NULL) {
25     foreach ($this->configuration['keywords'] as $keyword) {
26       $elements = node_view(clone $node);
27       if (strpos(\Drupal::service('renderer')->render($elements), $keyword) !== FALSE || strpos($node->label(), $keyword) !== FALSE) {
28         $node->setUnpublished();
29         $node->save();
30         break;
31       }
32     }
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function defaultConfiguration() {
39     return [
40       'keywords' => [],
41     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
48     $form['keywords'] = [
49       '#title' => t('Keywords'),
50       '#type' => 'textarea',
51       '#description' => t('The content 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."'),
52       '#default_value' => Tags::implode($this->configuration['keywords']),
53     ];
54     return $form;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
61     $this->configuration['keywords'] = Tags::explode($form_state->getValue('keywords'));
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
68     /** @var \Drupal\node\NodeInterface $object */
69     $access = $object->access('update', $account, TRUE)
70       ->andIf($object->status->access('edit', $account, TRUE));
71
72     return $return_as_object ? $access : $access->isAllowed();
73   }
74
75 }