Skip to content
# Schema for the configuration files of the Breakpoint module.
breakpoint.settings:
type: mapping
label: 'Breakpoint settings'
mapping:
multipliers:
type: sequence
label: 'Assigning resolution multipliers to breakpoints'
sequence:
- type: string
label: 'Multiplier'
breakpoint.breakpoint.*.*.*:
type: mapping
label: 'Defines the Breakpoint entity'
......
......@@ -41,8 +41,30 @@ public function addBreakpointFromMediaQuery($name, $media_query);
* The breakpoint name is either the machine_name or the ID of a breakpoint.
*
* @param array $breakpoints
* Array containing breakpoints keyed by their ID.
* Array containing breakpoint objects
*
* @return \Drupal\breakpoint\Entity\BreakpointGroup
* The breakpoint group object.
*/
public function addBreakpoints($breakpoints);
/**
* Gets the array of breakpoints for the breakpoint group.
*
* @return array
* The array of breakpoints for the breakpoint group.
*/
public function getBreakpoints();
/**
* Gets a breakpoint from the breakpoint group by ID.
*
* @param string $id
* The breakpoint ID to get.
*
* @return \Drupal\breakpoint\Entity\Breakpoint|boolean
* The breakpoint or FALSE if not in the Breakpoint group.
*/
public function getBreakpointById($id);
}
......@@ -61,15 +61,25 @@ class BreakpointGroup extends ConfigEntityBase implements BreakpointGroupInterfa
*/
public $label;
/**
* The breakpoint group breakpoint IDs.
*
* @var array
* Array containing all breakpoints IDs of this group.
*
* @see \Drupal\breakpoint\Entity\Breakpoint
*/
protected $breakpoint_ids = array();
/**
* The breakpoint group breakpoints.
*
* @var array
* Array containing all breakpoints of this group.
* Array containing all breakpoints objects of this group.
*
* @see \Drupal\breakpoint\Entity\Breakpoint
*/
public $breakpoints = array();
protected $breakpoints = array();
/**
* The breakpoint group source: theme or module name. Use 'user' for
......@@ -97,7 +107,6 @@ class BreakpointGroup extends ConfigEntityBase implements BreakpointGroupInterfa
*/
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$this->loadAllBreakpoints();
}
/**
......@@ -111,10 +120,7 @@ public function save() {
if (empty($this->id)) {
$this->id = $this->sourceType . '.' . $this->source . '.' . $this->name;
}
// Only save the keys, but return the full objects.
$this->breakpoints = array_keys($this->breakpoints);
parent::save();
$this->loadAllBreakpoints();
}
/**
......@@ -156,47 +162,70 @@ public function addBreakpointFromMediaQuery($name, $media_query) {
'mediaQuery' => $media_query,
'source' => $this->name,
'sourceType' => $this->sourceType,
'weight' => count($this->breakpoints),
'weight' => count($this->breakpoint_ids),
));
$breakpoint->save();
}
$this->breakpoints[$breakpoint->id()] = $breakpoint;
return $this->addBreakpoints(array($breakpoint));
}
/**
* {@inheritdoc}
*/
public function addBreakpoints($breakpoints) {
foreach ($breakpoints as $breakpoint_name) {
// Check if breakpoint exists, assume $breakpoint_name is a machine name.
$breakpoint = entity_load('breakpoint', $this->sourceType . '.' . $this->source . '.' . $breakpoint_name);
// If the breakpoint doesn't exist, assume $breakpoint_name is an id.
if (!$breakpoint) {
$breakpoint = entity_load('breakpoint', $breakpoint_name);
}
// If the breakpoint doesn't exists, do not add it.
if ($breakpoint) {
// Add breakpoint to group.
$this->breakpoints[$breakpoint->id()] = $breakpoint;
}
foreach ($breakpoints as $breakpoint) {
// Add breakpoint to group.
$this->breakpoints[$breakpoint->id()] = $breakpoint;
$this->breakpoint_ids[] = $breakpoint->id();
}
return $this;
}
/**
* Loads all breakpoints, remove non-existing ones.
*
* @return array
* Array containing breakpoints keyed by their id.
*/
protected function loadAllBreakpoints() {
$breakpoints = $this->breakpoints;
$this->breakpoints = array();
foreach ($breakpoints as $breakpoint_id) {
$breakpoint = breakpoint_load($breakpoint_id);
if ($breakpoint) {
$this->breakpoints[$breakpoint_id] = $breakpoint;
* {@inheritdoc}
*/
public function getBreakpoints() {
if (empty($this->breakpoints)) {
foreach ($this->breakpoint_ids as $breakpoint_id) {
$breakpoint = breakpoint_load($breakpoint_id);
if ($breakpoint) {
$this->breakpoints[$breakpoint_id] = $breakpoint;
}
}
}
return $this->breakpoints;
}
/**
* {@inheritdoc}
*/
public function getBreakpointById($id) {
$breakpoints = $this->getBreakpoints();
if (isset($breakpoints[$id])) {
return $breakpoints[$id];
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getExportProperties() {
$names = array(
'id',
'uuid',
'name',
'label',
'breakpoint_ids',
'source',
'sourceType',
'status',
'langcode',
);
$properties = array();
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
}
......@@ -50,8 +50,7 @@ public function testBreakpointGroupCRUD() {
$this->verifyBreakpointGroup($group);
// Update the breakpoint group.
$group->breakpoints = array_keys($breakpoints);
$group->save();
$group->addBreakpoints($breakpoints)->save();
$this->verifyBreakpointGroup($group);
// Delete the breakpoint group.
......
......@@ -33,7 +33,6 @@ public function verifyBreakpointGroup(BreakpointGroup $group, BreakpointGroup $c
'label',
'id',
'name',
'breakpoints',
'sourceType',
);
......@@ -58,5 +57,8 @@ public function verifyBreakpointGroup(BreakpointGroup $group, BreakpointGroup $c
$this->assertEqual($compare_set->{$property}, $group->{$property}, format_string('breakpoint_group_load: Proper %property: %property1 == %property2 for breakpoint group %group.', $t_args), 'Breakpoint API');
}
}
// Ensure that the breakpoint group has the expected breakpoints.
$this->assertEqual(array_keys($compare_set->getBreakpoints()), array_keys($group->getBreakpoints()));
}
}
......@@ -47,12 +47,14 @@ public function testThemeBreakpoints() {
'sourceType' => Breakpoint::SOURCE_TYPE_THEME,
'id' => Breakpoint::SOURCE_TYPE_THEME . '.breakpoint_test_theme.breakpoint_test_theme',
));
$breakpoint_group_obj->breakpoints = array(
'theme.breakpoint_test_theme.mobile' => array(),
'theme.breakpoint_test_theme.narrow' => array(),
'theme.breakpoint_test_theme.wide' => array(),
'theme.breakpoint_test_theme.tv' => array(),
);
$breakpoint_group_obj->addBreakpoints(entity_load_multiple('breakpoint',
array(
'theme.breakpoint_test_theme.mobile',
'theme.breakpoint_test_theme.narrow',
'theme.breakpoint_test_theme.wide',
'theme.breakpoint_test_theme.tv',
)
));
// Verify we can load this breakpoint defined by the theme.
$this->verifyBreakpointGroup($breakpoint_group_obj);
......@@ -74,11 +76,13 @@ public function testThemeBreakpointGroup() {
'source' => 'breakpoint_test_theme',
'id' => Breakpoint::SOURCE_TYPE_THEME . '.breakpoint_test_theme.test',
));
$breakpoint_group_obj->breakpoints = array(
'theme.breakpoint_test_theme.mobile' => array('1.5x', '2.x'),
'theme.breakpoint_test_theme.narrow' => array(),
'theme.breakpoint_test_theme.wide' => array(),
);
$breakpoint_group_obj->addBreakpoints(entity_load_multiple('breakpoint',
array(
'theme.breakpoint_test_theme.mobile',
'theme.breakpoint_test_theme.narrow',
'theme.breakpoint_test_theme.wide',
)
));
// Verify we can load this breakpoint defined by the theme.
$this->verifyBreakpointGroup($breakpoint_group_obj);
......@@ -88,39 +92,4 @@ public function testThemeBreakpointGroup() {
$this->assertFalse(entity_load('breakpoint_group', $breakpoint_group_obj->id()), 'breakpoint_group_load: Loading a deleted breakpoint group returns false.', 'Breakpoint API');
}
/**
* Test the breakpoints defined by the custom group in the module.
*/
public function testThemeBreakpointGroupModule() {
// Call the import manually, since the testbot needs to enable the module
// first, otherwise the theme isn't detected.
_breakpoint_import_breakpoint_groups('breakpoint_theme_test', Breakpoint::SOURCE_TYPE_MODULE);
// Verify the breakpoint group 'module_test' was created by
// breakpoint_theme_test module.
$breakpoint_group_obj = entity_create('breakpoint_group', array(
'label' => 'Test Module',
'name' => 'module_test',
'sourceType' => Breakpoint::SOURCE_TYPE_MODULE,
'source' => 'breakpoint_theme_test',
'id' => Breakpoint::SOURCE_TYPE_MODULE . '.breakpoint_theme_test.module_test',
));
$breakpoint_group_obj->breakpoints = array(
'theme.breakpoint_test_theme.mobile' => array(),
'theme.breakpoint_test_theme.narrow' => array(),
'theme.breakpoint_test_theme.wide' => array(),
);
// Verify we can load this breakpoint defined by the theme.
$this->verifyBreakpointGroup($breakpoint_group_obj);
// Disable the test theme and verify the breakpoint group still exists.
theme_disable(array('breakpoint_test_theme'));
$this->assertTrue(entity_load('breakpoint_group', $breakpoint_group_obj->id()), 'Breakpoint group still exists if theme is disabled.');
// Uninstall the test module and verify the breakpoint group is deleted.
module_uninstall(array('breakpoint_theme_test'));
$this->assertFalse(entity_load('breakpoint_group', $breakpoint_group_obj->id()), 'Breakpoint group is removed if module is uninstalled.');
}
}
module_test:
label: Test Module
breakpoints:
- theme.breakpoint_test_theme.mobile
- theme.breakpoint_test_theme.narrow
- theme.breakpoint_test_theme.wide
id: theme.breakpoint_test_theme.mobile
uuid: 3ae8bfe6-496b-478c-a811-17424038f49c
name: mobile
label: mobile
mediaQuery: '(min-width: 0px)'
source: breakpoint_test_theme
sourceType: theme
weight: 0
multipliers:
1x: 1x
status: true
langcode: en
id: theme.breakpoint_test_theme.narrow
uuid: 1d791b4a-7ccf-4c93-a800-c2bc2594cc62
name: narrow
label: narrow
mediaQuery: '(min-width: 560px)'
source: breakpoint_test_theme
sourceType: theme
weight: 1
multipliers:
1x: 1x
status: true
langcode: en
id: theme.breakpoint_test_theme.tv
uuid: e0ffa737-0570-4891-9809-9bce925673ca
name: tv
label: tv
mediaQuery: 'only screen and (min-width: 3456px)'
source: breakpoint_test_theme
sourceType: theme
weight: 3
multipliers:
1x: 1x
status: true
langcode: en
id: theme.breakpoint_test_theme.wide
uuid: 1561574d-99f8-48a6-b304-4e2b617673b2
name: wide
label: wide
mediaQuery: '(min-width: 851px)'
source: breakpoint_test_theme
sourceType: theme
weight: 2
multipliers:
1x: 1x
status: true
langcode: en
id: theme.breakpoint_test_theme.breakpoint_test_theme
uuid: 94b96e6e-a032-4b29-8100-efd5bf854fd1
name: breakpoint_test_theme
label: 'Breakpoint test theme'
breakpoint_ids:
- theme.breakpoint_test_theme.mobile
- theme.breakpoint_test_theme.narrow
- theme.breakpoint_test_theme.wide
- theme.breakpoint_test_theme.tv
source: breakpoint_test_theme
sourceType: theme
status: true
langcode: en
id: theme.breakpoint_test_theme.test
uuid: fcc25180-7e18-4149-8962-98d706faa59a
name: test
label: 'Test Theme'
breakpoint_ids:
- theme.breakpoint_test_theme.mobile
- theme.breakpoint_test_theme.narrow
- theme.breakpoint_test_theme.wide
source: breakpoint_test_theme
sourceType: theme
status: true
langcode: en
mobile: '(min-width: 0px)'
narrow: '(min-width: 560px)'
wide: '(min-width: 851px)'
tv: 'only screen and (min-width: 3456px)'
......@@ -1731,3 +1731,20 @@ function comment_library_info() {
);
return $libraries;
}
/**
* #post_render_cache callback; replaces the placeholder with the comment form.
*
* @param array $context
* An array with the following keys:
* - entity_type: an entity type
* - entity_id: an entity ID
* - field_name: a comment field name
*
* @return array $element
* The updated $element.
*/
function comment_replace_form_placeholder(array $context) {
$entity = entity_load($context['entity_type'], $context['entity_id']);
return comment_add($entity, $context['field_name']);
}
......@@ -2,9 +2,7 @@
/**
* @file
* Provide views data and handlers for comment.module.
*
* @ingroup views_module_handlers
* Provide views data for comment.module.
*/
/**
......
......@@ -36,7 +36,6 @@
* fieldable = TRUE,
* translatable = TRUE,
* render_cache = FALSE,
* route_base_path = "admin/structure/comments/manage/{bundle}",
* entity_keys = {
* "id" = "cid",
* "bundle" = "field_id",
......@@ -48,7 +47,8 @@
* },
* links = {
* "canonical" = "comment.permalink",
* "edit-form" = "comment.edit_page"
* "edit-form" = "comment.edit_page",
* "admin-form" = "comment.bundle"
* }
* )
*/
......@@ -216,10 +216,10 @@ public function id() {
public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller);
global $user;
$user = \Drupal::currentUser();
if (!isset($this->status->value)) {
$this->status->value = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
$this->status->value = $user->hasPermission('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
}
if ($this->isNew()) {
// Add the comment to database. This next section builds the thread field.
......
......@@ -138,7 +138,24 @@ public function viewElements(FieldItemListInterface $items) {
if ($status == COMMENT_OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW) {
// Only show the add comment form if the user has permission.
if ($this->currentUser->hasPermission('post comments')) {
$output['comment_form'] = comment_add($entity, $field_name);
// All users in the "anonymous" role can use the same form: it is fine
// for this form to be stored in the render cache.
if ($this->currentUser->isAnonymous()) {
$output['comment_form'] = comment_add($entity, $field_name);
}
// All other users need a user-specific form, which would break the
// render cache: hence use a #post_render_cache callback.
else {
$output['comment_form'] = array(
'#type' => 'render_cache_placeholder',
'#callback' => 'comment_replace_form_placeholder',
'#context' => array(
'entity_type' => $entity->entityType(),
'entity_id' => $entity->id(),
'field_name' => $field_name
),
);
}
}
}
......