Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3417011 by nkoporec: Error: Call to a member function hasField() on null #3722

Merged

Conversation

nkoporec
Copy link
Contributor

Problem

In our logs we are spotting Error: Call to a member function hasField() on null errors, its related to a missing profile.

Full error:

Error: Call to a member function hasField() on null in Drupal\\social_activity\\EmailTokenServices->getUserPreview() 
(line 252 of /app/html/profiles/contrib/social/modules/social_features/social_activity/src/EmailTokenServices.php) 
#0 /app/html/profiles/contrib/social/modules/social_features/social_group/social_group.tokens.inc(207): 
Drupal\\social_activity\\EmailTokenServices->getUserPreview(Object(Drupal\\social_user\\Entity\\User)) 
#1 /app/html/core/lib/Drupal/Core/Extension/ModuleHandler.php(545): social_group_tokens_alter(Array, Array, 
Object(Drupal\\Core\\Render\\BubbleableMetadata)) #2 /app/html/core/lib/Drupal/Core/Utility/Token.php(368): 
Drupal\\Core\\Extension\\ModuleHandler->alter('tokens', Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) 
#3 /app/html/core/lib/Drupal/Core/Utility/Token.php(241): Drupal\\Core\\Utility\\Token->generate('message', Array, Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) #4 /app/html/core/lib/Drupal/Core/Utility/Token.php(191): Drupal\\Core\\Utility\\Token->doReplace(true, '<p><a href=\"[me...', Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) #5 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(593): Drupal\\Core\\Utility\\Token->replace('<p><a href=\"[me...', Array, Array, Object(Drupal\\Core\\Render\\BubbleableMetadata)) #6 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(513): Drupal\\activity_creator\\ActivityFactory->processTokens(Array, Array, Object(Drupal\\message\\Entity\\Message)) #7 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(204): Drupal\\activity_creator\\ActivityFactory->getMessageText(Object(Drupal\\message\\Entity\\Message)) #8 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(135): Drupal\\activity_creator\\ActivityFactory->getFieldOutputText(Object(Drupal\\message\\Entity\\Message)) #9 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/ActivityFactory.php(110): Drupal\\activity_creator\\ActivityFactory->buildActivities(Array) #10 /app/html/profiles/contrib/social/modules/custom/activity_creator/src/Plugin/QueueWorker/ActivityWorkerActivities.php(61): Drupal\\activity_creator\\ActivityFactory->createActivities(Array) #11 /app/html/core/lib/Drupal/Core/Cron.php(183): Drupal\\activity_creator\\Plugin\\QueueWorker\\ActivityWorkerActivities->processItem(Array) #12 /app/html/modules/contrib/ultimate_cron/src/UltimateCron.php(70): Drupal\\Core\\Cron->processQueues() #13 /app/html/modules/contrib/ultimate_cron/src/ProxyClass/UltimateCron.php(70): Drupal\\ultimate_cron\\UltimateCron->run() #14 /app/vendor/drush/drush/src/Commands/core/DrupalCommands.php(63): Drupal\\ultimate_cron\\ProxyClass\\UltimateCron->run() #15 [internal function]: Drush\\Commands\\core\\DrupalCommands->cron(Array) #16 /app/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array(Array, Array) #17 /app/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\\AnnotatedCommand\\CommandProcessor->runCommandCallback(Array, Object(Consolidation\\AnnotatedCommand\\CommandData)) #18 /app/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): 
Consolidation\\AnnotatedCommand\\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\\AnnotatedCommand\\CommandData)) #19 /app/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(391): Consolidation\\AnnotatedCommand\\CommandProcessor->process(Object(Symfony\\Component\\Console\\Output\\ConsoleOutput), Array, Array, Object(Consolidation\\AnnotatedCommand\\CommandData)) #20 /app/vendor/symfony/console/Command/Command.php(326): Consolidation\\AnnotatedCommand\\AnnotatedCommand->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #21 /app/vendor/symfony/console/Application.php(1096): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #22 /app/vendor/symfony/console/Application.php(324): Symfony\\Component\\Console\\Application->doRunCommand(Object(Consolidation\\AnnotatedCommand\\AnnotatedCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #23 /app/vendor/symfony/console/Application.php(175): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #24 /app/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #25 /app/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\\Runtime\\Runtime->doRun(Array, Object(Symfony\\Component\\Console\\Output\\ConsoleOutput)) #26 /app/vendor/drush/drush/drush.php(139): Drush\\Runtime\\Runtime->run(Array) #27 /app/vendor/drush/drush/drush(4): require('/app/vendor/dru...') #28 /app/vendor/bin/drush(119): include('/app/vendor/dru...') #29 {main}. {\"exception\":\"[object] (Error(code: 0): Call to a member function hasField() on null at /app/html/profiles/contrib/social/modules/social_features/social_activity/src/EmailTokenServices.php:252)\",\"severity_level\":3}


