1

We would like to give admin users of a custom theme the ability to set a color palette for each content-type for all visitors to a site.

The color module gives a way to allow admin users some control of a single color palette for the whole website but we want to let them set a palette for each content type (or leave the base color theme for each). Including any new content types, they may create in the future.

Is this feasible?

I have been poking around the color module but I am not sure how hard it would be to write a custom module that extended it in this way. Any ideas if this is even possible?

This is for a Drupal 8 site and theme.

1

Found this after 5 seconds of googling:

The search query was: drupal color field.


  1. Choose one,
  2. add one color field per content type to the user entity type (users should be able to edit their profile),
  3. then retrieve the current user and the selected values and add them as node class via template_preprocess_node.

I may have been unclear. I don't want to let each user set their own color palette, I want users with the right permissions to be able to set the palette for an arbitrary content type for ALL users/anonymous users accessing the site. (I'll update my question) – tanbog

For that you need to create a configuration form. @see docs Working with Configuration Forms.

You can get a list of all content types to build your form. @see question Get the list of all content types.

You also may want to use the color FormElement. @see docs class Color.

In the end you probably still use template_preprocess_node, to get the stored config in there, and then set the CSS class according the current node type.

  • I may have been unclear. I don't want to let each user set their own color palette, I want users with the right permissions to be able to set the palette for an arbitrary content type for ALL users/anonymous users accessing the site. (I'll update my question) – tanbog Jan 22 at 22:38
  • 1
    @tanbog – Updated answer. – leymannx Jan 22 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.