/** * 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; }
/** * {@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; }