AdminCommentForm.php in Commerce Core 8.2
File
modules/log/src/Plugin/views/area/AdminCommentForm.php
View source
<?php
namespace Drupal\commerce_log\Plugin\views\area;
use Drupal\commerce_log\Form\LogCommentForm;
use Drupal\commerce_log\LogTemplateManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\views\Plugin\views\area\AreaPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AdminCommentForm extends AreaPluginBase {
use StringTranslationTrait;
protected $entityTypeManager;
protected $currentUser;
protected $formBuilder;
protected $logTemplateManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AccountInterface $account, FormBuilderInterface $form_builder, LogTemplateManagerInterface $log_template_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $account;
$this->formBuilder = $form_builder;
$this->logTemplateManager = $log_template_manager;
}
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'), $container
->get('current_user'), $container
->get('form_builder'), $container
->get('plugin.manager.commerce_log_template'));
}
public function render($empty = FALSE) {
if ($empty && empty($this->options['empty'])) {
return [];
}
$source_entity_id = NULL;
$source_entity_type = NULL;
foreach ($this->view->argument as $argument) {
if ($argument
->getField() === 'commerce_log.source_entity_id') {
$source_entity_id = $argument
->getValue();
}
elseif ($argument
->getField() === 'commerce_log.source_entity_type') {
$source_entity_type = $argument
->getValue();
}
}
if ($source_entity_id === NULL || $source_entity_type === NULL) {
return [];
}
$log_template_id = $source_entity_type . '_admin_comment';
if (!$this->logTemplateManager
->hasDefinition($log_template_id)) {
return [];
}
$permission = $this->currentUser
->hasPermission("add commerce_log {$source_entity_type} admin comment");
if (!$permission) {
return [];
}
$storage = $this->entityTypeManager
->getStorage($source_entity_type);
$entity = $storage
->load($source_entity_id);
if ($entity) {
$form = $this->formBuilder
->getForm(LogCommentForm::class, $source_entity_type, $source_entity_id, $log_template_id);
$form['log_comment']['comment']['#description'] = $this
->t('Your comment will only be visible to users who have access to the activity log.');
return $form;
}
return [];
}
}