andypost reviewed this module (in its core patch format) on #2920309-13: Add experimental module for Help Topics and suggested:

+++ b/core/modules/config_help/src/Entity/HelpTopic.php
@@ -0,0 +1,331 @@
+  public function serialize() {
...
+    return serialize(['_config_entity_serialize' => $data]);
...
+  public function unserialize($serialized) {
...
+    $this->__construct($data['values'], $data['entity_type_id']);
+    $this->enforceIsNew($data['is_new']);

andypost's suggestion: Probably it needs config_export annotation to define properties to export

jhodgdon's note: There are some @todo comments about the serialization in this topic. I had problems with it, and didn't know how to fix them without having to define serialize/unserialize methods. Maybe andypost's suggestion will fix it? There are tests that will verify it still works (there's a serialization test in there).

CommentFileSizeAuthor
#11 2920841-11.patch910 bytesjhodgdon
#8 2920841-8.patch4.21 KBjhodgdon
#2 2920841-2.patch2.13 KBjhodgdon

Comments

jhodgdon created an issue. See original summary.

jhodgdon’s picture

So... I added the config_export annotation to the config entity class, and removed the serialization methods (and the "implements Serializable" declaration).

Most of the tests pass locally. The serialization test fails with this error:

serialize(): __sleep should return an array only containing the names of instance-variables to serialize.

/home/jhodgdon/gitrepos/drupal_msq8/core/modules/config_help/tests/src/Functional/HelpTopicTest.php:136

I also manually tested using the UI to add a help topic with multiple entries, which is what initially triggered the serialization error, see
#2688730: Add new item button is not working due to serialization problem
So apparently the "add new" button (which uses Ajax in the form to add a new paragraph or whatever to the topic being edited) used to require serialization to work. Now it doesn't, because as I can see in my test, serializing this entity does not work.

As a note, my @todo comment in the serialize methods referenced Core issue #1977206: Default serialization of ConfigEntities.

So... It looks like with this patch, the module is functional, but serialization doesn't work. Should we care that serialization doesn't work? Where else is serialization needed, and can we test that whatever that is, it works without the low-level serialization test that I have now (part of HelpTopicTest)?

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Config entities are not meant to be serialized using the PHP serialize() method. When you need a "textual" (non-object) representation of an entity you should simply call ->toArray() on it. So I think it's fine to remove the serialization code from the module as well as the explicit test for it :)

jhodgdon’s picture

Great! In D8.1.x, I did have a problem in forms that they were implicitly serializing an entity if it was a #property on a form (like the Edit form for this config_help entity), and the form wouldn't work with Ajax as a result. But this seems to have been fixed in Core now.

Good to know that config entities don't need to be serialized... maybe you can add a comment to
#1977206: Default serialization of ConfigEntities
about that because obviously I don't know anything about the subject? :)

amateescu’s picture

Well, #1977206: Default serialization of ConfigEntities is now just about switching core's usage of __sleep() methods to the (new-ish) \Serializable pattern instead, but nothing is broken with the current code.

jhodgdon’s picture

Yeah, I guess the patch there basically does, in ConfigEntityBase, what this class was doing for itself.

The question I have about it is, why does Core care whether Config entities can be serialized or not? At some point in 8.1.x or so, it did matter for at least one thing: Ajax forms didn't work if they contained config entities in properties. Apparently today in 8.5.x it doesn't matter any more. I have no idea what changed in Core to make it not care any more about serializing in Ajax forms, but maybe Core doesn't care at all about serializing...

amateescu’s picture

It could have been an issue with config entities that were using a plugin, but I'm no really sure either..

jhodgdon’s picture

StatusFileSize
new4.21 KB

OK, here's the real patch, which also removes the serialization test method from HelpTopicTest.

I am actually not sure about the list in the config_export annotation though. Here's what I put in there:

+ *   config_export = {
+ *     "id",
+ *     "label",
+ *     "body",
+ *     "top_level",
+ *     "locked",
+ *     "related",
+ *     "list_on",
+ *   },

There are several other components that are in the config items exported to YAML:
- langcode
- status
- dependencies

Ah, I think this is OK, I looked at the change record for this, and it says:

The ConfigEntityBase properties uuid, langcode, status, dependencies, and third_party_settings are added by default.

https://www.drupal.org/node/2481909
Also I looked at several other config entities in Core and they don't include those properties in their annotation, so I think we're OK.

So, I went ahead and committed this to the Sandbox, and will also update the Core patch.

jhodgdon’s picture

Status: Reviewed & tested by the community » Fixed
jhodgdon’s picture

Assigned: Unassigned » jhodgdon

This patch introduced two problelms and made the tests fail on the core issue.

a) Small codings standards problem -- removing the serialize test, we don't need one use statement:

