function UnpublishByKeywordNode::execute

Same name in other branches
  1. 8.9.x core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php \Drupal\node\Plugin\Action\UnpublishByKeywordNode::execute()
  2. 11.x core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php \Drupal\node\Plugin\Action\UnpublishByKeywordNode::execute()
  3. 10 core/modules/action/src/Plugin/Action/UnpublishByKeywordNode.php \Drupal\action\Plugin\Action\UnpublishByKeywordNode::execute()

Overrides ExecutableInterface::execute

File

core/modules/node/src/Plugin/Action/UnpublishByKeywordNode.php, line 24

Class

UnpublishByKeywordNode
Unpublishes a node containing certain keywords.

Namespace

Drupal\node\Plugin\Action

Code

public function execute($node = NULL) {
    $elements = \Drupal::entityTypeManager()->getViewBuilder('node')
        ->view(clone $node);
    $render = \Drupal::service('renderer')->render($elements);
    foreach ($this->configuration['keywords'] as $keyword) {
        if (strpos($render, $keyword) !== FALSE || strpos($node->label(), $keyword) !== FALSE) {
            $node->setUnpublished();
            $node->save();
            break;
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.