This project is not covered by Drupal’s security advisory policy.
Introduction
The Entity Settings module provides the ability to store settings for entities in fields. The settings can then be employed by a theme, either in a preprocess function or a Twig template, to alter HTML markup or CSS classes based on the settings values. For referenced entities, settings are available for ancestors, though this currently only works for Paragraph entities.
The module currently defines three field types for storing setting values:
- Setting List (text), which is similar to the List (text) field type provided by the Options module
- Setting List (integer), which is similar to the List (integer) field type provided by the Options module
- Setting Boolean, which is similar to the Boolean field type
The field types do not allow multiple values to be set.
Requirements
The only requirements to use this module are two modules provided by Drupal core: Field and Options.
Installation
Install the module in the usual manner, either on the site’s Extend page or with Drush.
FAQ
How to define a setting field
Add a field to an entity, such as a node, user, or paragraph, selecting one of the setting field types, which are organized under the Setting heading in the Add a new field select list. Follow the steps to configure the field as usual, entering any allowed values. The name of the field is prefixed with setting_, instead of field_.
How to pass settings through to a theme template
Implement a preprocess hook for the type of template for which the settings are needed. Retrieve the settings by passing the entity to EntitySettings::getSettings. For example, add this line to the top of a theme’s THEMENAME.theme file:
use Drupal\entity_settings\EntitySettings;
then implement hook_preprocess_HOOK as follows to make settings available for all nodes:
function THEMENAME_preprocess_node(&$variables) {
if (isset($variables['node'])) {
$variables['settings'] = EntitySettings::getSettings($variables['node']);
}
}
Settings will now be available in node Twig templates in settings. For example, if a field allowed the user to select the name of a color, it could be used in the template by accessing settings.0.color. Ancestor settings can be used by incrementing the number according to the depth of the nesting. The first two levels (0 and 1) have aliases (self and parent), so they could be used as follows: settings.self.color settings.parent.color.
Known Issues
- Nested settings are only available for paragraph entities.
- When creating a setting field, the machine name displayed has the usual field_ prefix, instead of setting_, though the latter will be the actual prefix that is used upon saving.
Maintainers
Current maintainer:
- Rick Hawkins (rlhawk) - https://www.drupal.org/user/352283
Project information
- Project categories: Site structure
- Created by rlhawk on , updated
This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.
