Skip to content

Commit

Permalink
Issue backdrop#2134: Fixing broken handler cases, removing getAllCont…
Browse files Browse the repository at this point in the history
…exts().
  • Loading branch information
quicksketch committed May 2, 2021
1 parent 9f282fa commit b3d7575
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 37 deletions.
2 changes: 2 additions & 0 deletions core/modules/dashboard/includes/dashboard_layout_context.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Layout context handler for entities.
*/
class DashboardLayoutContext extends LayoutContext {
var $usageType = LayoutContext::USAGE_TYPE_SYSTEM;

/**
* Return the indicator for this context.
*/
Expand Down
2 changes: 1 addition & 1 deletion core/modules/layout/includes/block.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class Block extends LayoutHandler {
function form(&$form, &$form_state) {
/* @var Layout $layout */
$layout = $form_state['layout'];
$contexts = $layout->getAllContexts();
$contexts = $layout->getContexts();

$block_info = $this->getBlockInfo();
$current_context_settings = isset($this->settings['contexts']) ? $this->settings['contexts'] : array();
Expand Down
23 changes: 8 additions & 15 deletions core/modules/layout/includes/layout.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -780,17 +780,14 @@ class Layout {
* LayoutContext class, which includes the following:
*
* - USAGE_TYPE_ALL - All contexts from all possible sources.
* - USAGE_TYPE_DEFAULT - All contexts except those from relationships.
* - USAGE_TYPE_CUSTOM - Contexts manually specified in configuration.
* - USAGE_TYPE_MENU - Contexts automatically provided from a menu path.
* - USAGE_TYPE_SYSTEM - Global contexts such as logged-in user.
* - USAGE_TYPE_RELATIONSHIP - Contexts added through relationships.
*
* @return LayoutContext[]
*
* @see Layout::getAllContexts()
*/
function getContexts($include_types = LayoutContext::USAGE_TYPE_DEFAULT) {
function getContexts($include_types = LayoutContext::USAGE_TYPE_ALL) {
// Initialize a list of contexts if not defined.
if (is_null($this->contexts)) {
$this->contexts = array();
Expand Down Expand Up @@ -833,7 +830,12 @@ class Layout {
));
}

// Trim down the list of contexts to only those requested.
// Return all contexts if requested.
if ($include_types === LayoutContext::USAGE_TYPE_ALL) {
return $this->contexts;
}

// Otherwise filter down the list of contexts to only those requested.
$return_contexts = array();
foreach ($this->contexts as $key => $context) {
if ($context->usageType & $include_types) {
Expand All @@ -844,15 +846,6 @@ class Layout {
return $return_contexts;
}

/**
* Shortcut to Layout::getContexts() that loads all possible contexts.
*
* @return LayoutContext[]
*/
function getAllContexts() {
return $this->getContexts(LayoutContext::USAGE_TYPE_ALL);
}

/**
* Set the internally stored contexts.
*/
Expand Down Expand Up @@ -893,7 +886,7 @@ class Layout {
* TRUE if this layout has all the required contexts, FALSE otherwise.
*/
function hasContexts($required_contexts) {
$all_contexts = $this->getAllContexts();
$all_contexts = $this->getContexts();
foreach ($required_contexts as $required_context_name) {
$context_missing = TRUE;
foreach ($all_contexts as $context) {
Expand Down
2 changes: 1 addition & 1 deletion core/modules/layout/includes/layout.layout.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function layout_layout_context_info() {
*/
function layout_layout_relationship_info() {
$info['author_from_node'] = array(
'title' => t('User from node'),
'title' => t('Author from content'),
'class' => 'LayoutRelationshipAuthorFromNode',
'context' => 'node',
'context_label' => t('Node'),
Expand Down
10 changes: 7 additions & 3 deletions core/modules/layout/layout.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ function layout_settings_form($form, &$form_state, Layout $layout) {
'#access' => !$layout->isDefault(),
);

$contexts = $layout->getContexts();
// Get all contexts except those provided by relationships.
$contexts = $layout->getContexts(LayoutContext::USAGE_TYPE_ALL ^ LayoutContext::USAGE_TYPE_RELATIONSHIP);
$built_in_contexts = isset($contexts['overrides_path']) ? 2 : 1;
$form['context_wrapper'] = array(
'#title' => t('Contexts'),
Expand Down Expand Up @@ -379,7 +380,7 @@ function layout_settings_form($form, &$form_state, Layout $layout) {
);

foreach ($contexts as $context_key => $layout_context) {
if (!in_array($context_key, array('current_user', 'overrides_path', 'dashboard'))) {
if ($layout_context->usageType !== LayoutContext::USAGE_TYPE_SYSTEM) {
// Contexts that are locked to a particular position (such as node/%).
if ($layout_context->position && $layout_context->required) {
$form['context_wrapper']['context']['required'][$context_key]['summary'] = array(
Expand Down Expand Up @@ -445,13 +446,16 @@ function layout_settings_form($form, &$form_state, Layout $layout) {
}
}
}

$all_relationship_info = _layout_get_all_info('layout_relationship');
foreach ($layout->relationships as $relationship_key => $relationship) {
$form['context_wrapper']['context']['required'][$relationship_key]['summary'] = array(
'#markup' => $relationship->getAdminSummary(),
);
$form['context_wrapper']['context']['required'][$relationship_key]['plugin'] = array(
'#markup' => check_plain($all_relationship_info[$relationship->plugin]['title']),
'#markup' => isset($all_relationship_info[$relationship->plugin]['title']) ?
check_plain($all_relationship_info[$relationship->plugin]['title']) :
t('Broken'),
);
$form['context_wrapper']['context']['required'][$relationship_key]['operations'] = array(
'#type' => 'container',
Expand Down
4 changes: 1 addition & 3 deletions core/modules/layout/plugins/access/layout_access.inc
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ class LayoutAccess extends LayoutHandler {
/* @var Layout|LayoutMenuItem $item */
if ($form_state['menu_item']) {
$item = $form_state['menu_item'];
$contexts = $item->getContexts();
}
else {
$item = $form_state['layout'];
$contexts = $item->getAllContexts();
}
$access_info = layout_get_access_info($this->plugin);

$contexts = $item->getContexts();
$current_context_settings = isset($this->settings['contexts']) ? $this->settings['contexts'] : array();
$form['contexts'] = layout_contexts_form_element($contexts, $current_context_settings, $access_info);
}
Expand Down
9 changes: 2 additions & 7 deletions core/modules/layout/plugins/context/layout_context.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ abstract class LayoutContext extends LayoutHandler {
const USAGE_TYPE_RELATIONSHIP = 8;

/**
* The default list of context types when calling Layout::getContexts().
* All possible context types.
*/
const USAGE_TYPE_DEFAULT = self::USAGE_TYPE_CUSTOM | self::USAGE_TYPE_MENU | self::USAGE_TYPE_SYSTEM;

/**
* All possible context types, including relationships.
*/
const USAGE_TYPE_ALL = self::USAGE_TYPE_DEFAULT | self::USAGE_TYPE_RELATIONSHIP;
const USAGE_TYPE_ALL = self::USAGE_TYPE_CUSTOM | self::USAGE_TYPE_MENU | self::USAGE_TYPE_SYSTEM | self::USAGE_TYPE_RELATIONSHIP;

/**
* The name of the plugin that provides this context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @file
* Class that holds information relating to the user from node relationship.
* Provides relationship to relate the Author from a piece of content (node).
*/
class LayoutRelationshipAuthorFromNode extends LayoutRelationship {
/**
Expand Down Expand Up @@ -37,13 +37,13 @@ class LayoutRelationshipAuthorFromNode extends LayoutRelationship {
if (!empty($this->settings['context_parent'])) {
list($label, $value) = explode(':', $this->settings['context_parent']);
if ($label == 'position') {
return t('User from node in position @position', array('@position' => $value+1));
return t('Author from content in position @position', array('@position' => $value+1));
}
else {
return t('User from node @id', array('@id' => $value));
return t('Author from content @id', array('@id' => $value));
}
}
return t('User from node');
return t('Author from content');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ class LayoutRelationshipBroken extends LayoutRelationship {
function getContext(LayoutContext $source_context) {
return FALSE;
}

function getAdminSummary() {
return '<em>' . t('Missing relationship: @plugin', array('@plugin' => $this->plugin)) . '</em>';
}
function type() {
return 'broken';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class LayoutRendererStandard {
// Check access on each block.
if (empty($this->admin)) {
// Assign contexts to each block.
$layout_contexts = $this->layout->getAllContexts();
$layout_contexts = $this->layout->getContexts();

$has_contexts = TRUE;
$required_contexts = $block->getRequiredContexts();
Expand Down
2 changes: 1 addition & 1 deletion core/modules/layout/tests/layout.test
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LayoutInterfaceTest extends BackdropWebTestCase {
// Check the path first to populate available contexts.
$this->backdropPost('admin/structure/layouts/add', $edit, t('Check path'));

// Test user from node relationship.
// Test Author from Content relationship.
$edit = array();
$this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/configure', $edit, t('Add context relationship'));

Expand Down

0 comments on commit b3d7575

Please sign in to comment.