English version was created automatically using Drupal module auto_node_translate and free DeepL translator.

How to display the selected node using a different appearance theme

zveřejněno 2022-05-18

Using ThemeNegotiatorInterface we can change the theme of the appearance according to the conditions we define. This time we will show how to make a checkbox to select any node to be displayed using the admin theme.

Last time we showed how to use theme negotiators (ThemeNegotiatorInterface) to set a different theme for the selected content type. But sometimes we want to use different theme's for individual nodes. Here even the Theme Switcher Rules module can't help us. Fortunately, a simple custom module will solve the problem for us.

 

Identifying a node for a different theme using the contrib module

First, we somehow need to mark our selected nodes that it is for them that we want to apply a different default look. To do this we can use two existing modules with similar name and functionality.

The first module is Custom Publishing Options https://www.drupal.org/project/custom_pub known since Drupal 7. It is very simple, you just set the name of the option and that's it. To define a new option, hook_entity_base_field_info() is activated, which adds a new field to the node_field_data table.

The second module is Publishing Options https://www.drupal.org/project/pub_options. This allows you to set options for selected content types. It stores the settings in its own tables.

Identifying a node for another topic using custom code

If we want to have full control over the functionality, we can use a custom module to help us. Here again we use the hook_entity_base_field_info() function, but we can configure it in detail - see the BaseFieldDefinition class https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21….

Our hook might then look like this:

/**
 * Implements hook_entity_base_field_info().
 */
function my_module_entity_base_field_info(EntityTypeInterface $entity_type)
{
   if ($entity_type->id() == 'node') {
     $fields = [];
     $fields['use_admin_theme'] = BaseFieldDefinition::create('boolean')
         ->setLabel(t('Use admin theme'))
          ->setDescription(t('If checked, this page will display using admin theme.'))
          ->setSettings(['on_label' => 'Use admin theme', 'off_label' => 'Use default front-end theme']]
          ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
      'weight' => 2,
          ])
        ->setDisplayConfigurable('form', true)
         ->setDisplayConfigurable('view', false)
         ->setDefaultValue(false);
      return $fields;
   }
}

A nice article about baseFieldDefinition is here https://fivejars.com/blog/entity-basefielddefinitions-fields-examples-d….

As with the Custom Publishing Options module, the new field shows up for all existing content types. Thanks to the setDisplayConfigurable('form', true) definition, we can configure its visibility on the Manage form display page.

Manage form display

 

We can see it as a regular checkbox when creating/editing a node

Create Article

 

If we want to display it, for example. Promotion options, we need to use hook_form_BASE_FORM_ID_alter() like this:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function my_module_form_form_alter(&$form, $form_state, $form_id)
{
    $form['use_admin_theme']['#group'] = 'options';
}

Promotion options

 

Implementation of ThemeNegotiatorInterface

We've already last time shown how to use a custom ThemeNegotiatorInterface. Now we'll just tweak the previous code a bit.

Our admin theme condition will use the value of our field

         if ($node->is_admin_route->value) {

      return true;
         }

Force admin theme

Of course, we don't need to create any custom fields for certain cases, and we can just use the nid ($node->id()) as a condition.

 

Conclusion

Today we have shown how to use the admin theme - by using ThemeNegotiatorInterface - for selected nodes, which we can choose using a new custom field that we have defined using BaseFieldDefinition.

We can also use the existing Custom Publishing Options contrib modules and also Theme Switcher Rules, but for that we would need to write a custom condition, which should be possible using RulesConditionBasehttps://www.drupal.org/docs/contributed-modules/rules-essentials/for-de….