The Breakpoint module provides a method for creating media query breakpoint definitions within Drupal. These can be used by other components, such as the responsive image and toolbar modules, to make Drupal responsive.
Breakpoints are a type of plugin that can be defined in a module's or theme's breakpoints.yml
in its directory. In this recipe, we will define three different breakpoints under a custom group.
Ensure that the Breakpoint module is enabled. If you have used the standard Drupal installation, the module is enabled.
This recipe assumes that you have a custom module created. When you see mymodule
, use the machine name of the module that you created.
mymodule.breakpoints.yml
in your module's base directory. This file will hold the breakpoint configurations.mymodule.mobile: label: Mobile mediaQuery: '' weight: 0
mymodule.standard: label: Standard mediaQuery: 'only screen and (min-width: 60em)' weight: 1
mymodule.wide: label: Wide mediaQuery: 'only screen and (min-width: 70em)' weight: 2
The Breakpoint module defines the breakpoint configuration entity. Breakpoints do not have any specific form of direct functionalities beyond providing a way to save media queries and grouping them.
The Breakpoint module provides a default manager service. This service is used by other modules to discover breakpoint groups and then all of the breakpoints within a group.
Themes have the ability to provide breakpoints; however, they cannot be automatically discovered if new ones are added once they have been installed. Drupal only reads breakpoints provided by themes when a theme is either installed or uninstalled.
Inside breakpoint.manager
, there are two hooks: one for the theme install
and one for the theme uninstall
. Each hook retrieves the breakpoint manager service and rebuilds the breakpoint definitions. Without any extra deployment steps, new breakpoints added to a theme will not be discovered unless these hooks are fired.
Breakpoints are utility configurations for other modules. Breakpoints can be loaded by using the breakpoint manager service and specifying a group. For example, the following code returns all breakpoints used by the Toolbar module:
Drupal::service('breakpoint.manager') getBreakpointsByGroup('toolbar');
This code invokes the Drupal container to return the service to manage breakpoints, which, by default, is DrupalreakpointBreakpointManager
. The getBreakpointsByGroup
method returns all breakpoints within a group, which are initiated as the DrupalreakpointBreakpointInterface
objects.
The Toolbar element class utilizes this workflow to push the breakpoint media query values as JavaScript settings for the JavaScript model to interact with.
The multipliers value is used to support pixel resolution multipliers. This multiplier is used in coordination with retina displays. It is a measure of the viewport's device resolution as a ratio of the device's physical size and independent pixel size. The following is an example of standard multipliers: