It seems like there is an error with the context creation for the routing in this module.

I have a module that is trying to generate an url to the page with the view_mode_page route:

    $url = new Url(
      'view_mode_page.display_entity',
      [
        'view_mode' => 'apply',
        'entity_type' => 'node',
        'entity_id' => $node->id()
      ]
    );

But i get the following error:

Drupal\Component\Plugin\Exception\ContextException: The context is not a valid context. in Drupal\Core\Executable\ExecutablePluginBase->getContextDefinition() (line 184 of core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php).

Drupal\Core\Executable\ExecutablePluginBase->getContext() (Line: 116)
Drupal\Core\Executable\ExecutablePluginBase->getContextValue() (Line: 91)
Drupal\Core\Entity\Plugin\Condition\EntityBundle->evaluate() (Line: 77)
Drupal\Core\Condition\ConditionManager->execute() (Line: 84)
Drupal\Core\Condition\ConditionPluginBase->execute() (Line: 412)
Drupal\view_mode_page\Entity\ViewmodepagePattern->applies() (Line: 146)
Drupal\view_mode_page\PathProcessor\DynamicPathProcessor->processOutbound() (Line: 108)
Drupal\Core\PathProcessor\PathProcessorManager->processOutbound() (Line: 388)

I have checked and the view mode exists. This might be an issue with only newer drupal versions i only got this issue after updating core from 9.3.3 to 9.4.4

It seems to break on the check `$definition instanceof ContextAwarePluginDefinitionInterface` in the ContextAwarePluginTrait::getContextDefinition line 176.

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:

Comments

Hikkypo created an issue. See original summary.

wrg20’s picture

+1

robertmartin1961’s picture

Disabling the "pathauto" module resolved the issue for me, however it remains an issue that needs addressing as that is merely a workaround. In addition, I have the "domain" module enabled, which is known to cause issues with other modules that require workarounds.

maplinx’s picture

@robertmartin1961, thank you!
Thanks also @hikkypo for your post.
Well I thought I was doing something wrong! Was installing a new site and all looked good until I wrote a welcome page and 48hrs after, its problem solving mode! Despite figuring out the problem from the error message ("Drupal\Component\Plugin\Exception\ContextException: The context is not a valid context. in Drupal\Core\Executable\ExecutablePluginBase->getContextDefinition() (line 184 of /home/ndagr/public_html/core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php")., and uninstalling the usual culprits modules, I was no near solving it.

I understand this file or part thereof would be depreciated in dv10.

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

daveiano’s picture

Status: Active » Needs work

I opened a merge request for this.

The error seems to be related to this change record: https://www.drupal.org/node/3120980

If you visit the overview page (/admin/config/search/view-mode-page), the 'content type' condition is marked as deprecated.

The MR fixes this and also an other deprecation: The usage of ContextAwarePluginBase in src/Plugin/view_mode_page/AliasType/EntityAliasTypeBase.php.

Note: You need to re-create your view-mode pages to get this working. We should implement an update hook to replace the deprecated condition.

mistergroove’s picture

+1

Also I noted that if I use view mode page I get the error when viewing the page and also if I try to create a URL to that page.

So if my node bundle view mode page has the pattern:

/%/fulltext

And I try to create a link to the view mode page using the following code:

$fulltexturl = Url::fromUri('internal:' . $nodeurl->toString() . '/fulltext');

I get the same error:

Uncaught PHP Exception Drupal\\Component\\Plugin\\Exception\\ContextException: "The context is not a valid context." at /www/dev/project-xxx/web/core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php line 184

glynster’s picture

@daveiano thanks so much for the update and fix. I just grabbed this update and created a patch out of it as we have no idea how long it will take for the merge request to occur. Works on 2 production sites, provided you recreate the modes again.

mistergroove’s picture

@daveiano merge works great. Thanks for the patch @glynster but looks like it's missing the changes to src/Plugin/view_mode_page/AliasType/EntityAliasTypeBase.php.

As per: https://git.drupalcode.org/project/view_mode_page/-/merge_requests/3/diffs

Are these required? Apologies if i've misunderstood.

glynster’s picture

@mistergroove on my end just with the patch things were sorted and we needed a quick fix for 2 production sites. I have gone ahead and included the other file in the patch to be more complete.

mistergroove’s picture

Excellent. Thanks so much @glynster.

daveiano’s picture

@mistergroove The changes to src/Plugin/view_mode_page/AliasType/EntityAliasTypeBase.php are not needed to fix the issue. But these are Deprecations which will lead to problems in Drupal 10 and they are in the same context as this issue. So I thought I add these changes to the patch.

Sorry, I am just curious, why are you creating patch files manually? There is a "plain diff" link in the issue fork section!?

glynster’s picture

@daveiano I had no idea you could do that. Thanks for the tip, I always thought it had to be a .patch.

https://git.drupalcode.org/project/view_mode_page/-/merge_requests/3.diff

peter pulsifer’s picture

loopy1492’s picture

Just adding a static patch after @daveiano's work above...

loopy1492’s picture

StatusFileSize
new3.18 KB

For some reason I keep uploading patches that start with a space in the file name.

coaston’s picture

Status: Needs work » Needs review

#18 resolved issues in D10.

Thank you. You have saved my day!

anybody’s picture

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

