示例#1
0
 /**
  * Tests rebuild the theme data with theme parents.
  */
 public function testRebuildThemeDataWithThemeParents()
 {
     $this->extensionDiscovery->expects($this->at(0))->method('scan')->with('theme')->will($this->returnValue(array('test_subtheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml', 'test_subtheme.info.yml'), 'test_basetheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml', 'test_basetheme.info.yml'))));
     $this->extensionDiscovery->expects($this->at(1))->method('scan')->with('theme_engine')->will($this->returnValue(array('twig' => new Extension($this->root, 'theme_engine', $this->root . '/core/themes/engines/twig/twig.info.yml', 'twig.engine'))));
     $this->infoParser->expects($this->at(0))->method('parse')->with($this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml')->will($this->returnCallback(function ($file) {
         $info_parser = new InfoParser();
         return $info_parser->parse($file);
     }));
     $this->infoParser->expects($this->at(1))->method('parse')->with($this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml')->will($this->returnCallback(function ($file) {
         $info_parser = new InfoParser();
         return $info_parser->parse($file);
     }));
     $this->moduleHandler->expects($this->once())->method('buildModuleDependencies')->will($this->returnArgument(0));
     $theme_data = $this->themeHandler->rebuildThemeData();
     $this->assertCount(2, $theme_data);
     $info_basetheme = $theme_data['test_basetheme'];
     $info_subtheme = $theme_data['test_subtheme'];
     // Ensure some basic properties.
     $this->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_basetheme);
     $this->assertEquals('test_basetheme', $info_basetheme->getName());
     $this->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_subtheme);
     $this->assertEquals('test_subtheme', $info_subtheme->getName());
     // Test the parent/child-theme properties.
     $info_subtheme->info['base theme'] = 'test_basetheme';
     $info_basetheme->sub_themes = array('test_subtheme');
     $this->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_basetheme->owner);
     $this->assertEquals('twig', $info_basetheme->prefix);
     $this->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_subtheme->owner);
     $this->assertEquals('twig', $info_subtheme->prefix);
 }
 /**
  * Inject the relevant css for the template.
  *
  * You can specify CSS files to be included per entity type and bundle in your
  * themes css file. This code uses your current theme which is likely to be the
  * front end theme.
  *
  * Examples:
  *
  * entity_print:
  *   all: 'yourtheme/all-pdfs',
  *   commerce_order:
  *     all: 'yourtheme/orders'
  *   node:
  *     article: 'yourtheme/article-pdf'
  *
  * @param array $render
  *   The renderable array.
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity info from entity_get_info().
  *
  * @return array
  *   An array of stylesheets to be used for this template.
  */
 protected function addCss($render, ContentEntityInterface $entity)
 {
     $theme = $this->themeHandler->getDefault();
     $theme_path = $this->getThemePath($theme);
     /** @var \Drupal\Core\Extension\InfoParser $parser */
     $theme_info = $this->infoParser->parse("{$theme_path}/{$theme}.info.yml");
     // Parse out the CSS from the theme info.
     if (isset($theme_info['entity_print'])) {
         // See if we have the special "all" key which is added to every PDF.
         if (isset($theme_info['entity_print']['all'])) {
             $render['#attached']['library'][] = $theme_info['entity_print']['all'];
             unset($theme_info['entity_print']['all']);
         }
         foreach ($theme_info['entity_print'] as $key => $value) {
             // If the entity type doesn't match just skip.
             if ($key !== $entity->getEntityTypeId()) {
                 continue;
             }
             // Parse our css files per entity type and bundle.
             foreach ($value as $css_bundle => $css) {
                 // If it's magic key "all" add it otherwise check the bundle.
                 if ($css_bundle === 'all' || $entity->bundle() === $css_bundle) {
                     $render['#attached']['library'][] = $css;
                 }
             }
         }
     }
     return $render;
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function rebuildThemeData()
 {
     $listing = $this->getExtensionDiscovery();
     $themes = $listing->scan('theme');
     $engines = $listing->scan('theme_engine');
     $extension_config = $this->configFactory->get('core.extension');
     $installed = $extension_config->get('theme') ?: array();
     // Set defaults for theme info.
     $defaults = array('engine' => 'twig', 'regions' => array('sidebar_first' => 'Left sidebar', 'sidebar_second' => 'Right sidebar', 'content' => 'Content', 'header' => 'Header', 'primary_menu' => 'Primary menu', 'secondary_menu' => 'Secondary menu', 'footer' => 'Footer', 'highlighted' => 'Highlighted', 'messages' => 'Messages', 'help' => 'Help', 'page_top' => 'Page top', 'page_bottom' => 'Page bottom', 'breadcrumb' => 'Breadcrumb'), 'description' => '', 'features' => $this->defaultFeatures, 'screenshot' => 'screenshot.png', 'php' => DRUPAL_MINIMUM_PHP, 'libraries' => array());
     $sub_themes = array();
     $files = array();
     // Read info files for each theme.
     foreach ($themes as $key => $theme) {
         // @todo Remove all code that relies on the $status property.
         $theme->status = (int) isset($installed[$key]);
         $theme->info = $this->infoParser->parse($theme->getPathname()) + $defaults;
         // Add the info file modification time, so it becomes available for
         // contributed modules to use for ordering theme lists.
         $theme->info['mtime'] = $theme->getMTime();
         // Invoke hook_system_info_alter() to give installed modules a chance to
         // modify the data in the .info.yml files if necessary.
         // @todo Remove $type argument, obsolete with $theme->getType().
         $type = 'theme';
         $this->moduleHandler->alter('system_info', $theme->info, $theme, $type);
         if (!empty($theme->info['base theme'])) {
             $sub_themes[] = $key;
             // Add the base theme as a proper dependency.
             $themes[$key]->info['dependencies'][] = $themes[$key]->info['base theme'];
         }
         // Defaults to 'twig' (see $defaults above).
         $engine = $theme->info['engine'];
         if (isset($engines[$engine])) {
             $theme->owner = $engines[$engine]->getExtensionPathname();
             $theme->prefix = $engines[$engine]->getName();
         }
         // Prefix screenshot with theme path.
         if (!empty($theme->info['screenshot'])) {
             $theme->info['screenshot'] = $theme->getPath() . '/' . $theme->info['screenshot'];
         }
         $files[$key] = $theme->getPathname();
     }
     // Build dependencies.
     // @todo Move into a generic ExtensionHandler base class.
     // @see https://drupal.org/node/2208429
     $themes = $this->moduleHandler->buildModuleDependencies($themes);
     // Store filenames to allow system_list() and drupal_get_filename() to
     // retrieve them without having to scan the filesystem.
     $this->state->set('system.theme.files', $files);
     // After establishing the full list of available themes, fill in data for
     // sub-themes.
     foreach ($sub_themes as $key) {
         $sub_theme = $themes[$key];
         // The $base_themes property is optional; only set for sub themes.
         // @see ThemeHandlerInterface::listInfo()
         $sub_theme->base_themes = $this->getBaseThemes($themes, $key);
         // empty() cannot be used here, since ThemeHandler::doGetBaseThemes() adds
         // the key of a base theme with a value of NULL in case it is not found,
         // in order to prevent needless iterations.
         if (!current($sub_theme->base_themes)) {
             continue;
         }
         // Determine the root base theme.
         $root_key = key($sub_theme->base_themes);
         // Build the list of sub-themes for each of the theme's base themes.
         foreach (array_keys($sub_theme->base_themes) as $base_theme) {
             $themes[$base_theme]->sub_themes[$key] = $sub_theme->info['name'];
         }
         // Add the theme engine info from the root base theme.
         if (isset($themes[$root_key]->owner)) {
             $sub_theme->info['engine'] = $themes[$root_key]->info['engine'];
             $sub_theme->owner = $themes[$root_key]->owner;
             $sub_theme->prefix = $themes[$root_key]->prefix;
         }
     }
     return $themes;
 }