diff --git a/amptheme/amptheme.theme b/amptheme/amptheme.theme index 2d29b70..0374e8c 100644 --- a/amptheme/amptheme.theme +++ b/amptheme/amptheme.theme @@ -205,55 +205,57 @@ function amptheme_preprocess_block(&$variables) { if ($variables['content']['site_logo']['#access'] && ($logo_uri = $variables['content']['site_logo']['#uri'])) { // Check if logo is an SVG. if (substr(strrev($logo_uri), 0, 4) === strrev('.svg')) { - // If logo begins with '/', create an absolute URL with domain name. + // The logo URI should be a relative path. if (substr($logo_uri, 0, 1) === '/') { - $logoUrl = \Drupal\Core\Url::fromUserInput($logo_uri); - $logoUrl->setAbsolute(); - $logo_absolute = $logoUrl->toString(); + $logo_uri = DRUPAL_ROOT . $logo_uri; } - else { - $logo_absolute = $logo_uri; - } - - // Load XML for SVG file. - $logo_xml = simplexml_load_file($logo_absolute); - $logo_attributes = $logo_xml->attributes(); - // SVG needs viewBox property in order to obtain height and width. - if (property_exists($logo_attributes, 'viewBox')) { - $logo_viewbox = (string) $logo_attributes->viewBox; - $logo_viewbox_coordinates = []; + if (file_exists($logo_uri)) { + // Load XML for SVG file. + $logo_xml = simplexml_load_file($logo_uri); + $logo_attributes = $logo_xml ? $logo_xml->attributes() : NULL; + } - // Create array of viewBox coordinates depending on item delimiter. - if (strpos($logo_viewbox, ',') !== false) { - $logo_viewbox_coordinates = explode(',', $logo_viewbox); - } - elseif (strpos($logo_viewbox, ' ') !== false) { - $logo_viewbox_coordinates = explode(' ', $logo_viewbox); + if (!empty($logo_attributes)) { + // The amp-img tag generated for the logo requires height and width to be defined. + if (property_exists($logo_attributes, 'height') && property_exists($logo_attributes, 'width')) { + $variables['site_logo_height'] = round($logo_attributes->height, 0); + $variables['site_logo_width'] = round($logo_attributes->width, 0); } + // Use the SVG viewBox property in order to obtain height and width. + elseif (property_exists($logo_attributes, 'viewBox')) { + $logo_viewbox = (string) $logo_attributes->viewBox; + $logo_viewbox_coordinates = []; - // Get SVG height and width from full viewBox coordinates. - if (!empty($logo_viewbox_coordinates[2]) && !empty($logo_viewbox_coordinates[3])) { - $variables['site_logo_height'] = round($logo_viewbox_coordinates[3], 0); - $variables['site_logo_width'] = round($logo_viewbox_coordinates[2], 0); + // Create array of viewBox coordinates depending on item delimiter. + if (strpos($logo_viewbox, ',') !== false) { + $logo_viewbox_coordinates = explode(',', $logo_viewbox); + } + elseif (strpos($logo_viewbox, ' ') !== false) { + $logo_viewbox_coordinates = explode(' ', $logo_viewbox); + } + + // Get SVG height and width from full viewBox coordinates. + if (!empty($logo_viewbox_coordinates[2]) && !empty($logo_viewbox_coordinates[3])) { + $variables['site_logo_height'] = round($logo_viewbox_coordinates[3], 0); + $variables['site_logo_width'] = round($logo_viewbox_coordinates[2], 0); + } } } } // If logo is not an SVG, we can use the image factory service. else { - // Force path to be stream wrapped uri. + // The logo URI should be a relative path. if (substr($logo_uri, 0, 1) === '/') { - global $base_root; - $logo_uri = $base_root . $logo_uri; - $public_stream_base_url = PublicStream::baseUrl(); - if (substr($logo_uri, 0, strlen($public_stream_base_url)) == $public_stream_base_url) { - $logo_uri = file_build_uri(substr($logo_uri, strlen($public_stream_base_url))); - } + $logo_uri = DRUPAL_ROOT . $logo_uri; + } + + if (file_exists($logo_uri)) { + /** @var ImageInterface $logo_image */ + $logo_image = \Drupal::service('image.factory')->get($logo_uri); } - /** @var ImageInterface $logo_image */ - $logo_image = \Drupal::service('image.factory')->get($logo_uri); - if ($logo_image->isValid()) { + if (!empty($logo_image) && $logo_image->isValid()) { // Get height and width of logo from image factory service. $variables['site_logo_height'] = $logo_image->getHeight(); $variables['site_logo_width'] = $logo_image->getWidth();