Hello!
I have the fatal error after creating messages (notifications):
The website encountered an unexpected error. Please try again later.
Error: Call to undefined method Drupal\Core\StringTranslation\TranslatableMarkup::toString() in Drupal\socialbase\Plugin\Preprocess\Activity->preprocess() (line 28 of profiles/contrib/social/themes/socialbase/src/Plugin/Preprocess/Activity.php).
How to reproduce:
1) The function "template_preprocess_activity()" has the hext strings (see 39th line):
$date = t('%time ago', ['%time' => $created_time_ago->getString()]);
if ($full_url == '') {
$variables['date'] = $date;
}
else {
$variables['date'] = Link::fromTextAndUrl($date, $full_url);
}
2) "Activity->preprocess()" has the next strings (see 27th line):
// Remove href from date.
$variables['date'] = strip_tags($variables['date']->toString()->getGeneratedLink());
But it works correctly only if we have Link in $variables['date']. When we have TranslatableMarkup in the variable, it generates fatal error because TranslatableMarkup has not "toString()" method (it has "__toString()" only).
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | social-fix_fatal_tostring-2956971-10.patch | 1.73 KB | mikhailkrainiuk |
| #9 | 2956971-activity-notifications-box.png | 252.94 KB | jaapjan |
| #8 | social-fix_fatal_tostring-2956971-8.patch | 1.72 KB | mikhailkrainiuk |
| #6 | 2956971-activitystream.png | 67.24 KB | jaapjan |
Comments
Comment #2
mikhailkrainiuk commentedI created the patch to fix it.
It works fine for me. Verify it, please.
Comment #3
jaapjan commentedThanks for the report and the patch.
Looks like there is a similar issue here which also describes the issue and has a patch:
https://www.drupal.org/files/issues/social-fix-fatal-error-for-activitie...
What do you think would be the best way to solve it?
Comment #4
mikhailkrainiuk commentedHmm.
I checked the patch from this issue.
As I see,
$variables['date']will have different types - string or object, which needs special methods before rendering.The patch from the current issue creates
$variables['date']with renderable data.And 30th string has a strange code:
TranslatableMarkup doesn't need special method for rendering string and can't build "getGeneratedLink". Maybe author wanted to check "instanceof Link"?
Well, looks like the patch from current issue is better. But let's see other opinions :)
Comment #5
jaapjan commentedI actually follow your logic and agree with you. Opened a PR for this on GH: https://github.com/goalgorilla/open_social/pull/818
Comment #6
jaapjan commentedActually I've just checked it, even though the tests are passing, the functionality does not work correctly yet on default Open Social installation.
See this screenshot and you'll notice that the output is printed as HTML.

Marking it as "Needs work" for now.
Comment #7
mikhailkrainiuk commentedSure. We have
strip_tags(), it changes HTML link.If we build plain text "5 days ago", it remains plain.
If we build a link, it doesn't need "strip_tags" command.
I created another patch to fix it. Verify it, please.
Comment #8
mikhailkrainiuk commentedHmm. We have a problem with Link::toString() - it makes HTML to plain text.
I updated the patch to fix it.
P.S. we use "@time ago" instead of "%time ago" because "%" generates inner HTML in link (new "em" tag). We have a problem with Link with inner HTML (see https://www.drupal.org/node/2392803).
But HTML inside link is not required in the current case.
Comment #9
jaapjan commentedThe activity streams and node/post/comment page appears to be working now. But there is a new issue in the activity items now in the block in the header. See screenshot:

Appears to be because the removed line in themes/socialbase/src/Plugin/Preprocess/Activity.php
Not sure how to solve that in a flexible way. Any ideas?
Comment #10
mikhailkrainiuk commentedWe have the problem with double links:
File social/themes/socialbase/templates/activity/activity--notification.html.twig has code:
We have link in {{ date }} inside.
But this code is wrapped in the link too.
Browsers get the conflict:
Tag "a" inside tag "a".
If we have a link in the template, I suggest removing the link from the date. I updated the patch.
I tested it on the local website with Social - http://prntscr.com/j2u04n
HTML is rendered correct now and I have links for each notification.
Comment #11
jaapjan commentedAh, I see. Yes, browsers don't like that! I personally think the link to the original entity on the date is an important feature we should not remove. I suggest to check for view_mode notification or notification_archive instead:
https://github.com/goalgorilla/open_social/pull/818/commits/ced9a1223c4b...
Comment #12
jaapjan commentedIssue is merged and will be in the next minor release (1.17 is planned). Thanks!