From 11b2b62921b06491bc5c5a00602759bb3c857d7d Mon Sep 17 00:00:00 2001 From: Jonathan Hunt Date: Sat, 11 Feb 2023 17:54:55 +1300 Subject: [PATCH] Update Context to 7.x-3.12 --- .../all/modules/contrib/context/context.info | 6 +-- .../modules/contrib/context/context.module | 2 +- .../context_layouts/context_layouts.info | 6 +-- .../context/context_ui/context_ui.info | 6 +-- .../plugins/context_condition_context_all.inc | 54 ++++++++++++++++++- .../plugins/context_condition_user.inc | 39 ++++++++++++-- .../context/plugins/context_reaction_block.js | 2 +- .../context/tests/context.conditions.test | 38 +++++++++++++ 8 files changed, 138 insertions(+), 15 deletions(-) diff --git a/docroot/sites/all/modules/contrib/context/context.info b/docroot/sites/all/modules/contrib/context/context.info index 9280e938..4b180195 100644 --- a/docroot/sites/all/modules/contrib/context/context.info +++ b/docroot/sites/all/modules/contrib/context/context.info @@ -8,8 +8,8 @@ files[] = tests/context.test files[] = tests/context.conditions.test files[] = tests/context.reactions.test -; Information added by Drupal.org packaging script on 2022-07-26 -version = "7.x-3.11" +; Information added by Drupal.org packaging script on 2023-02-09 +version = "7.x-3.12" core = "7.x" project = "context" -datestamp = "1658874334" +datestamp = "1675905575" diff --git a/docroot/sites/all/modules/contrib/context/context.module b/docroot/sites/all/modules/contrib/context/context.module index c83b15ef..93e95ba9 100644 --- a/docroot/sites/all/modules/contrib/context/context.module +++ b/docroot/sites/all/modules/contrib/context/context.module @@ -527,7 +527,7 @@ function context_empty($element) { /** * Get a plugin handler. */ -function context_get_plugin($type = 'condition', $key, $reset = FALSE) { +function context_get_plugin($type, $key, $reset = FALSE) { static $cache = array(); if (!isset($cache[$type][$key]) || $reset) { switch ($type) { diff --git a/docroot/sites/all/modules/contrib/context/context_layouts/context_layouts.info b/docroot/sites/all/modules/contrib/context/context_layouts/context_layouts.info index a3e18bff..d047ec4c 100644 --- a/docroot/sites/all/modules/contrib/context/context_layouts/context_layouts.info +++ b/docroot/sites/all/modules/contrib/context/context_layouts/context_layouts.info @@ -6,8 +6,8 @@ core = 7.x files[] = plugins/context_layouts_reaction_block.inc -; Information added by Drupal.org packaging script on 2022-07-26 -version = "7.x-3.11" +; Information added by Drupal.org packaging script on 2023-02-09 +version = "7.x-3.12" core = "7.x" project = "context" -datestamp = "1658874334" +datestamp = "1675905575" diff --git a/docroot/sites/all/modules/contrib/context/context_ui/context_ui.info b/docroot/sites/all/modules/contrib/context/context_ui/context_ui.info index 8f282f3e..83fd0570 100644 --- a/docroot/sites/all/modules/contrib/context/context_ui/context_ui.info +++ b/docroot/sites/all/modules/contrib/context/context_ui/context_ui.info @@ -8,8 +8,8 @@ configure = admin/structure/context files[] = context.module files[] = tests/context_ui.test -; Information added by Drupal.org packaging script on 2022-07-26 -version = "7.x-3.11" +; Information added by Drupal.org packaging script on 2023-02-09 +version = "7.x-3.12" core = "7.x" project = "context" -datestamp = "1658874334" +datestamp = "1675905575" diff --git a/docroot/sites/all/modules/contrib/context/plugins/context_condition_context_all.inc b/docroot/sites/all/modules/contrib/context/plugins/context_condition_context_all.inc index faecbb34..ad4c0fc1 100644 --- a/docroot/sites/all/modules/contrib/context/plugins/context_condition_context_all.inc +++ b/docroot/sites/all/modules/contrib/context/plugins/context_condition_context_all.inc @@ -13,8 +13,18 @@ class context_condition_context_all extends context_condition_path { // and have values set. if (!in_array($context->name, $active_contexts, TRUE) && $values = $this->fetch_from_context($context, 'values')) { + $contexts_matched = 0; + + // Count the matched contexts + foreach($values as $value) { + // Always check against the active contexts. + if ($this->match(array_keys(context_active_contexts()), array($value))) { + $contexts_matched++; + } + } + // The condition is met if all contexts are active. - if (count(array_intersect($values, $active_contexts)) == count($values)) { + if ($contexts_matched == count($values)) { $this->condition_met($context); } } @@ -26,4 +36,46 @@ class context_condition_context_all extends context_condition_path { } } } + + /** + * Retrieve all context conditions. + * + * This method is slightly adapted to context_condition::get_contexts() in + * order to ensure that a context that is used as condition in another context + * gets handled before. + */ + function get_contexts($value = NULL) { + $map = context_condition_map(); + $map = isset($map[$this->plugin]) ? $map[$this->plugin] : array(); + + $contexts = array(); + + // Add the contexts that are needed for conditions in the other contexts + // first. Start with the negated ones first, as we can not unset a met + // condition afterwards. + krsort($map); + foreach ($map as $key => $submap) { + // Negated context conditions start with a "~". + if (substr($key, 0, 1) == "~") { + $key = substr($key, 1); + } + if (!isset($contexts[$key])) { + $context = context_load($key); + // Check if context exists. This will fail for wildcards. + if ($context) { + $contexts[$context->name] = $context; + } + } + } + foreach ($map as $key => $submap) { + foreach ($submap as $name) { + if (!isset($contexts[$name])) { + $context = context_load($name); + $contexts[$context->name] = $context; + } + } + } + + return $contexts; + } } diff --git a/docroot/sites/all/modules/contrib/context/plugins/context_condition_user.inc b/docroot/sites/all/modules/contrib/context/plugins/context_condition_user.inc index f7184f00..a6d62ccc 100644 --- a/docroot/sites/all/modules/contrib/context/plugins/context_condition_user.inc +++ b/docroot/sites/all/modules/contrib/context/plugins/context_condition_user.inc @@ -20,12 +20,45 @@ class context_condition_user extends context_condition { return $values; } + function options_form($context) { + $defaults = $this->fetch_from_context($context, 'options'); + return array( + 'negate_role' => array( + '#title' => t('Make role a negative condition'), + '#type' => 'checkbox', + '#description' => t("Checking this box will make this condition fire if the user's role is NOT one of the role's checked"), + '#default_value' => isset($defaults['negate_role']) ? $defaults['negate_role'] : 0, + ), + ); + } + function execute($account) { - $roles = $account->roles; - foreach ($roles as $rid => $role) { + $all_roles = user_roles(); + $users_roles = $account->roles; + foreach ($all_roles as $rid => $role) { foreach ($this->get_contexts($role) as $context) { - $this->condition_met($context, $role); + $options = $this->fetch_from_context($context, 'options'); + if (empty($options['negate_role'])) { + if (in_array($role, $users_roles)){ + $this->condition_met($context, $role); + } + } + else { + $negate_flag = TRUE; + foreach ($this->fetch_from_context($context, 'values') as $nid => $negated_role) { + if (!in_array($negated_role, $users_roles)) { + $negate_flag &= TRUE; + } + else { + $negate_flag &= FALSE; + } + } + if ($negate_flag) { + $this->condition_met($context, $role); + } + } } } } + } diff --git a/docroot/sites/all/modules/contrib/context/plugins/context_reaction_block.js b/docroot/sites/all/modules/contrib/context/plugins/context_reaction_block.js index 42bcdfa5..8d1042c1 100644 --- a/docroot/sites/all/modules/contrib/context/plugins/context_reaction_block.js +++ b/docroot/sites/all/modules/contrib/context/plugins/context_reaction_block.js @@ -425,7 +425,7 @@ DrupalContextBlockEditor.prototype = { $('a.context_ui_dialog-stop').hide(); $('.editing-context-label').remove(); - var label = $('#context-editable-trigger-'+context+' .label').text(); + var label = $('#context-editable-trigger-'+context+' .label.top').text(); label = Drupal.t('Now editing: @label', {'@label': label}); editor.parent().parent().prepend('
' + label + '
'); diff --git a/docroot/sites/all/modules/contrib/context/tests/context.conditions.test b/docroot/sites/all/modules/contrib/context/tests/context.conditions.test index a55276b7..f18ec7b7 100644 --- a/docroot/sites/all/modules/contrib/context/tests/context.conditions.test +++ b/docroot/sites/all/modules/contrib/context/tests/context.conditions.test @@ -49,6 +49,44 @@ class ContextConditionUserTest extends DrupalWebTestCase { } } +class ContextConditionNagateUserTest extends DrupalWebTestCase { + protected $profile = 'testing'; + + public static function getInfo() { + return array( + 'name' => 'Condition: nagate user', + 'description' => 'Test nagate user condition.', + 'group' => 'Context', + ); + } + + function setUp() { + parent::setUp('context', 'ctools'); + $this->user1 = $this->drupalCreateUser(array('access content', 'administer site configuration')); + + // Create test context. + ctools_include('export'); + $this->context = ctools_export_new_object('context'); + $this->context->name = 'testcontext'; + $this->context->conditions = array('user' => array('values' => array('authenticated user' => 'authenticated user'), 'options' => array('negate_role' => 1))); + $this->context->reactions = array('debug' => array('debug' => TRUE)); + $saved = context_save($this->context); + $this->assertTrue($saved, "Context 'testcontext' saved."); + } + + function test() { + // User 1 does not trigger the context. + $this->drupalLogin($this->user1); + $this->drupalGet('node'); + $this->assertNoText('Active context: testcontext'); + + // Anonymous triggers the context. + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertText('Active context: testcontext'); + } +} + class ContextConditionUserPageTest extends DrupalWebTestCase { protected $profile = 'testing';