Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2134: Converting context relationship functions into methods. #6

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions core/modules/layout/includes/block.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,10 @@ class Block extends LayoutHandler {
* Build the settings form for editing this block.
*/
function form(&$form, &$form_state) {
/* @var Layout $layout */
$layout = $form_state['layout'];
$contexts = $layout->getContexts();

// Get contexts from relationships
layout_get_context_from_relationships($layout->relationships, $contexts);

$block_info = $this->getBlockInfo();
$current_context_settings = isset($this->settings['contexts']) ? $this->settings['contexts'] : array();

Expand Down
90 changes: 85 additions & 5 deletions core/modules/layout/includes/layout.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Layout {
*
* This represents whether this layout is has a default, user-created, or
* overridden configuration. Possible values for this variable include the
* constants LAYOUT_STORAGE_NORMAL, LAYOUT_STORAGE_OVERRIDE, or
* constants LAYOUT_STORAGE_NORMAL, LAYOUT_STORAGE_OVERRIDE, or
* LAYOUT_STORAGE_DEFAULT.
*
* @var int
Expand Down Expand Up @@ -773,9 +773,22 @@ class Layout {
/**
* Return all contexts (from both the layout and menu item) for this Layout.
*
* return LayoutContext[]
* @var int $include_types
* Include a specific list of contexts based on how they are used.
*
* This is loaded from the list of constants provided in the
* LayoutContext class, which includes the following:
*
* - USAGE_TYPE_ALL - All contexts from all possible sources.
* - 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[]
*/
function getContexts() {
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 All @@ -799,6 +812,11 @@ class Layout {
$this->contexts += layout_context_required_by_path($this->path);
}

// Load contexts from relationships.
if ($include_types & LayoutContext::USAGE_TYPE_RELATIONSHIP) {
$this->contexts += $this->getContextsFromRelationships();
}

// Add on the current user context, which is always available.
if (!isset($this->contexts['current_user'])) {
$this->contexts['current_user'] = layout_current_user_context();
Expand All @@ -812,7 +830,20 @@ class Layout {
));
}

return $this->contexts;
// 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) {
$return_contexts[$key] = $context;
}
}

return $return_contexts;
}

/**
Expand Down Expand Up @@ -856,7 +887,6 @@ class Layout {
*/
function hasContexts($required_contexts) {
$all_contexts = $this->getContexts();
layout_get_context_from_relationships($this->relationships, $all_contexts);
foreach ($required_contexts as $required_context_name) {
$context_missing = TRUE;
foreach ($all_contexts as $context) {
Expand All @@ -874,6 +904,56 @@ class Layout {
return TRUE;
}

/**
* @param LayoutRelationship $relationship
* A relationship object.
*
* @param LayoutContext $source_context
* The context this relationship is based upon.
*
* @return LayoutContext|FALSE
* A context object if one can be loaded.
*/
private function getContextFromRelationship(LayoutRelationship $relationship, LayoutContext $source_context) {
$new_context = clone($source_context);
$context = $relationship->getContext($new_context);
if ($context) {
$context->usageType = LayoutContext::USAGE_TYPE_RELATIONSHIP;
return $context;
}
return FALSE;
}

/**
* Load the contexts based on this Layout's relationship configuration.
*/
private function getContextsFromRelationships() {
$contexts = array();
$contexts_from_relationships_list = array();
foreach ($this->relationships as $relationship_name => $relationship_data) {
$key = 'relationship_' . $relationship_name;
if (!isset($contexts_from_relationships_list[$key])) {
foreach ($this->contexts as $context) {
if ($context->plugin == $relationship_data->context) {
$plugin_array = explode(':', $relationship_data->settings['relationship']);
if ($plugin_array[1] != 'child') {
$relationship_data->childDelta = $plugin_array[1];
}
$contexts_from_relationships_list[$key] = $this->getContextFromRelationship($relationship_data, $context);
}
}
}
}
if ($contexts_from_relationships_list) {
foreach ($contexts_from_relationships_list as $key => $context_from_relationships) {
if (is_object($context_from_relationships)) {
$contexts[$key] = $context_from_relationships;
}
}
}
return $contexts;
}

/**
* Check if this layout is a fallback default layout.
*/
Expand Down
6 changes: 3 additions & 3 deletions core/modules/layout/includes/layout.layout.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function layout_layout_context_info() {
* Implements hook_layout_relationship_info().
*/
function layout_layout_relationship_info() {
$info['user_from_node'] = array(
'title' => t('User from node'),
'class' => 'LayoutRelationshipUserFromNode',
$info['author_from_node'] = array(
'title' => t('Author from content'),
'class' => 'LayoutRelationshipAuthorFromNode',
'context' => 'node',
'context_label' => t('Node'),
'description' => t('Creates the author of a node as a user context.'),
Expand Down
5 changes: 4 additions & 1 deletion core/modules/layout/includes/layout_menu_item.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ class LayoutMenuItem {
// Add in configured arguments.
foreach ($this->arguments as $position => $argument_config) {
if (isset($path_parts[$position]) && $path_parts[$position] === '%') {
$context_config = $argument_config + array('required' => TRUE);
$context_config = $argument_config + array(
'required' => TRUE,
'usageType' => LayoutContext::USAGE_TYPE_MENU,
);
$contexts[$position] = layout_create_context($argument_config['plugin'], $context_config);
}
}
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
72 changes: 6 additions & 66 deletions core/modules/layout/layout.module
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ function layout_autoload_info() {
'LayoutStyleDynamic' => 'plugins/styles/layout_style_dynamic.inc',

// Relationships.
'LayoutRelationshipUserFromNode' => 'plugins/relationships/user_from_node.inc',
'LayoutRelationshipAuthorFromNode' => 'plugins/relationships/author_from_node.inc',
'LayoutRelationship' => 'plugins/relationships/layout_relationship.inc',
'LayoutRelationshipBroken' => 'plugins/relationships/layout_relationship.inc',
);
Expand Down Expand Up @@ -1260,10 +1260,10 @@ function layout_get_layout_by_path($path = NULL, $router_item = NULL) {
}
$selected_layout = NULL;
foreach ($layouts as $layout) {
// Contexts must be set before the layout's access may be checked.
$contexts = $layout->getContexts();
// Set context data from the menu path.
$menu_contexts = $layout->getContexts(LayoutContext::USAGE_TYPE_MENU);

foreach ($contexts as $context) {
foreach ($menu_contexts as $context) {
if (isset($context->position)) {

// Check for an object loaded by the menu router. Use a preview from
Expand All @@ -1286,13 +1286,6 @@ function layout_get_layout_by_path($path = NULL, $router_item = NULL) {
}
}

// Get contexts from relationships
$relationships = $layout->relationships;
layout_get_context_from_relationships($relationships, $contexts);
foreach ($contexts as $key => $context) {
$layout->setContexts($key, $context);
}

if (!$layout->disabled && $layout->checkAccess()) {
$selected_layout = $layout;
break;
Expand Down Expand Up @@ -1542,61 +1535,6 @@ function layout_relationships_get_relevant_info($contexts) {
return $relevant;
}


/**
* @param $relationship
* A relationship object.
*
* @param $source_context
* The context this relationship is based upon.
*
* @return
* A context object if one can be loaded.
*/
function layout_get_context_from_relationship($relationship, $source_context) {
$new_context = clone($source_context);
$context = $relationship->getContext($new_context);
if ($context) {
return $context;
}
return array();
}

/**
* Fetch all active relationships
*
* @param $relationships
* An keyed array of relationship objects.
*
* @param $contexts
* A keyed array of contexts used to figure out which relationships
* are relevant. New contexts will be added to this.
*/
function layout_get_context_from_relationships($relationships, &$contexts) {
$contexts_from_relationships_list = &backdrop_static('layout_get_context_from_relationships', array());
foreach ($relationships as $rname => $rdata) {
$key = 'relationship_' . $rname;
if (!isset($contexts_from_relationships_list[$key])) {
foreach ($contexts as $context) {
if ($context->plugin == $rdata->context) {
$plugin_array = explode(':', $rdata->settings['relationship']);
if ($plugin_array[1] != 'child') {
$rdata->childDelta = $plugin_array[1];
}
$contexts_from_relationships_list[$key] = layout_get_context_from_relationship($rdata, $context);
}
}
}
}
if ($contexts_from_relationships_list) {
foreach ($contexts_from_relationships_list as $key => $context_from_relationships) {
if (is_object($context_from_relationships)) {
$contexts[$key] = $context_from_relationships;
}
}
}
}

/**
* Load all layout relationship information from modules.
*
Expand Down Expand Up @@ -1950,6 +1888,7 @@ function layout_context_required_by_path($path) {
foreach ($required_context_info as $context_position => $context_info) {
$context = layout_create_context($context_info['plugin']);
$context->required = TRUE;
$context->usageType = LayoutContext::USAGE_TYPE_MENU;
// String placeholders are not locked, allowing a new value to be set.
$context->locked = $context_info['plugin'] !== 'string';
$context->position = $context_position;
Expand All @@ -1973,6 +1912,7 @@ function layout_current_user_context() {
'name' => 'current_user',
'label' => t('Current user'),
'locked' => TRUE,
'usageType' => LayoutContext::USAGE_TYPE_SYSTEM,
));
$current_user_context->setData($GLOBALS['user']);
return $current_user_context;
Expand Down
7 changes: 1 addition & 6 deletions core/modules/layout/plugins/access/layout_access.inc
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +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->getContexts();

// Get contexts from relationships
layout_get_context_from_relationships($item->relationships, $contexts);
}
$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
Loading