PCPBlock.php in Profile Complete Percent 8
File
src/Plugin/Block/PCPBlock.php
View source
<?php
namespace Drupal\pcp\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\pcp\PcpServiceInterface;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PCPBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $accountProxy;
protected $pcpService;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $accountProxy, PcpServiceInterface $pcpService) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->accountProxy = $accountProxy;
$this->pcpService = $pcpService;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'), $container
->get('pcp.pcp_service'));
}
protected function blockAccess(AccountInterface $account) {
return $account
->isAuthenticated() ? AccessResult::allowed() : AccessResult::forbidden();
}
public function build() {
$user_id = $this->accountProxy
->id();
$user = User::load($user_id);
$pcp_data = $this->pcpService
->getCompletePercentageData($user);
if ($pcp_data['hide_pcp_block'] && $pcp_data['incomplete'] == 0 || $pcp_data['total'] == 0) {
return [];
}
$pcp_markup = [
'#theme' => 'pcp_template',
'#uid' => $pcp_data['uid'],
'#total' => $pcp_data['total'],
'#open_link' => $pcp_data['open_link'],
'#completed' => $pcp_data['completed'],
'#incomplete' => $pcp_data['incomplete'],
'#next_percent' => $pcp_data['next_percent'],
'#nextfield_name' => $pcp_data['nextfield_name'],
'#nextfield_title' => $pcp_data['nextfield_title'],
'#current_percent' => $pcp_data['current_percent'],
'#attached' => [
'library' => [
'pcp/pcp.block',
],
],
];
return $pcp_markup;
}
public function getCacheMaxAge() {
return 0;
}
}