When exporting pathauto with config manager and importing it afterwards you get error:

Drupal\Component\Plugin\Exception\ContextException: Required contexts without a value: language. in Drupal\Core\Plugin\Context\ContextHandler->applyContextMapping() (line 117 of core/lib/Drupal/Core/Plugin/Context/ContextHandler.php).

I think there is a problem in the YML file, at the end of pathauto.pattern.news.yml there is a context_definition with a leading "-".

selection_logic: and
weight: -10
context_definitions:
-
id: 'node:langcode:language'
label: Language

Comments

michiellucas created an issue. See original summary.

berdir’s picture

Status: Active » Postponed (maintainer needs more info)

Import where? Maybe your target site has an old ctools version? I can't imagine this being a bug in the export/import process.

michiellucas’s picture

Working on the same code base, so importing on a production server with drush config-import and drush config-export in our deploy process.

Working for me now when i import the config (getting the fatal error on node edit forms) then visiting the patterns page and resave the pattern.
The only problem i have now is that the UUID is changed in the config on production server.

Is that the expected export in the YML file?

context_definitions:
-
id: 'node:langcode:language'
label: Language

mathiasgmeiner’s picture

I have the same issue. With a small difference: I have no yml-files, I ex- and import from the database.

xano’s picture

We are using the latest release of all modules (Ctools alpha 22 in this case), and are experiencing the same problem. I'm still digging through the code to see what happens, but the symptoms are as follows: another team member exported Pathauto pattern config entities. These include the correct context definitions for the language. However, after having installed the site from scratch and imported all our config into it, these context definitions are empty in the database. This is the only difference between the config before and after the import.

xano’s picture

Status: Postponed (maintainer needs more info) » Active

I traced this down to PathautoPattern::preSave() overriding ::$context_definitions, which means the property's values changes during save operations. Why it's emptied, though, I don't know yet.

xano’s picture

Status: Active » Needs review
StatusFileSize
new883 bytes

I think this cleans up ::preSave() a little, as ::getContexts() merges ::$contexts with those from the alias type, and then ::preSave() removes those again anyway.

eclipsegc’s picture

StatusFileSize
new4.74 KB

I apologize for the lack of interdiff, Xano's code is small enough though that I think visual is ok here. I mostly adopted it with one minor change and some added docs explaining why.

The postLoad() code looks totally non-essential in retrospect. I move the spirit of that code into the getContexts() method so that it now properly instantiates contexts for us instead of all contexts always being loaded on load. Also, I moved $this->contexts to default to NULL so that we could tell if it's been instantiated yet. This will later be set to an empty array if no context relationships were generated.

I updated relevant methods to work with the changes to getContexts().

Eclipse

berdir’s picture

Yes, that goes into the direction I was thinking about.

I'm wondering if we should even go a step further and make context definitions the only API you use to externally to set them up. then we can also avoid the logic in preSave() as the context definitions would be the only relevant definition.

If you want to add a context definition/relationship, you'd use an API to do so, which would invalidate $this->contexts so we re-create them if needed in the same object lifetime.

I'll try that out as I'm interested in seeing how that would look.

berdir’s picture

StatusFileSize
new9.98 KB
new8.71 KB

Something like this.

I also renamed context definitions to relationships, because that's what it is IMHO. it's not a context definition, just a reference token/thing + label.

Will fix up the documentation around those methods if you like the direction.

eclipsegc’s picture

A quick scan of the interdiff looks kosher to me.

Eclipse

berdir’s picture

StatusFileSize
new12.8 KB
new8.75 KB

Ok, this was a bit more complicated than expected to finish ;)

Added documentation.

Then I noticed in applies() that we didn't actually *used* getContexts(). We called it but only to get the relationships as we then again got the contexts from the typed data resolver. That means we actually created the context object at least 4 times until we actually used it. So I changed that to simply call getContexts(). And it stopped working.

It took me a long time to understand that it's because of the cached relationship contexts. Which makes no sense because if we get a different context from the alias type plugin, then obviously we can also get different values for our relationship contexts. And since my refactoring above, we don't ever call getContexts() except in applies().

Which means that we can remove the property and attempt to statically cache that information completely. I also simplified it a bit to avoid re-creating the contexts, we can change the label without that. I also opened #2663002: Support passing in a label to convertTokenToContext() to simplify this code even more.

I also added a try/catch around applyContextMapping() to make sure that even if a relationship gets missing or a condition plugin is misconfigured, we never throw an uncatched exception there that breaks the patch.

Also including an untested update function.

And despite all the added documentation, update function and exception handling, we still end up with fewer lines of code than before :)

Please test this, also updating existing configuration so we can get this in asap.

xano’s picture

StatusFileSize
new1.74 KB
new13.11 KB

I tested this on our current project and the import and export work now. This patch adds some detail to docblocks.

The remaining problem is that the format of the relationships property has changed, and its schema (ctools.relationship) no longer applies. Shall we revert the format?

xano’s picture

StatusFileSize
new1.17 KB
new13.12 KB

Actually, labels are optional and can be null as well.

xano’s picture

StatusFileSize
new558 bytes
new13.34 KB

The schema was actually valid, but since we do not use the id key anymore, berdir and I thought it best to provide our own schema (for now).

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Going to let this sit for a day or so in case Dave or someone else wants to review as it is quite a change. I think it's ready, however.

berdir’s picture

Status: Reviewed & tested by the community » Fixed

Ok, that was enough waiting, committed.

  • Berdir committed 4443037 on authored by Xano
    Issue #2660236 by Xano, Berdir, EclipseGc: Wrong export of...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.