CommentStyleguide.php in Style Guide 8
File
src/Plugin/Styleguide/CommentStyleguide.php
View source
<?php
namespace Drupal\styleguide\Plugin\Styleguide;
use Drupal\comment\Entity\Comment;
use Drupal\styleguide\GeneratorInterface;
use Drupal\styleguide\Plugin\StyleguidePluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class CommentStyleguide extends StyleguidePluginBase {
protected $generator;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, GeneratorInterface $styleguide_generator, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->generator = $styleguide_generator;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('styleguide.generator'), $container
->get('module_handler'));
}
public function items() {
$items = [];
if ($this->moduleHandler
->moduleExists('comment')) {
$items['comment'] = [
'title' => $this
->t('Comment'),
'content' => $this
->commentPrepare(),
'group' => $this
->t('Comment'),
];
}
return $items;
}
private function commentPrepare() {
$comment = [
'subject' => $this->generator
->words(5),
'comment_type' => 'comment',
'cid' => 0,
];
return [
'#theme' => 'comment',
'#comment' => Comment::create($comment),
'#comment_threaded' => TRUE,
'content' => $this->generator
->paragraphs(),
];
}
}