Problem/Motivation
Rendering a referenced paragraph within a parent paragraph, which does not have mandatory fields and there for saved with empty fields which have the display organised in groups, throws an error while preprocessing on paragraphs_theme_suggestions_paragraph when field groups module is installed. Same applies for node_theme_suggestions_node. We are using version: 3.1.0 of the module ("drupal/field_group": "^3.1",).
Generally it happens because when preprocess is started, both paragraphs and node module expect on theme suggestions to have the $variables['elements'] set and it is being unset by the field_group.module on field_group_build_entity_groups:
function paragraphs_theme_suggestions_paragraph(array $variables) {
$suggestions = array();
$paragraph = $variables['elements']['#paragraph'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'paragraph__' . $sanitized_view_mode;
$suggestions[] = 'paragraph__' . $paragraph->bundle();
function node_theme_suggestions_node(array $variables) {
$suggestions = [];
$node = $variables['elements']['#node'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'node__' . $sanitized_view_mode;
$suggestions[] = 'node__' . $node->bundle();
But as `field_group_field_group_build_pre_render_alter` is being called before the `preprocess_preprocess` from ThemeManager (docroot/core/lib/Drupal/Core/Theme/ThemeManager.php ::287) and field group build pre render is calling `field_group_remove_empty_display_groups` which scans through each item of the sub (referenced) paragraph, determines that each of the fields is empty and unset the element:
docroot/modules/contrib/field_group/field_group.module::1053
// Reset an empty group.
if ($empty_group) {
$element = [];
}
It is a pointer, so when returned to ThemeManager and preprocess_preprocess is being called $variables['element'] is no longer set, there for when theme suggestions is hit, throws an error as it expects to have it.
Error: Call to a member function bundle() on null in paragraphs_theme_suggestions_paragraph() (line 97 of /var/www/mac3/docroot/modules/contrib/paragraphs/paragraphs.module)
#0 [internal function]: paragraphs_theme_suggestions_paragraph(Array)
#1 /var/www/mac3/docroot/core/lib/Drupal/Core/Extension/ModuleHandler.php(403): call_user_func_array('paragraphs_them...', Array)
#2 /var/www/mac3/docroot/modules/contrib/preprocess/src/PreprocessManager.php(67): Drupal\Core\Extension\ModuleHandler->invokeAll('theme_suggestio...', Array)
#3 /var/www/mac3/docroot/modules/contrib/preprocess/src/PreprocessManager.php(82): Drupal\preprocess\PreprocessManager->getSuggestions('paragraph', Array)
#4 /var/www/mac3/docroot/modules/contrib/preprocess/preprocess.module(23): Drupal\preprocess\PreprocessManager->preprocess('paragraph', Array)
#5 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(287): preprocess_preprocess(Array, 'paragraph', Array)
#6 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('paragraph', Array)
#7 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#8 /var/www/mac3/docroot/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#9 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(74): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#10 /var/www/mac3/vendor/twig/twig/src/Template.php(455): __TwigTemplate_3d784ff15c31c150f5bb082d4b26905526284164cdaaf8efccba14c725d5855b->doDisplay(Array, Array)
#11 /var/www/mac3/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#12 /var/www/mac3/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#13 /var/www/mac3/docroot/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#14 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#15 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('field', Array)
#16 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(444): Drupal\Core\Render\Renderer->doRender(Array)
#17 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#18 /var/www/mac3/docroot/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#19 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(103): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#20 /var/www/mac3/vendor/twig/twig/src/Template.php(216): __TwigTemplate_94db17e44ea1498098238f0242f924612d9b032f588305bebe4a7057be39a159->block_content(Array, Array)
#21 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(87): Twig\Template->displayBlock('content', Array, Array)
#22 /var/www/mac3/vendor/twig/twig/src/Template.php(216): __TwigTemplate_94db17e44ea1498098238f0242f924612d9b032f588305bebe4a7057be39a159->block_paragraph(Array, Array)
#23 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(65): Twig\Template->displayBlock('paragraph', Array, Array)
#24 /var/www/mac3/vendor/twig/twig/src/Template.php(455): __TwigTemplate_94db17e44ea1498098238f0242f924612d9b032f588305bebe4a7057be39a159->doDisplay(Array, Array)
#25 /var/www/mac3/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#26 /var/www/mac3/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#27 /var/www/mac3/docroot/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#28 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#29 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('paragraph', Array)
#30 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#31 /var/www/mac3/docroot/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#32 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(60): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#33 /var/www/mac3/vendor/twig/twig/src/Template.php(455): __TwigTemplate_725155222761f36951ec66cf25c752ef2d355eaedb9f2a7e534b845949e606c3->doDisplay(Array, Array)
#34 /var/www/mac3/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#35 /var/www/mac3/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#36 /var/www/mac3/docroot/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#37 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#38 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('field', Array)
#39 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(444): Drupal\Core\Render\Renderer->doRender(Array)
#40 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#41 /var/www/mac3/docroot/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#42 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(97): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#43 /var/www/mac3/vendor/twig/twig/src/Template.php(216): __TwigTemplate_7f0a276f84358e3adff17f96a051342a8d8b6a8f837b803466d2eeb4eb47f801->block_content(Array, Array)
#44 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(86): Twig\Template->displayBlock('content', Array, Array)
#45 /var/www/mac3/vendor/twig/twig/src/Template.php(455): __TwigTemplate_7f0a276f84358e3adff17f96a051342a8d8b6a8f837b803466d2eeb4eb47f801->doDisplay(Array, Array)
#46 /var/www/mac3/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#47 /var/www/mac3/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#48 /var/www/mac3/docroot/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#49 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#50 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('block', Array)
#51 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(444): Drupal\Core\Render\Renderer->doRender(Array)
#52 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#53 /var/www/mac3/docroot/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#54 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(126): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#55 /var/www/mac3/vendor/twig/twig/src/Template.php(455): __TwigTemplate_7ad0d0a700047f108244018f369af8a2cdab28babd86ea351fbc74fbdc183089->doDisplay(Array, Array)
#56 /var/www/mac3/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#57 /var/www/mac3/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#58 /var/www/mac3/docroot/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#59 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#60 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('layout__page_co...', Array)
#61 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(444): Drupal\Core\Render\Renderer->doRender(Array)
#62 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(444): Drupal\Core\Render\Renderer->doRender(Array)
#63 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#64 /var/www/mac3/docroot/core/lib/Drupal/Core/Template/TwigExtension.php(501): Drupal\Core\Render\Renderer->render(Array)
#65 /var/www/mac3/vendor/twig/twig/src/Environment.php(497) : eval()'d code(73): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#66 /var/www/mac3/vendor/twig/twig/src/Template.php(455): __TwigTemplate_d15a3085d956d582f9511a441e74ae8144c7d5b9f86c43cb3acaa75bb132fd02->doDisplay(Array, Array)
#67 /var/www/mac3/vendor/twig/twig/src/Template.php(422): Twig\Template->displayWithErrorHandling(Array, Array)
#68 /var/www/mac3/vendor/twig/twig/src/Template.php(434): Twig\Template->display(Array)
#69 /var/www/mac3/docroot/core/themes/engines/twig/twig.engine(64): Twig\Template->render(Array)
#70 /var/www/mac3/docroot/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/custom/s...', Array)
#71 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(431): Drupal\Core\Theme\ThemeManager->render('node', Array)
#72 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(200): Drupal\Core\Render\Renderer->doRender(Array, false)
#73 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(226): Drupal\Core\Render\Renderer->render(Array, false)
#74 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/Renderer.php(573): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#75 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(227): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#76 /var/www/mac3/docroot/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(117): Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#77 /var/www/mac3/docroot/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#78 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#79 /var/www/mac3/docroot/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#80 /var/www/mac3/vendor/symfony/http-kernel/HttpKernel.php(156): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent))
#81 /var/www/mac3/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#82 /var/www/mac3/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#83 /var/www/mac3/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#84 /var/www/mac3/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#85 /var/www/mac3/docroot/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#86 /var/www/mac3/docroot/modules/contrib/shield/src/ShieldMiddleware.php(115): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#87 /var/www/mac3/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\shield\ShieldMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#88 /var/www/mac3/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#89 /var/www/mac3/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#90 /var/www/mac3/docroot/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#91 /var/www/mac3/docroot/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#92 {main}
Notice: Undefined index: #paragraph in paragraphs_theme_suggestions_paragraph() (line 93 of modules/contrib/paragraphs/paragraphs.module).
paragraphs_theme_suggestions_paragraph(Array)
call_user_func_array('paragraphs_theme_suggestions_paragraph', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('theme_suggestions_paragraph', Array) (Line: 67)
Drupal\preprocess\PreprocessManager->getSuggestions('paragraph', Array) (Line: 82)
Drupal\preprocess\PreprocessManager->preprocess('paragraph', Array) (Line: 23)
preprocess_preprocess(Array, 'paragraph', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('paragraph', Array) (Line: 431)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200)
Drupal\Core\Render\Renderer->render(Array) (Line: 501)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 74)
__TwigTemplate_3d784ff15c31c150f5bb082d4b26905526284164cdaaf8efccba14c725d5855b->doDisplay(Array, Array) (Line: 455)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 422)
Twig\Template->display(Array) (Line: 434)
...
Steps to reproduce
- Create a paragraph with text field and an entity reference to media for image for example - all non mandatory.
- On Manage form display, for the field reference select: Paragraphs Browser EXPERIMENTAL widget and from the options select Edit Mode - Closed.
- On Manage Display add a field group and set one of the fields under it.
- Create a second paragraph (container) and provide a field reference to the already created paragraph - limited=1, non mandatory.
- Create a Content type and create new field reference to the second (container) paragraph.
- On Manage form display, for the field reference select: Paragraphs Browser EXPERIMENTAL widget and from the options select Edit Mode - Closed.
- Create new content of the newly created Content Type. For the referenced paragraph click Add Paragraph in order to load the form and the an item.
- Save page without filling anything under paragraph form.
- Open the page.
Proposed resolution
Remove the reset of element:
// Reset an empty group.
if ($empty_group) {
$element = [];
}
It would solve the error, but of course print an empty element (check screenshot) on the page. Which in my opinion is much better as it can be dealt with rather then having it broken.
Remaining tasks
Figure out how to re-implement the element removal on a stage where theme suggestions would've already passed.
I've attached a patch removing the reset of element.