While debugging the Wizard API for Page Manager, I found that ContextConfigure.php has the following code at the submit method:

    if (strpos($context_definition->getDataType(), 'entity:') === 0) {
      list(, $entity_type) = explode(':', $context_definition->getDataType());
      if (is_numeric($form_state->getValue('context_value'))) {
        $value = \Drupal::entityTypeManager()->getStorage($entity_type)->load($form_state->getValue('context_value'));
      }
    }

The line is_numeric() evaluates to false when you want to add a context such as Action, which has entity ids such as comment_publish_action. This causes the following error when the context is added because the value is required:

[Wed Mar 30 14:08:00.395367 2016] [:error] [pid 1700] [client 127.0.0.1:46308] Uncaught PHP Exception Drupal\\Component\\Plugin\\Exception\\ContextException: "The 'entity:action' context is required and not present." at /var/www/drupal8/core/lib/Drupal/Core/Plugin/Context/Context.php line 76, referer: http://d8.local/admin/structure/page_manager/manage/asdfaasdfd/page_variant__asdfaasdfd-block_display-0__contexts

Issue fork ctools-2696819

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • 8.x-3.x Comparecompare
  • 2696819-cannot-add-contexts Comparecompare

Comments

juampynr created an issue. See original summary.

juampynr’s picture

Status: Active » Needs review
StatusFileSize
new853 bytes

Here is a patch that fixes it. I did not find any Web tests to adjust in order to prove it. Should we create a new test that manages contexts?

sunset_bill’s picture

Patch applies and fixes the issue I was seeing that was preventing me from adding contexts.

sunset_bill’s picture

Status: Needs review » Reviewed & tested by the community
japerry’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

hmm so while the bug is totally legit, I think the patch is concerning. There was probably? good reason for us to check for that. We might need to add some other checks to make sure the input is proper.

And yup, we also need a test.

dcam’s picture

I just ran into this bug while trying to add contexts in Page Manager. This bug essentially makes it impossible to add contexts for any config entity since their IDs are usually not (never?) numeric. I came up with the same fix as in #2.

I can't guess as to why it was written this way, but I'll try to remember to ask @EclipseGc the next time I see him.

karolus’s picture

Just testing this in Drupal 8.5.0 running on PHP 7.0.

#2 is working fine for me. What needs to be done to add this change?

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new2 KB
new2.24 KB

Patch adds proper injection of ETM service & clean-ups docs for affected lines

@japerry this class is abstract and only page manager implements this forms:
- \Drupal\page_manager_ui\Form\StaticContextConfigure
- \Drupal\page_manager_ui\Form\AddVariantStaticContextConfigure

Can you point how to write test for this form? should it live in ctools or better add test for page manager?

karolus’s picture

@andypost @japerry Is there anything I can do to help? Most of my skills are design/front-end, but if given some guidance can get things started.

Status: Needs review » Needs work

The last submitted patch, 8: 2696819-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jonathan1055’s picture

Status: Needs work » Needs review

The test fails are unrelated to this patch. Setting to 'Needs Review'

andypost’s picture

Basically it needs a simple test case to add a coverage for the form, but I found forms in page manager module only
So for me it is not clear how test could be added here for abstract class

karolus’s picture

StatusFileSize
new111.42 KB

@andypost I have an example using Panels Everywhere.

Set Display Context

This screenshot shows a successful context set for using a particular layout with Panels Everywhere.

jmuzz’s picture

Edit: My bad something else is going on in my case.

fawad ali’s picture

Its work for me the context is added but now the tokens are not available to add the panel page. As in #13 the context is added but now the fields or the full form of the basic page content type is not available.

Status: Needs review » Needs work

The last submitted patch, 8: 2696819-8.patch, failed testing. View results

artematem’s picture

Update patch for 8.x-3.1 release

shubham.prakash’s picture

Status: Needs work » Needs review
StatusFileSize
new1.37 KB

Hope this will pass the test cases.

jonathan1055’s picture

Just for the record, seems like the majority of the code added between patch #2 and patch #8 is already committed. The patch in #18 is the same as in #17 and that essentially matches the previous actual change, so that's all good.

However, from the comments in #2 and #5 we need a test case to cover this, before it is committed. Can we use an example from Panels Everywhere as per #13?

mrP made their first commit to this issue’s fork.

mrP’s picture

lamp5’s picture

Status: Needs review » Fixed

This issue is fixed in the latest release.

jonathan1055’s picture

@lamp5, is there really nothing more to do? I thought we needed test coverage, see comments #2, #5, #12, #13 and #19

jonathan1055’s picture

Actually I have just checked in 8.x-3.5 (released 16 April 2021) and the single change from patch in #2 has not been included. We still have

    // We're dealing with an entity and should make sure it's loaded.
    if (strpos($context_definition->getDataType(), 'entity:') === 0) {
      list(, $entity_type) = explode(':', $context_definition->getDataType());
      if (is_numeric($form_state->getValue('context_value'))) {
        $value = $this->entityTypeManager->getStorage($entity_type)->load($form_state->getValue('context_value'));
      }
    }

So it is still checking that the context_value is numeric. The dependency injection of EntityTypeManager was done in #3013146: Drupal dependency injection so everything else from patch #8 is included in 8.x-3.5. But the important bit about not checking for numeric has not been removed.

andypost’s picture

Status: Fixed » Needs work
Issue tags: +Needs reroll
jonathan1055’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new1.38 KB

Re-rolled #18 which has the required code change and added the function doc block. Still no tests added.

andypost’s picture

@jonathan1055 thank you

jeroen dost’s picture

The patch in #26 doesn't seem to do the work anymore after security update to v3.6. I now get this error (again):

Drupal\Component\Plugin\Exception\ContextException: The 'entity:node' context is required and not present. in Drupal\Core\Plugin\Context\Context->getContextValue() (line 73 of core/lib/Drupal/Core/Plugin/Context/Context.php).
Drupal\Component\Plugin\ContextAwarePluginBase->getContextValue('entity') (Line: 107)
Drupal\ctools\Plugin\Block\EntityView->access(Object, 1) (Line: 209)
Drupal\context\Plugin\ContextReaction\Blocks->execute(Array, Object, Array) (Line: 119)
Drupal\context\Plugin\DisplayVariant\ContextBlockPageVariant->build() (Line: 55)
Drupal\context_groups\Plugin\DisplayVariant\ContextGroupsBlockPageVariant->build() (Line: 259)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Downgrading to v3.5 solves the problem. What was changed in 3.6 and should be added to the patch (or in fact included in the next release)?

david.fcondezo’s picture

@Jeroen Dost Did you manage to find a fix for the problem after 3.6?

fskreuz’s picture

StatusFileSize
new843 bytes

Updated patch against 3.x, which is currently at 3.8.

japerry’s picture

Status: Needs review » Needs work

Before this can get committed, I'd want to see some tests.

jonathan1055’s picture

Was there a reason for patch #30 having the docblock removed that was in patch #26?

fskreuz’s picture

@jonathan1055 The re-roll was specifically meant to remove it because https://www.drupal.org/project/ctools/issues/3293702 added the docblock in 3.8 and the patch wasn't applying. It's best practice to keep patches/commits specifically about the thing it's fixing to reduce the chance of unnecessary merge conflicts.

jonathan1055’s picture

Thanks @fskreuz, yes of course, its best to keep patches focussed.