TopicAddBlock.php in Open Social 10.2.x
Same filename and directory in other branches
- 8.9 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.2 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.3 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.4 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.5 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.6 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.7 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 8.8 modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 10.3.x modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 10.0.x modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
- 10.1.x modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.php
Namespace
Drupal\social_topic\Plugin\BlockFile
modules/social_features/social_topic/src/Plugin/Block/TopicAddBlock.phpView source
<?php
namespace Drupal\social_topic\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 'TopicAddBlock' block.
*
* @Block(
* id = "topic_add_block",
* admin_label = @Translation("Topic add block"),
* )
*/
class TopicAddBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* TopicAddBlock 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
->id() == $route_user_id && $account
->hasPermission("create topic content")) {
return AccessResult::allowed();
}
// By default, the block is not visible.
return AccessResult::forbidden();
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$url = Url::fromUserInput('/node/add/topic');
$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 Topic'), $url)
->toRenderable();
return $build;
}
}
Classes
Name | Description |
---|---|
TopicAddBlock | Provides a 'TopicAddBlock' block. |