feature_toggle
From Wikipedia
A feature toggle, (also feature switch, feature flag, feature flipper, conditional feature, etc.) is a technique in software development that attempts to provide an alternative to maintaining multiple source-code branches (known as feature branches).
http://en.wikipedia.org/wiki/Feature_toggle
This module gives non developers ability to show or hide features created & deployed by developers.
Configuration
Go to Administration » Configuration » System » Feature Toggle and you will see a listing page
which will allow you to enable or disable a feature.
Manage Features
From the listing page above, Site Administrators are capable to define new features or
Edit/Delete the existing ones.
How to get the status of a feature in PHP:
$status = \Drupal::service('feature_toggle.feature_status')->getStatus('some_feature');
if($istatus== TRUE){
//Do something
}
How to get the status of a feature in Javascript:
if(Drupal.settings.feature_toggle.enabled["site_help"]){
alert("Yes. site_help Feature is enabled");
}
Events are triggered when a feature is enabled/disabled:
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [];
$events[FeatureUpdateEvents::UPDATE][] = 'featureUpdate';
return $events;
}
/**
* Event callback to react to FeatureUpdateEvents::UPDATE event.
*/
public function featureUpdate(FeatureUpdateEvent $event) {
\Drupal::logger('feature_toggle_example')->debug($event->feature()->name());
\Drupal::logger('feature_toggle_example')->debug($event->feature()->label());
\Drupal::logger('feature_toggle_example')->debug($event->status());
}
Define access to custom routes based on feature status
It is possible to define route access rules based on the feature status.
my_module.feature_route_enabled:
path: '/access/if/feature/enabled'
defaults:
_form: '\Drupal\my_module\Form\MyModuleForm'
_title: 'Feature Enabled'
requirements:
_feature_toggle: my_feature.1Define Access to Views based on feature status
Feature Toggle provides 3 different Views Access checkers:
- Feature (Unrestricted): Only checks the feature status
- Permission + Feature: Checks the feature status + a given permission
- Permission + Role: Checks the feature status + a given role
Define Access to Blocks based on feature status
Feature Toggle provides a condition plugin that can be used for Block visibility or
other condition based plugins.
Its name is Feature Toggle and allows to define a list of features. If any of them is enabled,
the condition plugin will be evaluated to TRUE.
Access to features in Twig
Is possible to create conditional logic based on features in twig.
{% if feature_toggle_status('my_feature) %}
// Do something.
{% endif %}