Summary: the simplesharer.theme.inc file is throwing a Notice the first time a page loads that uses this Module if/when all of the social-media endpoints are not enabled.

Example Notice:
Undefined variable: ... in simplesharer_content() (line ### of modules/contrib/simplesharer/includes/simplesharer.theme.inc).

Version: 8.x-1.3

I know this is a Notice and that most people (should) turn off Notices & Warnings in Production environments making this Issue not critical by any means. Further, this Notice only shows up the first time a page using this Module is loaded due to caching. But a simple code-change seems to be enough to fix this Notice as well as clean-up some of the code.

In particular, it's nice to not have Notices and Warnings not show up when demo-ing a site to a client... explaining that the "errors are not really errors" is sub-optimal.

---------------------------------------

Example of the entire 'error' for a single social-media item:

Notice: Undefined variable: linkedin_button_array in simplesharer_content() (line 165 of modules/contrib/simplesharer/includes/simplesharer.theme.inc).
simplesharer_content(Array) (Line: 143)
Drupal\simplesharer\Plugin\Block\SimpleSharerBlock->build() (Line: 203)
Drupal\block\BlockViewBuilder::preRender(Array)
call_user_func('Drupal\block\BlockViewBuilder::preRender', Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 450)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 104)
__TwigTemplate_e436107d8014a72a4269492c73cb4b259c88bc54c03736a1686a5d35d1b2557d->doDisplay(Array, Array) (Line: 432)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template->display(Array) (Line: 411)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/themes/bartik/templates/page.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 90)
__TwigTemplate_be8c7bbb9c824f2826368d7c8da984c6279779db72a67fd8056a00bb23b816f2->doDisplay(Array, Array) (Line: 432)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template->display(Array) (Line: 411)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/themes/classy/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 665)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Comments

davidm777 created an issue. See original summary.

davidm777’s picture

Issue summary: View changes
davidm777’s picture

davidm777’s picture

Changes

Initialization of variables prevents Notices from being thrown (when php.ini is set to show them).

Also removed the use of temporary variables and just using the $config array (ex. no longer using $facebook as a temporary placeholder for $config['simplesharer_facebook']).

Steps to reproduce:

  1. Make sure your PHP settings are as such that Notices/Warnings are being displayed. There are various ways to do this depending on your setup. This StackOverflow post has some good suggestions.
  2. Install module
  3. Add module to a relevant block
  4. Configure the module (in the block) to not use at least one social-media button/link
  5. Save the changes
  6. Load a page that uses the relevant block
  7. Without the patch, one will see Notice(s) for each social-media button that is disabled
  8. With the patch, the page will load without error

Of note, one has to either clear/flush the render cache or change a setting in a block (cf. "Configure the module..." above) in order to continue to see the Notice errors. (sans patch). After the first time the page is loaded, it will not longer throw Notices.

davidm777’s picture

Status: Active » Needs review

Setting issue to 'Needs Review'

davidm777’s picture

Wow - hard to test my patch if there is no patch attached!! Please forgive my newbie mistake - in all of the 'Please wait 2000 seconds before posting a comment' stuff, it seems the patch-itself never made it into this Issue.

In theory (ha), I'm attaching it now.

ericdsd’s picture

#6 patch fixes the issue.
Thanks @davidm777
we could probably make it more modular by only adding values to $markup_content_array when the button is in use instead of setting null value, but maybe i'm wrong.

travis-bradbury’s picture

StatusFileSize
new7.04 KB

I don't think setting those to null is an issue since that's the default value in the theme hook definition. Or if it is an issue the default should be an empty array so it's always the same type of value.

This patch isn't revolutionary but I don't think #6 cleaned up enough. We don't need to give all those variables a default value if we just add them to the render array as we go.

This patch also removes a handful of unused calls to drupal_render and leaves the resulting render array as a render array instead of rendering the content and using it as #markup. It's a bit out of scope of the reported problem but I think it's still in the spirit of this issue.