2

I am working on a project involving migrating a site with many static pages to Drupal 8. Since a number of the pages are old blog posts, there's often broken links, broken images, etc.

For quality assurance, we are using the httrack command line tool to traverse the new Drupal 8 site and report broken links and images.

It turns out that Drupal 8 is putting some extra <link> tags in the page head (the <head> section of the html, not the header region or anything like that).

First, when there is a page with a taxonomy term in it, there are links to the term's admin pages in the head, like so:

<link rel="devel-definition" href="/devel/taxonomy_term/226/definition" />
<link rel="clone-form" href="/entity_clone/taxonomy_term/226" />
<link rel="token-devel" href="/taxonomy/term/226/devel/token" />
<link rel="display" href="/taxonomy/term/226/manage-display" />

These links are on the page even for anonymous users.

Second, we have media images in our pages, and they are getting weird head link tags, also. For instance, if we have the image inside-a-classroom.jpg, we'll have this: <link rel="image_src" href="Inside A Classroom" />.

The problem is, httrack is finding these links in head, trying to download them, and returning a 404. And of course it would, because when it spiders, it's not logged on and can't access admin menus. In the case of images, they're not even proper links to the image.

Is there any way we can turn these off? They are false negatives in our scraping, and make it difficult to quality check our work.

  • 1
    For me this looks like a Drupal permission issue or a server caching issue. Maybe anonymous user role has too many permissions granted? Also why enable Devel on live at all? And do you use something like Varnish, Akamai etc.? – leymannx Jun 11 '18 at 20:56
  • @leymannx It's not live, but our development/migration target server. We don't have any caching or in-betweens. I don't understand how they would know Drupal-specific paths anyway. – user1359 Jun 12 '18 at 1:14
  • Well, what happens when you disable the Devel module? – leymannx Jun 12 '18 at 1:19
  • @leymannx I tried this, and I'm still getting the link tags in the head : / – user1359 Jun 12 '18 at 13:49
1

Links like this are handled as attachments to the page. They are inserted where the Drupal placeholder tags (like head-placeholder) are in the twig file.

To modify these, you need to use hook_page_attachments_alter(). Here's example code that fixes a problem with canonical URLs containing query string not getting generated properly by core (converts & to &amp;).

/**
 * Implements hook_page_attachments_alter().
 */
function mytheme_page_attachments_alter(array &$attachments) {
  if ( isset($attachments['#attached']['html_head'])) {
    foreach ($attachments['#attached']['html_head'] as $key => $item) {
      if (in_array("canonical_url", $item)) {
        $href = $item[0]['#attributes']['href'];
        $attachments['#attached']['html_head'][$key][0] = [
          '#type' => 'inline_template',
          // Formatted to include a newline in page source.
          '#template' => "{{ href|raw }}
",
          '#context' => [
            'href' => '<link rel="canonical" href="' . $href . '"/>',
          ]
        ];
      }
    }
  }
}

In the case of these entity links, these are in the 'html_head_link' area of the attachments.

FYI - This is also related to this core issue.

  • I've used this hook to look at the contents of the attachments array, and I don't see the various extra page links. I do see elements such as head, shortlink, and canonical_url, so there must be something adding them later. – user1359 Jun 12 '18 at 14:57
  • These are in the 'html_head_link' key of the attached array. Updated answer. – CG Monroe Jun 12 '18 at 15:36
  • Yes, under that key I have elements such as head, shortlink, etc. as I mentioned above, but it does not contain elements that have taxonomy/term/212/devel/token, devel/taxonomy_term/213/render , etc. – user1359 Jun 12 '18 at 15:55
  • Hmm, I see them via a ksm dump if I'm logged in. They are not there for the anonymous user. These are basically the links defined in the entity definitions. So they also vary by what is being displayed. My test was displaying a taxonomy term page. They do have a odd structure like $attachments['html_head_link'][14][ ['rel'=>..., 'href'=> ..], TRUE] – CG Monroe Jun 12 '18 at 17:31
  • Thanks for taking the time to look into this to help me with my problem. In my theme, I just added a die(print_r(... in the page_alter hook. I don't have any of the image or taxonomy information in the key, either logged in or not. Just the ones I referenced above, and a few others. – user1359 Jun 12 '18 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.