Any maintainer here to merge this and tag a new 4.x release, as 4.x isn't really D10 compatible currently.

anybody’s picture

Title: Context exception when trying to create url » Context exception when trying to create url (Drupal 10 is broken!)
Priority: Major » Critical
andysipple’s picture

Getting the following error when trying to config import into a D10 site. Patch #18 is not working for me.

+------------+------------------------------+-----------+
| Collection | Config                       | Operation |
+------------+------------------------------+-----------+
|            | view_mode_page.pattern.print | Create    |
+------------+------------------------------+-----------+

 Import the listed configuration changes? (yes/no) [yes]:
 > yes

 [notice] Synchronized configuration: create view_mode_page.pattern.print.
 [notice] Finalizing configuration synchronization.

In ConfigImportCommands.php line 324:
                                                                                                                                                             
  The import failed due to the following reasons:                                                                                                            
  Unexpected error during import with operation create for view_mode_page.pattern.print: The "node_type" plugin does not exist. Valid plugin IDs   
  for Drupal\Core\Condition\ConditionManager are: config_pages_values_access, gtag_domain, gtag_language, language, current_theme, request_path, user_role,  
   webform, entity_bundle:block_content, entity_bundle:config_pages, entity_bundle:crop, entity_bundle:entity_subqueue, entity_bundle:feeds_feed, entity_bu  
  ndle:media, entity_bundle:menu_link_content, entity_bundle:node, entity_bundle:redirect, entity_bundle:taxonomy_term, entity_bundle:webform_submission, e  
  ntity_bundle:paragraph 

Turns out the config that it was trying to import the ID is wrong for D10.
Does not work id: 'node_type' works: id: 'entity_bundle:node'

nkraft’s picture

The patch in #18 PLUS the config update in #23 from AndySimple worked for me!

1) I applied patch #18 in composer.json and then by running composer update
2) Then went into my config sync folder, searched for: node_type
3) And then changed all instances related to view_mode_page from:
node_type
to
entity_bundle:node

then ran: drush cim
to import the manually modified config files
then
drush cr (as you do)

And voila! My view mode pages are back and working correctly! Thanks all, you saved my day, getting ready to take D10 live hopefully tomorrow. Cheers to you all.

stijnd’s picture

The combination of #18 and #23 also worked for me. Perfectly explained by @nkraft!

pianomansam’s picture

Status: Reviewed & tested by the community » Needs work

Changing the status back to needs work as there are unresolved questions on the merge request. Further, the merge request doesn't do anything to solve the issue raised in #23. The fix in #23 is still a manual fix that should be taken care of programmatically with a migration/update.

jschref’s picture

With a fresh install on D10 (not an upgrade from D9), patch #18 worked like a charm for me. Also fixed/is the root of this bad AJAX call:
https://www.drupal.org/project/view_mode_page/issues/3414818#comment-155...

rcodina’s picture

The patch #18 works like a charm but the MR patch doesn't apply.

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

arlina’s picture

Status: Needs work » Needs review

Updated PR to be compatible with latest release 4.0.3, as it already addresses the deprecation issues.

btully’s picture

The latest patch #18 fails to apply when using version 4.0.3 of the module through composer require.

Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2023-05-01/3300706-17-context_exception_when_trying_to_create_url_0.patch
pearls’s picture

StatusFileSize
new23.42 KB

I saw same issue as mentioned in #31. Patch #18 fails.

MR!3 ( #29) is applied but now other problems.

When creating a new pattern it shows

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "node_type" plugin does not exist

For existing patterns, I see the error mentioned in the screenshot below.
Only local images are allowed.

Versions: Drupal version 10.4.1 Viev mode page: 4.0.3

kleve’s picture

Looks like the 4.0.3 version includes the patch code. Maybe that is why you get `Could not apply patch` @btully.
Simply remove the patch should remove the error.

papagrande’s picture

Status: Needs review » Needs work

If this issue is indeed fixed, can we get a link to the issue and/or commit that updated the code?

Then we can mark this issue as "Closed (duplicate)" or "Closed (outdated)".

raphaelbertrand’s picture

The issue is not yet fully fixed in 4.0.3, whithout patch or MR applied

When creating a new pattern it shows

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "node_type" plugin does not exist

I don't have time to investigate more on this issue now but maybe the work on related issues "#3318559: Replace use of node_type condition" and " #3356400: Drupal 10 compatibility" may help to finish to solve this one, mainly the part on /src/Form/PatternEditForm.php file.

raphaelbertrand’s picture

Maybe the commit that updated the code is in #3435378 Automated Drupal 11 compatibility fixes for view_mode_page ?

edmoreta’s picture

Posting the MR changes as a patch here. It is compatible with v4.0.3

raphaelbertrand’s picture

Good idea it help to see what remain in the MR.
Maybe switch back to "need review" in order to finish with this issue and by the same time with "#3356400: Drupal 10 compatibility"
wich is almost duplicate now.

weseze’s picture

Status: Needs work » Needs review

I can confirm that the patch from #37 applies to the latest v4.0.3 version of the module and everything is working as expected.
Setting to "Needs review" so the maintainers can have a look and provide feedback, if needed.

nigelcunningham’s picture

Status: Needs review » Reviewed & tested by the community

@weseze, the status you actually want is RTBC since you've said you've confirmed it works. Updating to that.