social_lms_integrator-1.0.0-beta4/modules/social_lms_integrator_language_version/src/Plugin/Block/LanguageVersionAddBlock.php
modules/social_lms_integrator_language_version/src/Plugin/Block/LanguageVersionAddBlock.php
<?php
namespace Drupal\social_lms_integrator_language_version\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a 'LanguageVersionAddBlock' block.
*
* @Block(
* id = "language_version_add_block",
* admin_label = @Translation("Language Version add block"),
* )
*/
class LanguageVersionAddBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* LanguageVersionAddBlock constructor.
*
* @param array $configuration
* The given configuration.
* @param string $plugin_id
* The given plugin id.
* @param mixed $plugin_definition
* The given plugin definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The route match.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $routeMatch) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $routeMatch;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match')
);
}
/**
* {@inheritdoc}
*
* Custom access logic to display the block only on current user Topic page.
*/
protected function blockAccess(AccountInterface $account) {
$route_user_id = $this->routeMatch->getParameter('user');
if ($account->hasPermission('create language_version content')) {
return AccessResult::allowed();
}
// By default, the block is not visible.
return AccessResult::forbidden();
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$url = Url::fromUserInput('/node/add/language_version');
$link_options = [
'attributes' => [
'class' => [
'btn',
'btn-primary',
'btn-raised',
'waves-effect',
'brand-bg-primary',
],
],
];
$url->setOptions($link_options);
$build['content'] = Link::fromTextAndUrl($this->t('Create Language Version'), $url)
->toRenderable();
return $build;
}
}
