knowledge-8.x-1.x-dev/src/Plugin/Action/UnpublishByKeywordKnowledge.php
src/Plugin/Action/UnpublishByKeywordKnowledge.php
<?php
namespace Drupal\knowledge\Plugin\Action;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\EntityViewBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Unpublishes a knowledge containing certain keywords.
*
* @Action(
* id = "knowledge_unpublish_by_keyword_action",
* label = @Translation("Unpublish knowledge containing keyword(s)"),
* type = "knowledge"
* )
*/
class UnpublishByKeywordKnowledge extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
/**
* The knowledge entity builder handler.
*
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
*/
protected $viewBuilder;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs an UnpublishByKeywordKnowledge object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityViewBuilderInterface $knowledge_view_builder
* The knowledge entity builder handler.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityViewBuilderInterface $knowledge_view_builder, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->viewBuilder = $knowledge_view_builder;
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')->getViewBuilder('knowledge'),
$container->get('renderer')
);
}
/**
* {@inheritdoc}
*/
public function execute($knowledge = NULL) {
$build = $this->viewBuilder->view($knowledge);
$text = $this->renderer->renderInIsolation($build);
foreach ($this->configuration['keywords'] as $keyword) {
if (strpos($text, $keyword) !== FALSE) {
$knowledge->setUnpublished();
$knowledge->save();
break;
}
}
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'keywords' => [],
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['keywords'] = [
'#title' => $this->t('Keywords'),
'#type' => 'textarea',
'#description' => $this->t('The knowledge 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' => Tags::implode($this->configuration['keywords']),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['keywords'] = Tags::explode($form_state->getValue('keywords'));
}
/**
* {@inheritdoc}
*/
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\knowledge\KnowledgeInterface $object */
$result = $object->access('update', $account, TRUE)
->andIf($object->status->access('edit', $account, TRUE));
return $return_as_object ? $result : $result->isAllowed();
}
}
