3 namespace Drupal\node\Plugin\Action;
5 use Drupal\Component\Utility\Tags;
6 use Drupal\Core\Action\ConfigurableActionBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Session\AccountInterface;
11 * Unpublishes a node containing certain keywords.
14 * id = "node_unpublish_by_keyword_action",
15 * label = @Translation("Unpublish content containing keyword(s)"),
19 class UnpublishByKeywordNode extends ConfigurableActionBase {
24 public function execute($node = NULL) {
25 foreach ($this->configuration['keywords'] as $keyword) {
26 $elements = node_view(clone $node);
27 if (strpos(drupal_render($elements), $keyword) !== FALSE || strpos($node->label(), $keyword) !== FALSE) {
28 $node->setPublished(FALSE);
38 public function defaultConfiguration() {
47 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
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']),
60 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
61 $this->configuration['keywords'] = Tags::explode($form_state->getValue('keywords'));
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));
72 return $return_as_object ? $access : $access->isAllowed();