Problem/Motivation

We found one issue linked to Style Guide module.

In database log, you can see next message: Theme hook link not found.

Problem is actually for all themes on the site, for example, Drupal core themes Seven and Bartik.

The problem actually only on the Style Guide pages.
Log message has Warning shortcut.

Screenshots attached to the bottom of the issue.

We found one item with this hook in:
/styleguide/src/Plugin/Styleguide/defaultStyleguide.php:692

And another usage found in:
/styleguide/src/Generator.php:168

Steps to reproduce:

1. Install clear Drupal 8.5.x-dev instance
2. Install module styleguide 8.x-1.x-dev
3. Go to the page /admin/appearance/styleguide/seven
4. Go to the page /admin/reports/dblog
5. Click on Warning message with title 'theme'

Proposed resolution

In Style Guide module need to delete hook 'link' from items because this hook not implemented in Drupal core theme registry.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ysamoylenko created an issue. See original summary.

ysamoylenko’s picture

Issue summary: View changes
ysamoylenko’s picture

Issue summary: View changes
ysamoylenko’s picture

Issue summary: View changes
ioana apetri’s picture

Status: Needs work » Needs review
FileSize
600 bytes

Hello,
Please review my patch, I am not sure about it, because I can't reproduce the warning in DB log.
Thanks.

Oleksiy’s picture

Status: Needs review » Needs work

Hello. Thanks for the patch.

The styleguide_links is Styleguide theme hook. It used to display the page elements navigation menu, so we shouldn't remove it.

The issue comes from Generator (http://cgit.drupalcode.org/styleguide/tree/src/Generator.php?h=8.x-1.x#n168) where used this missed theme hook link. So the link generation in sentence method needs to be fixed to build it correctly.

ysamoylenko’s picture

Hello,
Please review my patch.
All elements on the Style Guide pages not changed.
In DB log no any warning messages from Style Guide module.
Thanks.

ysamoylenko’s picture

Status: Needs work » Needs review
Oleksiy’s picture

Status: Needs review » Needs work

Your patch fixed the warning messages, but the links are still not displaying on the StyleGuide page.

I think it's because of '#url' => $link, , the #url option requires Drupal\Url object (core/lib/Drupal/Core/Render/Element/Link.php)

ysamoylenko’s picture

Status: Needs work » Needs review
FileSize
1.93 KB

Hello,
Please review my new patch.
All elements on the Style Guide pages not changed.
Links are successfully rendered in Drupal messages if they are valid.
In DB log no any warning messages from Style Guide module.
Please check the patch on the Drupal 8.4.x or 8.5.x-dev version.
Module version 8.x-1.x-dev.
Thanks.

Oleksiy’s picture

Thanks for the patch.

+++ b/src/Generator.php
@@ -154,6 +154,8 @@ class Generator implements GeneratorInterface {
+
+

Please remove these extra empty lines

+++ b/src/Generator.php
@@ -163,16 +165,20 @@ class Generator implements GeneratorInterface {
+      if (filter_var($link, FILTER_VALIDATE_URL)) {
+        $explode = explode(' ', $sentence);
+        $link = [
+          '#type' => 'link',
+          '#title' => $explode[0],
+          '#url' => \Drupal\Core\Url::fromUri($link),

We should allow adding any kind of URL in the sentence. Looks like will be better to move \Drupal\Core\Url::fromUri($link) outside the sentence() method.

+++ b/src/Plugin/Styleguide/defaultStyleguide.php
@@ -529,7 +529,7 @@ class DefaultStyleguide extends StyleguidePluginBase {
-      drupal_set_message($this->generator->sentence('http://www.example.com'), $message);
+      drupal_set_message(t($this->generator->sentence('http://www.example.com')), $message);

I mean we should create and pass this \Drupal\Core\Url object here, this way a user can create the Url object using any available method and pass it into $this->generator->sentence() method. Also, this link path shouldn't be translatable.

ysamoylenko’s picture

Thanks for the review.
Please say about behaviour for the link in this case.
Link must be set for the whole sentence or, must look like the original variant, the first word is a link, next part of the sentence is plain text?

Oleksiy’s picture

Behaviour stays the same as before.
Just instead of using the 'http://www.example.com' (string) in $this->generator->sentence('http://www.example.com') need to pass object (\Drupal\Core\Url) as argument.

ysamoylenko’s picture

Hello, I try to pass object Url with call it in method sentence

$this->generator->sentence(Url::fromUri('http://www.example.com'))

and changed taked param link to * @param \Drupal\Core\Url $link in /src/GeneratorInterface.php:110,
but URL class with method fromUri is returned only Url object and can not be converted to string inside implode function from sentence method in Generator class.

Also, link in sentence() method needed Link title from the first word in the generated sentence. I think it can with usage Link class, for example:

$explode[0] = \Drupal\Core\Link::fromTextAndUrl($explode[0], $link)->toString();

And additional I found that sentence() method taken as the link from another calls string or integer values as the link, for fix it needs to implement the filter in sentence method for not object values or refactor another call of the sentence() method.

In case if link is taken to string value we need to translate link title with usage
t()
function or use another way to markup link with string translation.

Could you say any another way to fix the link in messages if my thinks are not correct?

ysamoylenko’s picture

FileSize
2.42 KB

New patch attached to the bottom of the message.
Please check the patch on the Drupal 8.4.x or 8.5.x-dev version.
Module version 8.x-1.x-dev.

Please add to testing environment :
Drupal versions 8.4.x 8.5.x-dev and style guide module version 8.x-1.x.

Oleksiy’s picture

The goal was to keep '#path' => $link, , just make the $link of \Drupal\Core\Url type, as the '#type' => 'link', requires for the '#path' \Drupal\Core\Url value.
No need to use the t() function here, we generate new sentence every time.

ysamoylenko’s picture

I created a new patch. Please review it.

It keeps code structure from my first patch, but not call \Drupal\Core\Link class, this time to sentence() method comes \Drupal\Core\Url object from drupal_set_message() function in defaultStyleguide.php class. Sentence() method returns markup object in this case.

Oleksiy’s picture

Status: Needs review » Needs work
+++ b/src/Generator.php
@@ -165,18 +166,19 @@ class Generator implements GeneratorInterface {
         '#options' => array(
           'attributes' => array(),
           'html' => FALSE,
-        ),
+          ),

Please remove 2 extra spaces. All other code looks good now :)

ysamoylenko’s picture

The new patch uploaded to the top of the message. Please review it.
Thanks for reviewing my patches.

ysamoylenko’s picture

Status: Needs work » Needs review

  • Oleksiy committed fc5e470 on 8.x-1.x authored by ysamoylenko
    Issue #2928676 by ysamoylenko, Oleksiy: Theme hook link not found
    
Oleksiy’s picture

Status: Needs review » Fixed

Thank you! The patch was committed.

Status: Fixed » Closed (fixed)

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