I want to force all content to be created using my primary site theme. I want to do this because I am using a renderer in MYMODULE_node_presave()
like this:
function MYMODULE_node_presave(NodeInterface $node) {
$renderer = \Drupal::service('renderer');
$content_type = $node->getType();
if ($content_type == 'page') {
$viewmode_render = $node->get('field_text_main_to_render')
->view('rendered_output');
$processed_display = $renderer->renderPlain($viewmode_render);
$node->set('field_text_main_display', "$processed_display");
$node->field_text_main_display->format = 'processed';
}
My site uses Bartik as the main theme and Seven as the admin theme.
Under Appearance settings (/admin/appearance), I have checked the preference Use the administration theme when editing or creating content. This ensures that nodes are rendered in Bartik upon creation in the UI.
However, when I resave nodes on /admin/content
, the nodes are re-saved with Seven, instead of Bartik. So I added a ThemeNegotiator
:
/**
* Select the correct theme for various routes.
*/
class ThemeNegotiator implements ThemeNegotiatorInterface {
/**
* {@inheritDoc}
*/
public function applies(RouteMatchInterface $route_match) {
return $this->negotiateRoute($route_match) ? TRUE : FALSE;
}
/**
* {@inheritDoc}
*/
public function determineActiveTheme(RouteMatchInterface $route_match) {
return $this->negotiateRoute($route_match) ?: NULL;
}
/**
* Select the theme for special cases.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The currently matched route.
*
* @return bool|string
* The theme name to use (string) or false (bool).
*/
private function negotiateRoute(RouteMatchInterface $route_match) {
$route_name = $route_match->getRouteName();
// Admin content page must use node render theme.
// Why: "Save content" action will use the theme of this page.
if ($route_name === 'system.admin_content') {
return 'bartik';
}
else {
return FALSE;
}
}
}
Now, when I run a bulk node resave on /admin/content
, nodes are rendered correctly with Bartik.
Next, I installed the Feeds module. Now, when I import nodes via CSV using Feeds, they are rendered using Seven, not Bartik. I want to force these nodes to also be rendered using Bartik, but I don't know how to do that. (For example, if there was a way to use negotiateRoute
in the ThemeNegotiator
, that would be fine, but I don't know how to set that up.) Any ideas?