/**
  * Prints a page listing various types of help.
  *
  * The page has sections defined by \Drupal\help\HelpSectionPluginInterface
  * plugins.
  *
  * @return array
  *   A render array for the help page.
  */
 public function helpMain()
 {
     $output = [];
     // We are checking permissions, so add the user.permissions cache context.
     $cacheability = new CacheableMetadata();
     $cacheability->addCacheContexts(['user.permissions']);
     $plugins = $this->helpManager->getDefinitions();
     $cacheability->addCacheableDependency($this->helpManager);
     foreach ($plugins as $plugin_id => $plugin_definition) {
         // Check the provided permission.
         if (!empty($plugin_definition['permission']) && !$this->currentuser()->hasPermission($plugin_definition['permission'])) {
             continue;
         }
         // Add the section to the page.
         /** @var \Drupal\help\HelpSectionPluginInterface $plugin */
         $plugin = $this->helpManager->createInstance($plugin_id);
         $this_output = ['#theme' => 'help_section', '#title' => $plugin->getTitle(), '#description' => $plugin->getDescription(), '#empty' => $this->t('There is currently nothing in this section.'), '#links' => []];
         $links = $plugin->listTopics();
         if (is_array($links) && count($links)) {
             $this_output['#links'] = $links;
         }
         $cacheability->addCacheableDependency($plugin);
         $output[$plugin_id] = $this_output;
     }
     $cacheability->applyTo($output);
     return $output;
 }
 /**
  * @covers ::addCacheableDependency
  * @dataProvider providerTestMerge
  *
  * This only tests at a high level, because it reuses existing logic. Detailed
  * tests exist for the existing logic:
  *
  * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
  * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
  * @see \Drupal\Tests\Core\Cache\CacheContextsTest
  */
 public function testAddCacheableDependency(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected)
 {
     $cache_contexts_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')->disableOriginalConstructor()->getMock();
     $container = new ContainerBuilder();
     $container->set('cache_contexts_manager', $cache_contexts_manager);
     \Drupal::setContainer($container);
     $this->assertEquals($expected, $a->addCacheableDependency($b));
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public function getActionsForRoute($route_appears)
 {
     if (!isset($this->instances[$route_appears])) {
         $route_names = array();
         $this->instances[$route_appears] = array();
         // @todo - optimize this lookup by compiling or caching.
         foreach ($this->getDefinitions() as $plugin_id => $action_info) {
             if (in_array($route_appears, $action_info['appears_on'])) {
                 $plugin = $this->createInstance($plugin_id);
                 $route_names[] = $plugin->getRouteName();
                 $this->instances[$route_appears][$plugin_id] = $plugin;
             }
         }
         // Pre-fetch all the action route objects. This reduces the number of SQL
         // queries that would otherwise be triggered by the access manager.
         if (!empty($route_names)) {
             $this->routeProvider->getRoutesByNames($route_names);
         }
     }
     $links = array();
     /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */
     foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
         $cacheability = new CacheableMetadata();
         $route_name = $plugin->getRouteName();
         $route_parameters = $plugin->getRouteParameters($this->routeMatch);
         $access = $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
         $links[$plugin_id] = array('#theme' => 'menu_local_action', '#link' => array('title' => $this->getTitle($plugin), 'url' => Url::fromRoute($route_name, $route_parameters), 'localized_options' => $plugin->getOptions($this->routeMatch)), '#access' => $access, '#weight' => $plugin->getWeight());
         $cacheability->addCacheableDependency($access);
         // For backward compatibility in 8.0.x, plugins that do not implement
         // the \Drupal\Core\Cache\CacheableDependencyInterface are assumed
         // to be cacheable forever.
         if ($plugin instanceof CacheableDependencyInterface) {
             $cacheability->addCacheableDependency($plugin);
         } else {
             $cacheability->setCacheMaxAge(Cache::PERMANENT);
         }
         $cacheability->applyTo($links[$plugin_id]);
     }
     $links['#cache']['contexts'][] = 'route';
     return $links;
 }
 /**
  * {@inheritdoc}
  */
 public function addCacheableDependency($other_object)
 {
     parent::addCacheableDependency($other_object);
     if ($other_object instanceof AttachmentsInterface) {
         $this->addAttachments($other_object->getAttachments());
     }
     return $this;
 }