BlocktabsBlock.php in Block Tabs 8
File
src/Plugin/Block/BlocktabsBlock.php
View source
<?php
namespace Drupal\blocktabs\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlocktabsBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $blockManager;
protected $entityTypeManager;
protected $blocktabs;
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'access content');
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->blockManager = $block_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.block'), $container
->get('entity_type.manager'));
}
public function defaultConfiguration() {
return [];
}
public function blockForm($form, FormStateInterface $form_state) {
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
}
public function build() {
$blocktabs_entity = $this
->getEntity();
$build['block'] = [
'#theme' => 'blocktabs',
'#blocktabs' => $blocktabs_entity,
];
return $build;
}
protected function getEntity() {
$id = $this
->getDerivativeId();
if (!isset($this->blocktabs)) {
$this->blocktabs = $this->entityTypeManager
->getStorage('blocktabs')
->load($id);
}
return $this->blocktabs;
}
public function getCacheContexts() {
return $this
->getEntity()
->getCacheContexts();
}
public function getCacheTags() {
return $this
->getEntity()
->getCacheTags();
}
public function getCacheMaxAge() {
return $this
->getEntity()
->getCacheMaxAge();
}
}