0

I need to change this tag <img> to <amp-img>, I've been researching about this and I have this function for change the img tag.

function amptheme_image($variables) {
  // Remove RDF properties incompatible with AMP specification.
  if (isset($variables['attributes']['typeof'])) {
    unset($variables['attributes']['typeof']);
  }

  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      if (empty($attributes[$key])) {
        $attributes[$key] = $variables[$key];
      }
    }
  }

  return '<amp-img' . drupal_attributes($attributes) . '></amp-img>';
}

This function should change <img> to <amp-img> but it's not working.

The img tag is not working

I think it's maybe for the name of the function. My theme's name is bartik, but the function example it's amptheme_image.

I just did tried like this bartiktheme_image, also bartik_theme_image, but it's the same error.

I also tried like this, theme_image, but boom error cause there's another function with the same name.

Thanks for the answers and suggestions.

0

I think it's maybe for the name of the function. My theme's name is bartik, but the function example it's amptheme_image.

Without more info it's hard to be sure, but it sounds like you're adding this code to the core bartik.theme file? If so then yes, that's why it's not working. You need to create a subtheme, add your code to its .theme file, and then enable that theme on the front end.

The function must be named <themename>_image, so to use your existing code exactly as-is, you should make sure the machine name of your subtheme is amptheme.

Once you've done that and rebuilt the cache, your image elements should start using the override you've setup.

  • Yes, I'm adding the code to the core bartik.theme file. I don't know if it's necessary a subtheme because this is only for a node (Machine name: ir_news). Or there's a way to apply the Google AMP just for a content type using a sub theme without change the theme style. That's why I'm doing this with this code. Please, any help will be appreciated. – user3123766 Apr 24 at 19:21

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.