Unpublishes a comment containing certain keywords.
@Action(
id = "comment_unpublish_by_keyword_action",
label = @Translation("Unpublish comment containing keyword(s)"),
type = "comment"
)
Expanded class hierarchy of UnpublishByKeywordComment
class UnpublishByKeywordComment extends ConfigurableActionBase {
/**
* {@inheritdoc}
*/
public function execute($comment = NULL) {
$build = comment_view($comment);
$text = drupal_render($build);
foreach ($this->configuration['keywords'] as $keyword) {
if (strpos($text, $keyword) !== FALSE) {
$comment->status->value = COMMENT_NOT_PUBLISHED;
$comment
->save();
break;
}
}
}
/**
* {@inheritdoc}
*/
protected function getDefaultConfiguration() {
return array(
'keywords' => array(),
);
}
/**
* {@inheritdoc}
*/
public function form(array $form, array &$form_state) {
$form['keywords'] = array(
'#title' => t('Keywords'),
'#type' => 'textarea',
'#description' => 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."'),
'#default_value' => drupal_implode_tags($this->configuration['keywords']),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submit(array &$form, array &$form_state) {
$this->configuration['keywords'] = drupal_explode_tags($form_state['values']['keywords']);
}
}