Adding a Tab without an existing Channel Layout throws an error
Description of the problem If a Channel doesn't have a Publish Layout created, then when viewing a saved entry with a custom Tab throws an error:
Error at https://github.com/ExpressionEngine/ExpressionEngine/blob/7.dev/system/ee/ExpressionEngine/Model/Content/Display/DefaultLayout.php#L72 because $fields[$field_id] is not set.
How To Reproduce Steps to reproduce the behavior:
- Create a custom field with a tab as described at https://docs.expressionengine.com/latest/development/tab-files.html
- Install the add-on/field
- Create a new channel and don't create a Publish Layout
- Create a new entry in that channel. The create page should load as expected. But after you click Save and it reloads the new entry, the error should appear.
Error Messages
Error Caught
Call to a member function getId() on null
ee/ExpressionEngine/Model/Content/Display/LayoutTab.php:49
Stack Trace: Please include when reporting this error
#0 ee/ExpressionEngine/Model/Content/Display/DefaultLayout.php(73): ExpressionEngine\Model\Content\Display\LayoutTab->addField(NULL)
#1 ee/ExpressionEngine/Model/Channel/Display/DefaultChannelLayout.php(229): ExpressionEngine\Model\Content\Display\DefaultLayout->transform(Array)
#2 ee/ExpressionEngine/Model/Channel/ChannelEntry.php(835): ExpressionEngine\Model\Channel\Display\DefaultChannelLayout->transform(Array)
#3 ee/ExpressionEngine/Controller/Publish/Edit.php(509): ExpressionEngine\Model\Channel\ChannelEntry->getDisplay(Object(ExpressionEngine\Model\Channel\Display\DefaultChannelLayout))
#4 [internal function]: ExpressionEngine\Controller\Publish\Edit->entry('83676')
#5 ee/ExpressionEngine/Core/Core.php(268): call_user_func_array(Array, Array)
#6 ee/ExpressionEngine/Core/Core.php(124): ExpressionEngine\Core\Core->runController(Array)
#7 ee/ExpressionEngine/Boot/boot.php(184): ExpressionEngine\Core\Core->run(Object(ExpressionEngine\Core\Request))
Environment Details:
- Version: 7.3.12
- PHP Version: 8.0.30
- Web Server: Apache
Possible Solution The solution is to create a Publish Layout for the channel. But that isn't obvious given the error message. I'd suggest either a more graceful error in this case to guide the user to create a Publish Layout manually, or to handle the error gracefully in code so it doesn't actually prevent the entry from loading.
@swierczek does your upd. file include tabs() method and install() method has necessary code as according to https://docs.expressionengine.com/latest/development/add-on-update-file.html#add-publish-tabs-with-your-add-on-tabs ?
@intoeetive I tried adding the tabs() function to the upd file, but that didn't seem to change anything here. I haven't tried adjusting the install/update functions though, as this error happens when the channel doesn't have any layouts .
As soon as I create a layout, then it works as expected (even without tabs()).
@swierczek I believe this still belongs to the side of add-ons. If the add-on provides tabs, the upd file should consider that as per the docs