Solution

Even though the profile should always be there and I think this might even be a setup issue, we can still improve the code and
make sure to not call hasField method if the variable is not the correct type.

Issue tracker

https://www.drupal.org/project/social/issues/3417011

Theme issue tracker

How to test

  • Create a user without a profile and send them an email.

Definition of done

Before merge

  • Code/peer review is completed
  • All commit messages are clear and clean. If applicable a rebase was performed
  • All automated tests are green
  • Functional/manual tests of the acceptance criteria are approved
  • All acceptance criteria were met
  • New features or changes to existing features are covered by tests, either unit (preferably) or behat
  • Update path is tested. New hook_updates should respect update order, right naming convention and consider hook_post_update code
  • Module can be safely uninstalled. Update/implement hook_uninstall and make sure that removed configuration or dependencies are removed/uninstalled
  • This pull request has all required labels (team/type/priority)
  • This pull request has a milestone
  • This pull request has an assignee (if applicable)
  • Any front end changes are tested on all major browsers
  • New UI elements, or changes on UI elements are approved by the design team
  • New features, or feature changes are approved by the product owner

After merge

  • Code is tested on all branches that it has been cherry-picked
  • Update hook number might need adjustment, make sure they have the correct order
  • The Drupal.org ticket(s) are updated according to this pull request status

Screenshots

Release notes

Fixed an issue with undefined error when sending an email to a user without a profile.

Change Record

Translations

@nkoporec nkoporec added type: bug Fixes a bug in Open Social status: needs review This pull request is waiting for a requested review prio: low team: guardians labels Jan 25, 2024
@nkoporec nkoporec added this to the 12.0.6 milestone Jan 25, 2024
Copy link

mergeable bot commented Jan 25, 2024

Thanks for contributing towards Open Social! A maintainer from the @goalgorilla/maintainers group might not review all changes from all teams/contributors. Please don't be discouraged if it takes a while. In the meantime, we have some automated checks running and it might be that you will see our comments with some tips or requests to speed up the review process. 😊

@nkoporec nkoporec force-pushed the bug/issue-3417011/call-to-function-hasfield-on-null branch from cb916a3 to b408c3a Compare January 26, 2024 13:44
@ronaldtebrake ronaldtebrake modified the milestones: 12.0.6, 12.0.7 Jan 30, 2024
@robertragas robertragas force-pushed the bug/issue-3417011/call-to-function-hasfield-on-null branch from b408c3a to 3f04d74 Compare January 30, 2024 08:03
@robertragas robertragas force-pushed the bug/issue-3417011/call-to-function-hasfield-on-null branch from 3f04d74 to 18b06ed Compare January 30, 2024 14:01
@ronaldtebrake ronaldtebrake modified the milestones: 12.0.7, 12.0.8 Jan 30, 2024
@robertragas
Copy link
Contributor

Approved, code looks good and tested the email out without and with profile, no weird stuff. Just some extra information once they have a profile.

Reason that the profile did not get created in some cases can be related to internal migrate module that alters the registration hooks and skips the profile creation. In distro cases it likely doesn't happen, but still good to have the defensive check in there.

@robertragas robertragas self-requested a review January 30, 2024 14:24
@robertragas robertragas merged commit 2c9c136 into main Jan 30, 2024
187 checks passed
@robertragas robertragas deleted the bug/issue-3417011/call-to-function-hasfield-on-null branch January 30, 2024 20:55
@robertragas robertragas added backport: verified This pull request has been back ported to an older minor version status: no action needed and removed status: needs review This pull request is waiting for a requested review labels Jan 31, 2024
@robertragas
Copy link
Contributor

Also cherry-picked now to 12.0.x and 12.1.x

@ribel
Copy link
Contributor

ribel commented Mar 20, 2024

Hi @robertragas, and @nkoporec,
I don't see this change in the newest version of Open Social.
Could you please check if it was actually cherry-picked? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport: verified This pull request has been back ported to an older minor version prio: low team: guardians type: bug Fixes a bug in Open Social
4 participants