Problem/Motivation
Currently, pattern fields specified for HTML content with WYSIWYG integration allow the WYSIWYG filter to be used for populating content, but back-end filters configured for the text format aren't executed. This means the WYSIWYG editor is mostly used for HTML formatting, but any post-processing done server-side isn't applied to the value before it is rendered in the pattern. Examples where this presents an issue include any filter implementations that use tokens/macros for replacement of content including:
- Media embeds
- Linkit macros
Steps to reproduce
- Enable the patternkit and patternkit_example modules
- Enable layout builder on a content type
- Create a new node of that content type
- Configure a new text format and ckeditor format that escape all HTML
- Set the new format for use under the patternkit settings form
- Edit the layout for the new node and place a new @patternkit/atoms/example/src/example' block with content in the formatted text field
- Save the layout and view the page
- Expect the formatted text to be escaped due to the text filter
Proposed resolution
Implement a processing plugin that traverses the pattern schema and applies processing of values for any properties specified to contain HTML and use the WYSIWYG. This could later be expanded to include additional plugins to act on other property types based on the schema specifications.
Remaining tasks
- Community review and testing
User interface changes
None.
API changes
A new "pattern" render element is introduced and usable with a loaded Pattern entity instance:
$library = \Drupal::service('patternkit.asset.library');
$patterns = $library->getAssets();
$id = '@patternkit/atoms/example/src/example';
$example_pattern =\ Drupal\patternkit\EntityPattern::create($patterns[$id]);
$element = [
'#type' => 'pattern',
'#pattern' => $example_pattern,
'#config' => [
'text' => 'Test text',
'formatted_text' => 'My formatted text',
]
];
A new plugin type is introduced: pattern_field_processor. Examples may be seen at src/Plugin/PatternFieldProcessor.
Release notes snippet
(Major and critical issues should have a snippet that can be pulled into the release notes when a release is created that includes the fix)
Original report
Dynamically set the field filter value based on the schema and figure out best way to implement that - whether in Drupal or patternkit module
The schema isn’t available to the preprocess function, so we need to either fix that in patternkit (patternkit contrib module) or we need to add custom code to Drupal as a stopgap
| Comment | File | Size | Author |
|---|
Issue fork patternkit-3281936
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
Comment #2
sluceroComment #4
sluceroComment #5
sluceroI'm attaching a patch for the current state of the MR as of #3 for use in Composer-based patching workflows.
If you're using cweagans/composer-patches, then you can install it by adding the following to your composer.json or composer.patches.json file depending on your configuration:
Comment #6
mariohernandez commentedComment #7
sluceroI got some reports that errors like the following were being logged when using patterns that had references:
Exception: Unknown schema type "" at cta.link.text in Drupal\patternkit\PatternFieldProcessorPluginManager->getPropertySchema() (line 147 of modules/contrib/patternkit/src/PatternFieldProcessorPluginManager.php).I was able to reproduce this and have added updates to both reproduce this in tests and resolve it. The schema traversal in PatternFieldProcessorPluginManager now recognizes references and attempts to load the referenced schema. I've updated the MR, and I'm now attaching an updated patch for testing via Composer:
There is still some more improvement that could be done to better formalize and test the schema traversal as well as improving exception handling throughout that process, but this should get the basic functionality working.
Comment #8
sluceroAfter reviewing and testing with some others, we've identified the following issues that currently exist with the implementation in this ticket:
I'll be moving forward with writing tests for each of these issues then working to resolve them.
Comment #9
sluceroI've just pushed up some more work to fix the issues noted in #8. This work adds tests to confirm the issues with traversing the schema through references, and fixes them.
I, however, wasn't able to reproduce the issue with the WYSIWYG processor running before the Token processor and outputting an "Array" string instead, but I added notes to add future validation of return values from processors being applied and changed the WYSIWYG processor to always return a rendered out string.
This work is once again ready for review and testing.
Comment #10
johnle commentedTested the work, is working great. Thanks for making this happened. One thing I've notice is that the `$tokenized[$token]` sometimes does not exist and throws a warning message
I think we can check for that via
Comment #11
sluceroThanks for the testing, feedback, and recommendation @johnle! In the work I just pushed up, I've added both a test to reproduce the issue you noted and the check for the token replacement value as you described to fix the issue. As a result of this fix, the unprocessed token value remains visible in the final output. If that's not desired, we should re-evaluate the solution for it to account for that. It's worth noting, however, that I'm planning to rewrite the token replacement logic altogether to follow core usage more closely in a follow-up issue and we may consider revising the intended behavior for it there.
This should fix the issues that have been noted so far and make this work ready for review again, but there are a couple of other tweaks I'm considering adding before this gets merged in:
PatternFieldProcessorPluginManagerComment #12
sluceroI've just pushed up the changes for the items listed in #11. With that, I consider this work feature-complete for this issue, so we'll move forward with code review and community testing before anything gets merged in.
Comment #13
sluceroI've just pushed up another round of updates addressing various code review items that were posted as well as:
Comment #14
sluceroFurther testing has highlighted issues in the schema traversal logic when encountering array property values like the following:
This is also an issue in other array cases where the values in the
anyOfmay contain references as well. In either case, the error that is output looks like this:Next Steps
PatternFieldProcessorPluginManagerto handle traversal of array properties when encountered.Comment #15
sluceroI've just pushed up a huge chunk of work refactoring the schema traversal process to use the external swaggest/json-schema library to handle schema validation and a lot of the traversal/inspection process.
This should make the schema traversal and processing much more robust than it was previously, and it should specifically fix the previously noted issues with array traversal. To address this, a new set of iterator classes and helpers have been added in src/Schema to handle the schema traversal in a more modular way. To keep the underlying implementation and use of the new library abstracted, the new services that have been created to support this have been marked as private.
Since this change now includes changes to the composer.json file including a new dependency, installation as a patch is no longer the most straightforward method of applying these changes for testing. To this end, the best way to install these changes locally is to specify the GitLab fork as a new repository in your composer.json file and install the specific branch in question. The related changes to your composer.json file are demonstrated below:
Notes:
dev-3281936-process-text-format.composer update drupal/patternkit.Comment #16
sluceroAll remaining issues that have been reported to me have been addressed and manual testing reported to me seems to be successful. If we can get an approval on this and code review, then this should be good to merge in next week barring any unforeseen issues.
Comment #17
sluceroComment #19
sluceroMerged into
9.1.xfor inclusion in Beta 4.