本文整理汇总了PHP中Drupal::theme方法的典型用法代码示例。如果您正苦于以下问题:PHP Drupal::theme方法的具体用法?PHP Drupal::theme怎么用?PHP Drupal::theme使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal
的用法示例。
在下文中一共展示了Drupal::theme方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testLocalizedTokenizedString
/**
* Tests if a translated and tokenized string is properly escaped by Twig.
*
* In each assert* call we add a new line at the expected result to match the
* newline at the end of the template file.
*/
public function testLocalizedTokenizedString()
{
$tests_to_do = [1 => ['original' => 'Go to the <a href="[locale_test:security_test1]">frontpage</a>', 'replaced' => 'Go to the <a href="javascript:alert(&#039;Mooooh!&#039;);">frontpage</a>'], 2 => ['original' => 'Hello <strong>[locale_test:security_test2]</strong>!', 'replaced' => 'Hello <strong>&lt;script&gt;alert(&#039;Mooooh!&#039;);&lt;/script&gt;</strong>!']];
foreach ($tests_to_do as $i => $test) {
$original_string = $test['original'];
$rendered_original_string = \Drupal::theme()->render('locale_test_tokenized', ['content' => $original_string]);
// Twig assumes that strings are unsafe so it escapes them, and so the
// original and the rendered version should be different.
$this->assertNotEqual($rendered_original_string, $original_string . "\n", 'Security test ' . $i . ' before translation');
// Pass the original string to the t() function to get it marked as safe.
$safe_string = t($original_string);
$rendered_safe_string = \Drupal::theme()->render('locale_test_tokenized', ['content' => $safe_string]);
// t() function always marks the string as safe so it won't be escaped,
// and should be the same as the original.
$this->assertEqual($rendered_safe_string, $original_string . "\n", 'Security test ' . $i . ' after translation before token replacement');
// Replace tokens in the safe string to inject it with dangerous content.
// @see locale_test_tokens().
$unsafe_string = \Drupal::token()->replace($safe_string);
$rendered_unsafe_string = \Drupal::theme()->render('locale_test_tokenized', ['content' => $unsafe_string]);
// Token replacement changes the string so it is not marked as safe
// anymore. Check it is escaped the way we expect.
$this->assertEqual($rendered_unsafe_string, $test['replaced'] . "\n", 'Security test ' . $i . ' after translation after token replacement');
}
}
示例2: testTwigCacheOverride
/**
* Ensures Twig template cache setting can be overridden.
*/
function testTwigCacheOverride()
{
$extension = twig_extension();
$theme_handler = $this->container->get('theme_handler');
$theme_handler->install(array('test_theme'));
$theme_handler->setDefault('test_theme');
// The registry still works on theme globals, so set them here.
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->getActiveThemeByName('test_theme'));
// Reset the theme registry, so that the new theme is used.
$this->container->set('theme.registry', NULL);
// Load array of Twig templates.
// reset() is necessary to invalidate caches tagged with 'theme_registry'.
$registry = $this->container->get('theme.registry');
$registry->reset();
$templates = $registry->getRuntime();
// Get the template filename and the cache filename for
// theme_test.template_test.html.twig.
$info = $templates->get('theme_test_template_test');
$template_filename = $info['path'] . '/' . $info['template'] . $extension;
$cache_filename = $this->container->get('twig')->getCacheFilename($template_filename);
// Navigate to the page and make sure the template gets cached.
$this->drupalGet('theme-test/template-test');
$this->assertTrue(PhpStorageFactory::get('twig')->exists($cache_filename), 'Cached Twig template found.');
// Disable the Twig cache and rebuild the service container.
$parameters = $this->container->getParameter('twig.config');
$parameters['cache'] = FALSE;
$this->setContainerParameter('twig.config', $parameters);
$this->rebuildContainer();
// This should return false after rebuilding the service container.
$this->assertFalse($this->container->get('twig')->getCache(), 'Twig environment has caching disabled.');
}
示例3: activeList
/**
* @param $active_group_ids
*
* @return array
*/
public function activeList($active_group_ids) {
$active_group_ids = explode(',', $active_group_ids);
/** @var \Drupal\block_visibility_groups\Entity\BlockVisibilityGroup[] $groups */
$groups = $this->storage->loadMultiple($active_group_ids);
$edit_links = [];
foreach ($groups as $group) {
$edit_links[] = [
'#type' => 'container',
'edit' => [
'#type' => 'link',
'#title' => $group->label(),
'#url' => $group->urlInfo('edit-form'),
'#suffix' => ' - ',
],
'manage' => [
'#type' => 'link',
'#title' => t('Manage Blocks'),
'#url' => Url::fromRoute('block.admin_display_theme', [
'theme' => \Drupal::theme()->getActiveTheme()->getName(),
],
[
'query' => ['block_visibility_group' => $group->id()],
]
),
],
];
}
return $edit_links;
}
示例4: bootstrap_preprocess_region
/**
* Pre-processes variables for the "region" theme hook.
*
* See template for list of available variables.
*
* @see region.tpl.php
*
* @ingroup theme_preprocess
*/
function bootstrap_preprocess_region(&$variables)
{
$region = $variables['elements']['#region'];
$variables['region'] = $region;
$variables['content'] = $variables['elements']['#children'];
$theme = \Drupal::theme()->getActiveTheme()->getName();
// Content region.
if ($region === 'content') {
// @todo is this actually used properly?
$variables['theme_hook_suggestions'][] = 'region__no_wrapper';
} elseif ($region === 'help' && !empty($variables['content'])) {
$content = $variables['content'];
$variables['content'] = array('icon' => array('#markup' => _bootstrap_icon('question-sign')), 'content' => array('#markup' => $content));
$variables['attributes']['class'][] = 'alert';
$variables['attributes']['class'][] = 'alert-info';
$variables['attributes']['class'][] = 'messages';
$variables['attributes']['class'][] = 'info';
}
// Support for "well" classes in regions.
static $wells;
if (!isset($wells)) {
foreach (system_region_list($theme) as $name => $title) {
$wells[$name] = bootstrap_setting('region_well-' . $name);
}
}
if (!empty($wells[$region])) {
$variables['attributes']['class'][] = $wells[$region];
}
}
示例5: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('user');
// Set up a test theme that prints the user's mail field.
\Drupal::service('theme_handler')->install(array('user_test_theme'));
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('user_test_theme'));
// Clear the theme registry.
$this->container->set('theme.registry', NULL);
}
示例6: getSocialLinks
public function getSocialLinks()
{
//Base Path
global $base_url;
$theme_path = \Drupal::theme()->getActiveTheme()->getPath();
//Get Current Path
$current_path = \Drupal::service('path.current')->getPath();
$path = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
//Get Current Title
$request = \Drupal::request();
if ($route = $request->attributes->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
$title_object = \Drupal::service('title_resolver')->getTitle($request, $route);
if (is_array($title_object)) {
$title = $title_object['#markup'];
} else {
$title = $title_object;
}
}
//ShareThis Key
$settings = \Drupal::config('social_lite.settings');
$key = $settings->get('api_key');
$email = $settings->get('use_email');
$print = $settings->get('use_print');
$logo = null;
$url_share = null;
$url_email = null;
$url_print = null;
if ($settings->get('logo')) {
$logo = '&img=' . $base_url . '/' . $theme_path . '/' . $settings->get('logo');
}
$share_icon = $settings->get('share_icon');
$email_icon = $settings->get('email_icon');
$print_icon = $settings->get('print_icon');
//Check if the key has a value
if (!empty($key)) {
//Share URL
$url_share = Url::fromUri("http://www.sharethis.com/share?url={$base_url}{$path}&title=" . $title . "{$logo}&pageInfo=%7B%22hostname%22%3A%22{$base_url}%22%2C%22publisher%22%3A%22{$key}%22%7D");
}
//If Email option is selected
if ($email == 1) {
//Email URL
$url_email = Url::fromUri('mailto:?subject=' . $title . '&body=View Article ' . $base_url . $path);
}
//If Print option is selected
if ($print == 1) {
//Print URL
$url_print = Url::fromUri($base_url . $path . '/#');
}
//Values to be passed to the block
$content = array('url_share' => $url_share, 'url_email' => $url_email, 'url_print' => $url_print, 'theme_path' => $base_url . '/' . $theme_path, 'share_icon' => $share_icon, 'email_icon' => $email_icon, 'print_icon' => $print_icon);
return $content;
}
示例7: testThemeDataTypes
/**
* Test that _theme() returns expected data types.
*/
function testThemeDataTypes()
{
// theme_test_false is an implemented theme hook so \Drupal::theme() service should
// return a string, even though the theme function itself can return anything.
$foos = array('null' => NULL, 'false' => FALSE, 'integer' => 1, 'string' => 'foo');
foreach ($foos as $type => $example) {
$output = \Drupal::theme()->render('theme_test_foo', array('foo' => $example));
$this->assertTrue(is_string($output), format_string('\\Drupal::theme() returns a string for data type !type.', array('!type' => $type)));
}
// suggestionnotimplemented is not an implemented theme hook so \Drupal::theme() service
// should return FALSE instead of a string.
$output = \Drupal::theme()->render(array('suggestionnotimplemented'), array());
$this->assertIdentical($output, FALSE, '\\Drupal::theme() returns FALSE when a hook suggestion is not implemented.');
}
示例8: testStableTemplateOverrides
/**
* Ensures that Stable overrides all relevant core templates.
*/
public function testStableTemplateOverrides()
{
$registry = new Registry(\Drupal::root(), \Drupal::cache(), \Drupal::lock(), \Drupal::moduleHandler(), $this->themeHandler, \Drupal::service('theme.initialization'), 'stable');
$registry->setThemeManager(\Drupal::theme());
$registry_full = $registry->get();
foreach ($registry_full as $hook => $info) {
if (isset($info['template'])) {
// Allow skipping templates.
if (in_array($info['template'], $this->templatesToSkip)) {
continue;
}
$this->assertEquals('core/themes/stable', $info['theme path'], $info['template'] . '.html.twig overridden in Stable.');
}
}
}
示例9: onRequest
/**
* Initializes devel module requirements.
*/
public function onRequest(GetResponseEvent $event)
{
if ($this->account->hasPermission('access devel information')) {
devel_set_handler(devel_get_handlers());
// See http://www.firephp.org/HQ/Install.htm
$path = NULL;
if (@(include_once 'fb.php') || @(include_once 'FirePHPCore/fb.php')) {
// FirePHPCore is in include_path. Probably a PEAR installation.
$path = '';
} elseif ($this->moduleHandler->moduleExists('libraries')) {
// Support Libraries API - http://drupal.org/project/libraries
$firephp_path = libraries_get_path('FirePHPCore');
$firephp_path = $firephp_path ? $firephp_path . '/lib/FirePHPCore/' : '';
$chromephp_path = libraries_get_path('chromephp');
} else {
$firephp_path = DRUPAL_ROOT . '/libraries/FirePHPCore/lib/FirePHPCore/';
$chromephp_path = './' . drupal_get_path('module', 'devel') . '/chromephp';
}
// Include FirePHP if it exists.
if (!empty($firephp_path) && file_exists($firephp_path . 'fb.php')) {
include_once $firephp_path . 'fb.php';
include_once $firephp_path . 'FirePHP.class.php';
}
// Include ChromePHP if it exists.
if (!empty($chromephp_path) && file_exists($chromephp_path .= '/ChromePhp.php')) {
include_once $chromephp_path;
}
}
if ($this->config->get('rebuild_theme')) {
drupal_theme_rebuild();
// Ensure that the active theme object is cleared.
$theme_name = \Drupal::theme()->getActiveTheme()->getName();
\Drupal::state()->delete('theme.active_theme.' . $theme_name);
\Drupal::theme()->resetActiveTheme();
/** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler*/
$theme_handler = \Drupal::service('theme_handler');
$theme_handler->refreshInfo();
// @todo This is not needed after https://www.drupal.org/node/2330755
$list = $theme_handler->listInfo();
$theme_handler->addTheme($list[$theme_name]);
if (\Drupal::service('flood')->isAllowed('devel.rebuild_theme_warning', 1)) {
\Drupal::service('flood')->register('devel.rebuild_theme_warning');
if ($this->account->hasPermission('access devel information')) {
drupal_set_message(t('The theme information is being rebuilt on every request. Remember to <a href=":url">turn off</a> this feature on production websites.', array(':url' => $this->urlGenerator->generateFromRoute('devel.admin_settings'))));
}
}
}
}
示例10: testMaintenanceTheme
/**
* Tests that the maintenance theme initializes the theme and its base themes.
*/
public function testMaintenanceTheme()
{
$this->setSetting('maintenance_theme', 'seven');
// Get the maintenance theme loaded.
drupal_maintenance_theme();
// Do we have an active theme?
$this->assertTrue(\Drupal::theme()->hasActiveTheme());
$active_theme = \Drupal::theme()->getActiveTheme();
$this->assertEquals('seven', $active_theme->getName());
$base_themes = $active_theme->getBaseThemes();
$base_theme_names = array_keys($base_themes);
$this->assertSame(['classy', 'stable'], $base_theme_names);
// Ensure Classy has the correct base themes and amount of base themes.
$classy_base_themes = $base_themes['classy']->getBaseThemes();
$classy_base_theme_names = array_keys($classy_base_themes);
$this->assertSame(['stable'], $classy_base_theme_names);
}
示例11: testDrupalAlter
/**
* Tests if the theme has been altered.
*/
function testDrupalAlter()
{
// This test depends on Bartik, so make sure that it is always the current
// active theme.
\Drupal::service('theme_handler')->install(array('bartik'));
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('bartik'));
$array = array('foo' => 'bar');
$entity = new \stdClass();
$entity->foo = 'bar';
// Verify alteration of a single argument.
$array_copy = $array;
$array_expected = array('foo' => 'Drupal theme');
\Drupal::moduleHandler()->alter('drupal_alter', $array_copy);
\Drupal::theme()->alter('drupal_alter', $array_copy);
$this->assertEqual($array_copy, $array_expected, 'Single array was altered.');
$entity_copy = clone $entity;
$entity_expected = clone $entity;
$entity_expected->foo = 'Drupal theme';
\Drupal::moduleHandler()->alter('drupal_alter', $entity_copy);
\Drupal::theme()->alter('drupal_alter', $entity_copy);
$this->assertEqual($entity_copy, $entity_expected, 'Single object was altered.');
// Verify alteration of multiple arguments.
$array_copy = $array;
$array_expected = array('foo' => 'Drupal theme');
$entity_copy = clone $entity;
$entity_expected = clone $entity;
$entity_expected->foo = 'Drupal theme';
$array2_copy = $array;
$array2_expected = array('foo' => 'Drupal theme');
\Drupal::moduleHandler()->alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
\Drupal::theme()->alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
$this->assertEqual($array_copy, $array_expected, 'First argument to \\Drupal::moduleHandler->alter() was altered.');
$this->assertEqual($entity_copy, $entity_expected, 'Second argument to \\Drupal::moduleHandler->alter() was altered.');
$this->assertEqual($array2_copy, $array2_expected, 'Third argument to \\Drupal::moduleHandler->alter() was altered.');
// Verify alteration order when passing an array of types to \Drupal::moduleHandler->alter().
// common_test_module_implements_alter() places 'block' implementation after
// other modules.
$array_copy = $array;
$array_expected = array('foo' => 'Drupal block theme');
\Drupal::moduleHandler()->alter(array('drupal_alter', 'drupal_alter_foo'), $array_copy);
\Drupal::theme()->alter(array('drupal_alter', 'drupal_alter_foo'), $array_copy);
$this->assertEqual($array_copy, $array_expected, 'hook_TYPE_alter() implementations ran in correct order.');
}
示例12: setUp
/**
* {@inheritdoc}
*/
public function setUp($processor = NULL)
{
parent::setUp('rendered_item');
// Load configuration and needed schemas. (The necessary schemas for using
// nodes are already installed by the parent method.)
$this->installConfig(array('system', 'filter', 'node', 'comment'));
$this->installSchema('system', array('router'));
\Drupal::service('router.builder')->rebuild();
// Create a node type for testing.
$type = NodeType::create(array('type' => 'page', 'name' => 'page'));
$type->save();
node_add_body_field($type);
// Create anonymous user role.
$role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous'));
$role->save();
// Insert the anonymous user into the database.
$anonymous_user = User::create(array('uid' => 0, 'name' => ''));
$anonymous_user->save();
// Default node values for all nodes we create below.
$node_data = array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => '', 'body' => array('value' => '', 'summary' => '', 'format' => 'plain_text'), 'uid' => $anonymous_user->id());
// Create some test nodes with valid user on it for rendering a picture.
$node_data['title'] = 'Title for node 1';
$node_data['body']['value'] = 'value for node 1';
$node_data['body']['summary'] = 'summary for node 1';
$this->nodes[1] = Node::create($node_data);
$this->nodes[1]->save();
$node_data['title'] = 'Title for node 2';
$node_data['body']['value'] = 'value for node 2';
$node_data['body']['summary'] = 'summary for node 2';
$this->nodes[2] = Node::create($node_data);
$this->nodes[2]->save();
// Set proper configuration for the tested processor.
$config = $this->processor->getConfiguration();
$config['view_mode'] = array('entity:node' => ['page' => 'full', 'article' => 'teaser'], 'entity:user' => 'compact', 'entity:comment' => 'teaser');
$config['roles'] = array($role->id());
$this->processor->setConfiguration($config);
$this->index->save();
$this->index->getDatasources();
// Enable the classy theme as the tests rely on markup from that.
\Drupal::service('theme_handler')->install(array('classy'));
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('classy'));
}
示例13: testThemeDataTypes
/**
* Test that ThemeManager renders the expected data types.
*/
function testThemeDataTypes()
{
// theme_test_false is an implemented theme hook so \Drupal::theme() service
// should return a string or an object that implements MarkupInterface,
// even though the theme function itself can return anything.
$foos = array('null' => NULL, 'false' => FALSE, 'integer' => 1, 'string' => 'foo', 'empty_string' => '');
foreach ($foos as $type => $example) {
$output = \Drupal::theme()->render('theme_test_foo', array('foo' => $example));
$this->assertTrue($output instanceof MarkupInterface || is_string($output), format_string('\\Drupal::theme() returns an object that implements MarkupInterface or a string for data type @type.', array('@type' => $type)));
if ($output instanceof MarkupInterface) {
$this->assertIdentical((string) $example, $output->__toString());
} elseif (is_string($output)) {
$this->assertIdentical($output, '', 'A string will be return when the theme returns an empty string.');
}
}
// suggestionnotimplemented is not an implemented theme hook so \Drupal::theme() service
// should return FALSE instead of a string.
$output = \Drupal::theme()->render(array('suggestionnotimplemented'), array());
$this->assertIdentical($output, FALSE, '\\Drupal::theme() returns FALSE when a hook suggestion is not implemented.');
}
示例14: testCurrentTheme
/**
* Tests the current theme condition.
*/
public function testCurrentTheme()
{
\Drupal::service('theme_handler')->install(array('test_theme'));
$manager = \Drupal::service('plugin.manager.condition');
/** @var $condition \Drupal\Core\Condition\ConditionInterface */
$condition = $manager->createInstance('current_theme');
$condition->setConfiguration(array('theme' => 'test_theme'));
/** @var $condition_negated \Drupal\Core\Condition\ConditionInterface */
$condition_negated = $manager->createInstance('current_theme');
$condition_negated->setConfiguration(array('theme' => 'test_theme', 'negate' => TRUE));
$this->assertEqual($condition->summary(), SafeMarkup::format('The current theme is @theme', array('@theme' => 'test_theme')));
$this->assertEqual($condition_negated->summary(), SafeMarkup::format('The current theme is not @theme', array('@theme' => 'test_theme')));
// The expected theme has not been set up yet.
$this->assertFalse($condition->execute());
$this->assertTrue($condition_negated->execute());
// Set the expected theme to be used.
\Drupal::service('theme_handler')->setDefault('test_theme');
\Drupal::theme()->resetActiveTheme();
$this->assertTrue($condition->execute());
$this->assertFalse($condition_negated->execute());
}
示例15: getTheme
/**
* Retrieves a theme instance of \Drupal\materialize.
*
* @param string $name
* The machine name of a theme. If omitted, the active theme will be used.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler object.
*
* @return \Drupal\materialize\Theme
* A theme object.
*/
public static function getTheme($name = NULL, ThemeHandlerInterface $theme_handler = NULL)
{
// Immediately return if theme passed is already instantiated.
if ($name instanceof Theme) {
return $name;
}
static $themes = [];
static $active_theme;
if (!isset($active_theme)) {
$active_theme = \Drupal::theme()->getActiveTheme()->getName();
}
if (!isset($name)) {
$name = $active_theme;
}
if (!isset($theme_handler)) {
$theme_handler = self::getThemeHandler();
}
if (!isset($themes[$name])) {
$themes[$name] = new Theme($theme_handler->getTheme($name), $theme_handler);
}
return $themes[$name];
}