VotingApiLoader.php in Votingapi Widgets 8
File
src/VotingApiLoader.php
View source
<?php
namespace Drupal\votingapi_widgets;
use Drupal\votingapi_widgets\Plugin\VotingApiWidgetManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
class VotingApiLoader implements TrustedCallbackInterface {
protected $widgetManager;
protected $entityTypeManager;
public function __construct(VotingApiWidgetManager $widget_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->widgetManager = $widget_manager;
$this->entityTypeManager = $entity_type_manager;
}
public function buildForm($plugin_id, $entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, $settings) {
$definitions = $this->widgetManager
->getDefinitions();
$entity = $this->entityTypeManager
->getStorage($entity_type)
->load($entity_id);
$plugin = $this->widgetManager
->createInstance($plugin_id, $definitions[$plugin_id]);
$fieldDefinition = $entity->{$field_name}
->getFieldDefinition();
if (empty($plugin) || empty($entity) || !$entity
->hasField($field_name)) {
return [];
}
return $plugin
->buildForm($entity_type, $entity_bundle, $entity_id, $vote_type, $field_name, unserialize($settings));
}
public static function trustedCallbacks() {
return [
'buildForm',
];
}
}