Every time a form is submitted where the simple hierarchical select widget generates a form error, the following PHP Error is returned:

The website encountered an unexpected error. Please try again later.
Error: Using $this when not in object context in Drupal\shs\Plugin\Field\FieldWidget\OptionsShsWidget::validateElement() (line 355 of modules/contrib/shs/src/Plugin/Field/FieldWidget/OptionsShsWidget.php).

Drupal\shs\Plugin\Field\FieldWidget\OptionsShsWidget::validateElement(Array, Object, Array)
call_user_func_array(Array, Array) (Line: 280)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 236)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object) (Line: 236)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'node_person_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('node_person_form', Array, Object) (Line: 589)
Drupal\Core\Form\FormBuilder->processForm('node_person_form', Array, Object) (Line: 321)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 91)
Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
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: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
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: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
CommentFileSizeAuthor
#2 3242736-1.patch1.17 KBkomlenic

Comments

komlenic created an issue. See original summary.

komlenic’s picture

StatusFileSize
new1.17 KB

Patch attached.

komlenic’s picture

Status: Active » Needs review
komlenic’s picture

Issue summary: View changes
nicolabelleri’s picture

I had the same Problem.
Patch #2 works fine for me, thanks.

Using Drupal 9.2.7

afeijo’s picture

Patch #2 also worked for me, thanks! I'm also using D9.2.7

dahousecat’s picture

Patch #2 worked for me on D8.9.19.

komlenic’s picture

Status: Needs review » Reviewed & tested by the community

I'm moving this to RTBC due to the simplicity of the change.

stephen-cox’s picture

Patch works for me on Drupal 8.9.19 and 9.2.7.

seeallie’s picture

The same issue occurs in 2.0.0-rc1

  • komlenic authored 8bfbab8 on 2.0.x
    Issue #3242736 by komlenic: Error: Using $this when not in object...
stborchert’s picture

Status: Reviewed & tested by the community » Fixed

Thanks @komlenic for the patch and all the others for testing :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

stephane888’s picture

I had the same Problem.
Patch #2 works fine for me, thanks.

Using Drupal 9.2.10

crtlf’s picture

Patch #2 also working with Drupal 9.3.0.

Thank you !

summit’s picture

Hi, Patch #2 not working using latest SHS dev and drupal 9.3.3, and these patches:

-  "Error: Using $this when not in object context (https://www.drupal.org/project/shs/issues/3242736)": "https://www.drupal.org/files/issues/2021-10-11/3242736-1.patch",
- "Save term lineage (https://www.drupal.org/project/shs/issues/3248983)" : "https://www.drupal.org/files/issues/2022-01-22/3248983-5.patch",
- "Add functionality to add new terms (https://www.drupal.org/project/shs/issues/2712115)": "https://www.drupal.org/files/issues/2021-05-10/shs-add-new-terms-functionality-2712115-60.patch"

Error status:

- Applying patches for drupal/shs
    https://www.drupal.org/files/issues/2021-10-11/3242736-1.patch (Error: Using $this when not in object context (https://www.drupal.org/project/shs/issues/3242736))
   Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2021-10-11/3242736-1.patch
Installation failed, reverting ./composer.json and ./composer.lock to their original content.
                                                                       
  [Exception]                                                                  
  Cannot apply patch Error: Using $this when not in object context (https://w  
  ww.drupal.org/project/shs/issues/3242736) (https://www.drupal.org/files/iss  
  ues/2021-10-11/3242736-1.patch)!            

Thanks for looking into this.
greetings, Martijn

serverjohn’s picture

I second @Summit. Patch #2 is not working with Drupal 9.3.3 and SHS 2.0.0-rc1. Applying the patch with Git returns Skipped patch 'src/Plugin/Field/FieldWidget/OptionsShsWidget.php'. Applying the patch with the patch command just hangs.

I manually made the changes in the patch to the shs/src/Plugin/Field/FieldWidget/OptionsShsWidget.php file. This resolved the issue. I have never done a patch before but I am gonna try. BRB.

serverjohn’s picture

It looks like the latest version of the code has the fix in it. The problem is that the recommended version on the main SHS module page is for 2.0.0-rc1 which does not include the fix.

As a note the same page mentions:

Note on versions
SHS 8.x-1.0-alpha3 is the last version working on Drupal <8.6!

If you plan to use SHS on sites using Drupal >=8.6 you must use SHS 8.x-1.x-dev or versions newer than 8.x-1.0-alpha3.

Is it possible to get the recommended version updated to include this fix? Either way use the other version and you won't this problem.

komlenic’s picture

Yes, the module needs a new release to incorporate this and other changes. The changes are necessary to use the module in Drupal 9 on production sites (without resorting to using a dev version).

serverjohn’s picture

I made a mistake. This patch does work. I didn't execute the patch command properly... :-( There is an additional issue with the same problem. Both resolve the issue. Duplicate: https://www.drupal.org/project/shs/issues/3265197