Sunday, October 27, 2019

Drupal 7 / Ubercart 7 - Remove Language Switcher Dropdown Undefined Index Errors

Drupal core 7.67 / Ubercart 7.x-3.13  / Stability Theme 

Following years of frustratingly hard-to-reach, disinterested and/or incompetent and needlessly expensive Drupal developers we have set up an affordable commercial service for Small and Medium sized Enterprise (SME) decision-makers who rely on Drupal to support their business, like us.  So, if you need help with this problem, or any other form of Drupal Wizardry, feel free to contact us via our new division, Drupal Wizard, which provides support for all versions of Drupal.  

We offer the following services to serve the unique and evolving needs of your business:
  • 24/7 Emergency Support
  • Low-Cost Annual Support Packages
  • Drupal Module Integration and Development
  • Linux and Drupal Scheduled Systems Maintenance
Visit www.drupalwizard.com for more information.


Remove Language Switcher Dropdown Undefined Index Errors


After installing the Language Switcher Dropdown Module, and using it for a little while, my Administrator interface began to be filled with these errors: 



Turns out this is just due to a bit of lazy coding and a lot of lazy fixing.  

A fix is readily available for this problem for two years now, that hasn't made its way into the module core code yet.  Shameful!  

The fix is really straightforward; it involves checking to see if the entity exists before using it.  Unfortunately, the "fix" remains a patch, which means that if you update the module and the fix hasn't been implemented, it will vanish.

In other words we are once again being forced to wildcat hack Drupal modules.

Here's an "OK" solution I found on the Drupal.org website, that avoids this error by simply checking that entity being used isn't empty(), which leads to a null pointer error (or some such).  There is a typo in the following fix, which I have corrected later on in this document.


diff --git a/lang_dropdown.module b/lang_dropdown.module
index 8e1a5c1..47e6114 100644
--- a/lang_dropdown.module
+++ b/lang_dropdown.module
@@ -452,7 +452,7 @@ function lang_dropdown_language_switch_links_alter(&$links, $type, $path) {
   foreach ($links as $langcode => $link) {
 
     $translation_access  = FALSE;
-    if (module_exists('entity_translation')) {
+    if (!empty($translations_data[$langcode]) && module_exists('entity_translation')) {
       $translation_access = entity_translation_access(
         $translations_data[$langcode]['entity_type'],
         $translations_data[$langcode]);


(from https://www.drupal.org/project/lang_dropdown/issues/2946147)

But you'll notice that the error in this case appears in two places, not one, so this particular fix might need to be applied in two places, potentially:


  • Notice: Undefined index: zh-hant in lang_dropdown_language_switch_links_alter() (line 457 of /var/www/vhosts/holisticpethelp.com/www/mobile/sites/all/modules/lang_dropdown/lang_dropdown.module).
  • Notice: Undefined index: zh-hant in lang_dropdown_language_switch_links_alter() (line 458 of /var/www/vhosts/holisticpethelp.com/www/mobile/sites/all/modules/lang_dropdown/lang_dropdown.module).

But, then again, maybe not.  

Here's the offending lines, and it turns out they are dependent on a single if statement.


    if (!empty($translations_data[$langcode]) &&
      (module_exists('entity_translation')) {
      $translation_access = entity_translation_access(
        $translations_data[$langcode]['entity_type'],
        $translations_data[$langcode]);

    }

So, fixing the if statement should resolve both errors.  Two for one!

Solving this problem was pretty simple once I had an idea of what the problem was, and where it was.

My Fallback Plan:

First, I documented the existing problem, leaving the original code in place as a fallback, which came in handy, because the original fix code crashed Drupal!

/*
//  GL  2019-10-27  Getting undefined index errors with this block of code - BEGIN
    if (module_exists('entity_translation')) {
      $translation_access = entity_translation_access(
        $translations_data[$langcode]['entity_type'],
        $translations_data[$langcode]);
    }
//  GL  2019-10-27  Getting undefined index errors with this block of code - END
*/

Next, I fixed the code by 

- Inserting !empty($translations_data[$langcode]) && into the if condition

//  GL  2019-10-27  Replacement block of code with !empty check added - BEGIN
    if ( !empty($translations_data[$langcode]) && module_exists('entity_translation') ) {
      $translation_access = entity_translation_access(
        $translations_data[$langcode]['entity_type'],
        $translations_data[$langcode]);
    }
//  GL  2019-10-27  Replacement block of code with !empty check added - END

And then tested the result.

Lo!  The error went away!  





Now we know how to get rid of these kinds of errors


REFERENCES:

https://www.drupal.org/project/lang_dropdown


https://www.drupal.org/project/lang_dropdown/issues/2946147

No comments:

Post a Comment