Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / block_content / src / Plugin / views / area / ListingEmpty.php
1 <?php
2
3 namespace Drupal\block_content\Plugin\views\area;
4
5 use Drupal\Core\Access\AccessManagerInterface;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\Core\Url;
8 use Drupal\views\Plugin\views\area\AreaPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines an area plugin to display a block add link.
13  *
14  * @ingroup views_area_handlers
15  *
16  * @ViewsArea("block_content_listing_empty")
17  */
18 class ListingEmpty extends AreaPluginBase {
19
20   /**
21    * The access manager.
22    *
23    * @var \Drupal\Core\Access\AccessManagerInterface
24    */
25   protected $accessManager;
26
27   /**
28    * The current user.
29    *
30    * @var \Drupal\Core\Session\AccountInterface
31    */
32   protected $currentUser;
33
34   /**
35    * Constructs a new ListingEmpty.
36    *
37    * @param array $configuration
38    *   A configuration array containing information about the plugin instance.
39    * @param string $plugin_id
40    *   The plugin ID for the plugin instance.
41    * @param mixed $plugin_definition
42    *   The plugin implementation definition.
43    * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
44    *   The access manager.
45    * @param \Drupal\Core\Session\AccountInterface $current_user
46    *   The current user.
47    */
48   public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, AccountInterface $current_user) {
49     parent::__construct($configuration, $plugin_id, $plugin_definition);
50
51     $this->accessManager = $access_manager;
52     $this->currentUser = $current_user;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59     return new static(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63       $container->get('access_manager'),
64       $container->get('current_user')
65     );
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function render($empty = FALSE) {
72     if (!$empty || !empty($this->options['empty'])) {
73       /** @var \Drupal\Core\Access\AccessResultInterface|\Drupal\Core\Cache\CacheableDependencyInterface $access_result */
74       $access_result = $this->accessManager->checkNamedRoute('block_content.add_page', [], $this->currentUser, TRUE);
75       $element = [
76         '#markup' => $this->t('Add a <a href=":url">custom block</a>.', [':url' => Url::fromRoute('block_content.add_page')->toString()]),
77         '#access' => $access_result->isAllowed(),
78         '#cache' => [
79           'contexts' => $access_result->getCacheContexts(),
80           'tags' => $access_result->getCacheTags(),
81           'max-age' => $access_result->getCacheMaxAge(),
82         ],
83       ];
84       return $element;
85     }
86     return [];
87   }
88
89 }