Use JSON_PRETTY_PRINT to encode JSON so config files are more readable and diffs work

Created on 13 March 2023, over 1 year ago
Updated 13 June 2023, about 1 year ago

Problem/Motivation

Currently, the configuration files for TB Mega Menu store JSON in the block_config and menu_config keys. When config is exported, these values are each output as one big quoted string on a single line. This makes it so that Git sees any change to these values as just changing that line, so commands like git diff and drush cex --diff are useless for actually tracking any changes made to the menu.

Steps to reproduce

1. Export configuration.
2. Look at the mess of a huge amount of JSON on a single line.
3. Be sad.

Current configuration:

langcode: en
status: true
dependencies:
  theme:
    - mytheme
  menu:
    - main
id: main__mytheme
menu: main
theme: mytheme
block_config: '{"animation":"none","duration":400,"delay":200,"breakpoint":1199,"auto-arrow":"0","always-show-submenu":"0","number-columns":29}'
menu_config: '{"menu_link_content:5ad54cce-0460-4597-822b-99f699080428":{"rows_content":[[{"col_content":[{ ... 100K+ OF DATA ... ,"submenu_config":{"class":""},"item_config":{"caption":"","class":"","xicon":"","label":""}}}'

Proposed resolution

If we output the JSON using the JSON_PRETTY_PRINT option of json_encode() , it will be more usable. It would be readable and changes could be tracked in Git.

Configuration after change:

langcode: en
status: true
dependencies:
  theme:
    - mytheme
  menu:
    - main
id: main__mytheme
menu: main
theme: mytheme
block_config: |-
  {
      "animation": "none",
      "duration": 400,
      "delay": 200,
      "breakpoint": 1199,
      "auto-arrow": "0",
      "always-show-submenu": "0",
      "number-columns": 36
  }

menu_config: |-
  {
      "menu_link_content:5ad54cce-0460-4597-822b-99f699080428": {
          "rows_content": [
              [
                  {
                      "col_content": [
                          {

                          ... 100K+ OF DATA ... 

                          {
                              "type": "menu_item",
                              "plugin_id": "menu_link_content:15f691be-2dc6-4c73-bdf3-fe65575f345d",
                              "tb_item_config": [],
                              "weight": "50"
                          }
                      ],
                      "col_config": []
                  }
              ]
          ]
      }
  }

Config diff after change:

Now, you can see only what actually changed.

$ drush cex --diff
 [notice] Differences of the active config to the export directory:
diff --git a/tmp/drush_tmp_1678736783_640f7d8fe0b6d/tb_megamenu.menu_config.main__mytheme.yml b/tmp/drush_tmp_1678736784_640f7d90016f4/tb_megamenu.menu_config.main__mytheme.yml
index cba8ad4b..10303d11 100644
--- a/tmp/drush_tmp_1678736783_640f7d8fe0b6d/tb_megamenu.menu_config.main__mytheme.yml
+++ b/tmp/drush_tmp_1678736784_640f7d90016f4/tb_megamenu.menu_config.main__mytheme.yml
@@ -2038,7 +2038,7 @@ menu_config: |-
               "alignsub": "",
               "group": "0",
               "hidewcol": "0",
-              "hidesub": "0",
+              "hidesub": "1",
               "label": ""
           }
       },

Data model changes

The format of the output will not change how things actually work.

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jrb Raleigh-Durham Area, NC, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024