--- modules/config_help/tests/src/Functional/HelpTopicTest.php
+++ PHP_CodeSniffer
@@ -3,7 +3,6 @@
 namespace Drupal\Tests\config_help\Functional;
 
 use Drupal\Tests\BrowserTestBase;
-use Drupal\config_help\Entity\HelpTopic;
 
 /**
  * Verifies help topic display and user access to help based on permissions.

b) There was also a test failure:

Drupal\KernelTests\Config\DefaultConfigTest

exception: [Other] Line 0 of sites/default/files/simpletest/phpunit-337.xml:
PHPunit Test failed to complete; Error: PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

Testing Drupal\KernelTests\Config\DefaultConfigTest
E................................................................ 65 / 75 ( 86%)
..........

Time: 1.56 minutes, Memory: 4.00MB

There was 1 error:

1) Drupal\KernelTests\Config\DefaultConfigTest::testModuleConfig with data set "config_help" ('config_help')
Exception: config_help.topic.config_basic: Drupal\Component\Diff\Engine\DiffOpDelete::__set_state(array(
   'type' => 'delete',
   'orig' => 
  array (
    0 => 'top_level: true',
    1 => 'locked: true',
    2 => 'related: {  }',
    3 => 'list_on: {  }',
  ),
   'closing' => false,
))

/var/www/html/core/tests/Drupal/KernelTests/AssertConfigTrait.php:38
/var/www/html/core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php:142
/var/www/html/core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php:109

FAILURES!
Tests: 75, Assertions: 74, Errors: 1.

It looks like maybe we need to re-export the help topics config after this change. I'll look into this...

jhodgdon’s picture

StatusFileSize
new910 bytes

Hm.... To see what was going on, I first uninstalled and reinstalled this module so I made sure I had the config from the current config/install directory in my active config (as should be the case in the tests).

Then I installed my contrib config_update module and looked at the diffs report for config help topics. There were none. This uses the same methodology as the core test, I believe, to verify that config is the same between config/install or config/optional directories and the active storage.

Well, there is one difference: my config_update reports module doesn't consider differences in the order of config components -- the Core test is more strict. When I went to admin/config/development/configuration/single/export and exported the topic that caused the test to fail, the order of output was different from what it was before. I think this is due to the order I used in the annotation.

So here's a patch, which locally makes the export have the original order (after uninstall/reinstall). I also tried running that config test but it's sloooooow (on my slow computer, which I really ought to replace, but so it is). So I'll just let the testbot run the tests. Should be OK.

Anyway, I'll go ahead and commit these two lines of fix to the Sandbox and make a new Core patch.

  • jhodgdon committed bea8c5c on 8.x-1.x
    Issue #2920841 by jhodgdon, amateescu: Fix serialization of help topics
    

  • jhodgdon committed 1b16f47 on 8.x-1.x
    Issue #2920841 by jhodgdon: Fix serialization of help topics
    
andypost’s picture

andypost’s picture

Proper link #2922758: Fix entity serialization in edit form (new d.o path for issues confusing)

jhodgdon’s picture

@amateescu -- you might want to take a look at the discussion on #2922758-10: Fix entity serialization in edit form. It looks like HelpTopic config entities do, actually, need to be serializable via a call to PHP's serialize() function, because our editing form uses Ajax...

Status: Fixed » Closed (fixed)

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