entity_type_access_conditions
A Drupal module that allows you to control access to entity types and their bundles based on configurable conditions.
Overview
Entity Type Access Conditions provides a flexible way to control access to entity types on your Drupal site. This module allows you to restrict operations like create, view, update, and delete for entity types and bundles based on various conditions. For example, you could restrict the creation of a specific content type to users with a certain role, or only on certain domains.
But why?
The benefit of this approach is for multi-sites, or shared codebase platforms, that share configuration across multiple sites but want some flexibility in what entity types are available. For example, if there are two sites sharing the same codebase, Site A and Site B, and only Site B should be able to create "Premium Article" content types, then using third party settings we are able to configure access to that content type based on a condition - this could be one provided by core, the contrib space, or something custom. This way both sites can still share the same configuration, as the third party settings are stored in config, but the conditions are evaluated per-site.
Requirements
- Drupal 10.x or 11.x
- PHP 8.1 or higher
- Conditions Helper
Installation
1. Download the module:
You can download the module using Composer:
composer require drupal/entity_type_access_conditions
2. Enable the module:
After downloading, enable the module using Drush or the Drupal admin interface:
drush en entity_type_access_conditions
Or, enable it through the admin interface at /admin/modules.
Configuration
The module offers a settings page to configure which conditions are available for use with restricting entity type access at: /admin/config/content/entity_type_access_conditions
Usage
1. Navigate to the configuration form for any supported entity type. For example, for a Node Type, go to Structure > Content types and edit a content type.
2. Look for the "Entity Type Access Conditions" section.
3. Add one or more conditions that determine when access should be granted.
4. Configure the condition parameters as needed.
5. Save the configuration.
Users who do not meet the configured conditions will not be able to perform restricted operations on that entity type or its bundles. Users with the 'bypass entity type access conditions' permission will always have access.
Extending the module
This module provides a plugin system to define which entity types should have access conditions. Developers can add support for new entity types by creating a YAML file in their own module named my_module.entity_type_access_conditions.yml.
In this file, you can define the entity type, the forms to alter, and which operations should be restricted.
For example, to add support for a custom entity type my_entity_type:
my_entity_type:
label: 'My Entity Type'
altered_forms:
- my_entity_type_edit_form
- my_entity_type_add_form
restricted_operations:
- view
- update
- delete
Additionally, developers can dynamically alter the list of forms that are altered using the provided hook:
/**
* Implements hook_entity_type_access_conditions_form_ids_alter().
*/
function my_module_entity_type_access_conditions_form_ids_alter(array &$form_ids): void {
// Add support for another entity type form.
$form_ids[] = 'some_other_form_id';
}
Contributing
Contributions are welcome! If you would like to contribute to this module, please follow these steps:
1. Open an issue on the issue tracker
2. Create a Merge Request or add a patch
3. Mark your issue as Needs Review.
License
This module is licensed under the GPL-2.0+ license.