本文整理汇总了PHP中Drupal\Core\Cache\CacheableMetadata::applyTo方法的典型用法代码示例。如果您正苦于以下问题:PHP CacheableMetadata::applyTo方法的具体用法?PHP CacheableMetadata::applyTo怎么用?PHP CacheableMetadata::applyTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Cache\CacheableMetadata
的用法示例。
在下文中一共展示了CacheableMetadata::applyTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: helpMain
/**
* 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;
}
示例2: getActionsForRoute
/**
* {@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;
}
示例3: applyTo
/**
* Applies the values of this bubbleable metadata object to a render array.
*
* @param array &$build
* A render array.
*/
public function applyTo(array &$build)
{
parent::applyTo($build);
$build['#attached'] = $this->attachments;
}
示例4: applyTo
/**
* Applies the values of this bubbleable metadata object to a render array.
*
* @param array &$build
* A render array.
*/
public function applyTo(array &$build)
{
parent::applyTo($build);
$build['#attached'] = $this->attached;
$build['#post_render_cache'] = $this->postRenderCache;
}